坚持 成长 每日一篇

使用UIView的动画套件

使用UIView的套件包含动画,[UIView beginAnimations:nil context:nil]和[UIView commitAnimations]来包含动画。
可以设置的动画常用方法如下:

//设置动画代理对象,当动画开始或者结束时会发消息给代理对象
+ (void)setAnimationDelegate:(id)delegate     

//当动画即将开始时,执行delegate对象的selector,并且把beginAnimations:context:中传入的参数传进selector,第一个参数字符串可以直接进入,第二个参数如果是字符串也可以直接进入,其他还有待研究
+ (void)setAnimationWillStartSelector:(SEL)selector 

// 当动画结束时,执行delegate对象的selector,并且把beginAnimations:context:中传入的参数传进selector  
+ (void)setAnimationDidStopSelector:(SEL)selector 

//动画的持续时间,秒为单位
+ (void)setAnimationDuration:(NSTimeInterval)duration   

//动画延迟delay秒后再开始
+ (void)setAnimationDelay:(NSTimeInterval)delay  
//动画的开始时间,默认为now
+ (void)setAnimationStartDate:(NSDate *)startDate   
//动画的节奏控制,动画是淡入还是淡出,动画的快慢
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve  
//动画的重复次数
+ (void)setAnimationRepeatCount:(float)repeatCount  
//如果设置为YES,代表动画每次重复执行的效果会跟上一次相反
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses  
//设置视图view的过渡效果, transition指定过渡类型, cache设置YES代表使用视图缓存,性能较好,设置为YES页面会静止
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache

用例

NSDate *date = [NSDate date];
    [UIView beginAnimations:@"222" context:(__bridge void * _Nullable)(date)];
    [UIView setAnimationDuration:1];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [view removeFromSuperview];
    [UIView setAnimationWillStartSelector:@selector(startAnimation:context:)];
    [UIView setAnimationDidStopSelector:@selector(stopAnimation:context:)];
     [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
    [UIView commitAnimations];

注意:在没有块动画时候,我们一般是使用动画套件,但是iOS出了块动画,苹果一般推荐使用块动画设置动画结束和开始

块动画

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations //durantion设置动画时间,代码块设置动画结束状态
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion//delay多久开始动画,options是可以设置动画缓存,动画节奏,动画是否重复,可以&&设置多项。completion是动画结束后的回调

注意:代码块里面可以结合套件动画来使用,options可以设置动画时候是否现实状态栏。
例子:

[UIView animateWithDuration:_durantion animations:^{
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationTransition:transition forView:view cache:YES];
    }];

Layer层动画

Layer层动画通过创建动画对象然后添加到Layer层完成动画设置

//创建CATransition对象
    CATransition *animation = [CATransition animation];
    //设置运动时间
    animation.duration = _durantion;

    //设置运动type
    animation.type = type;
    if (subtype != nil) {

        //设置子类
        animation.subtype = subtype;
    }

    //设置运动速度
    animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;

    [view.layer addAnimation:animation forKey:@"animation"];

贴一个动画的博客http://www.cocoachina.com/ios/20141226/10775.html

总结:无论是何种动画,都可以实现一些简单的翻页动画效果