代码改变世界

iOS UI进阶-3.0 核心动画

2015-09-29 11:51  jiangys  阅读(331)  评论(0)    收藏  举报

Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍,使用它需要先添加QuartzCore.framework和引入对应的框架<QuartzCore/QuartzCore.h>

Core Animation的使用,请参考最下面的博客。

由于Core Animation动画,改变的只是一个影子,实际的位置和尺寸都不会有变化。因而,在实际开发中,还是建议直接使用UIView动画。

UIView动画

- (void)testViewSimpleAnim
{
    [UIView beginAnimations:nil context:nil];
    // 动画执行完毕后, 会自动调用self的animateStop方法
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animateStop)];
    self.myview.center = CGPointMake(200, 300);
    [UIView commitAnimations];
}

-(void)animateStop
{
    NSLog(@"%@",@"--animateStop--");
}

 或者:

    [UIView animateWithDuration:1.0 animations:^{
        self.myview.center = CGPointMake(200, 300);
    } completion:^(BOOL finished) {
        
    }];

效果是从红色框移动:

执行动画所需要的工作由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代表使用视图缓存,性能较好
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache

Block动画

方式一:

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

参数解析:

  • uduration:动画的持续时间
  • udelay:动画延迟delay秒后开始
  • uoptions:动画的节奏控制
  • uanimations:将改变视图属性的代码放在这个block中
  • ucompletion:动画结束后,会自动调用这个block

方式二:

+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

参数解析:

  • uduration:动画的持续时间
  • uview:需要进行转场动画的视图
  • uoptions:转场动画的类型
  • uanimations:将改变视图属性的代码放在这个block中
  • ucompletion:动画结束后,会自动调用这个block

方式三:

+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion

方法调用完毕后,相当于执行了下面两句代码:

// 添加toView到父视图

[fromView.superview addSubview:toView];

// 把fromView从父视图中移除

[fromView.superview removeFromSuperview];

参数解析:

  • uduration:动画的持续时间
  • uoptions:转场动画的类型
  • uanimations:将改变视图属性的代码放在这个block中
  • ucompletion:动画结束后,会自动调用这个block

图片翻页

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.index++;
    if (self.index == 3) {
        self.index = 0;
    }
    
    NSString *filename = [NSString stringWithFormat:@"%d.jpg", self.index + 1];
    self.iconView.image = [UIImage imageNamed:filename];
    
    [UIView transitionWithView:self.iconView duration:1.0 options:UIViewAnimationOptionTransitionCurlUp animations:nil completion:nil];
}

效果:

 

参考博客:

Core Animation1-简介:http://www.cnblogs.com/mjios/archive/2013/04/15/3021039.html

Core Animation2-CABasicAnimation:http://www.cnblogs.com/mjios/archive/2013/04/15/3021343.html