从事iOS开发也有将近一年,以前出了问题都是网上去别人的博客里找资料,现在自己记录下来,权作当是巩固一下,也方便以后查阅。
今天在开发的时候需要自己做一个欢迎界面,要求有界面切换,网上找了些资料,自己试了一下,效果实现了。
UIView有一个_layer(CALayer)的属性,通过该属性来实现绘画界面。动画效果由类CATransition来实现。
/* The name of the transition. Current legal transition types include
* `fade', `moveIn', `push' and `reveal'. Defaults to `fade'. */
@property(copy) NSString *type;
/* An optional subtype for the transition. E.g. used to specify the
* transition direction for motion-based transitions, in which case
* the legal values are `fromLeft', `fromRight', `fromTop' and
* `fromBottom'. */
@property(copy) NSString *subtype;
CATransition的两个主要属性,type是切换的样式,例如渐隐,覆盖;subtype是切换方向,从左或是右
type
{
kCATransitionFade, //淡入淡出
kCATransitionMoveIn, //覆盖
kCATransitionPush, //推挤
kCATransitionReveal, //揭开
//以上4种我在类库里面能找到,下面的几种是在网上看来的,需要注意的是字符串大小写不能搞错,否则会不生效
cube, //立方体
suckEffect, //吮吸
oglFlip, //翻转
rippleEffect, //波纹
pageCurl, //翻页
pageUnCurl, //反翻页
cameraIrisHollowOpen, //开镜头
cameraIrisHollowClose, //关镜头
}
subtype
{
kCATransitionFromLeft;
kCATransitionFromBottom;
kCATransitionFromRight;
kCATransitionFromTop;
}
设置完加载方式后,还有两个重要属性:切换的时间和运动效果
/* The basic duration of the object. Defaults to 0. */
@property CFTimeInterval duration;
/* A timing function defining the pacing of the animation. Defaults to
* nil indicating linear pacing. */
@property(strong) CAMediaTimingFunction *timingFunction;
duration是设置切换效果的持续时间,长短自己设置
timingFunction
{
CA_EXTERN NSString * const kCAMediaTimingFunctionLinear
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseIn
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseOut
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseInEaseOut
CA_EXTERN NSString * const kCAMediaTimingFunctionDefault
}
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
这四个属性设置完成之后,就可以调用CALayer::addAnimation:(CAAnimation *)forKey:(NSString *)来实现效果了。
[self.view.layer addAnimation:transition forKey@"animation"]
调用该函数之后,当前界面就会显示相应的效果。如果这个时候要替换背景图片,就能实现页面以特定的方式切换。
接下来实现动画切换,动画切换主要是调用UIView的block动画来完成。
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0, completion = NULL
UIViewAnimationTransitionNone 正常
UIViewAnimationTransitionFlipFromLeft 从左向右翻
UIViewAnimationTransitionFlipFromRight 从右向左翻
UIViewAnimationTransitionCurlUp 从下向上卷
UIViewAnimationTransitionCurlDown 从上向下卷
[UIView animateWithDuration:1.0 animations:^{
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
}];
或是其他加入延时、结束切换等操作的函数
[UIView animateWithDuration:2.0 animations:^{
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];}
completion:^(BOOL Finish){
NSLog(@"Finish");
}
];