iOS 核心动画 Core Animation

相关资料:

这个理论比较多:http://www.360doc.com/content/15/0727/09/20918780_487655250.shtml

这个实践比较多,常见的效果都有了http://www.cnblogs.com/wengzilin/p/4250957.html

例子:放大效果。思路是让CALayer动,CABasicAnimation是怎么动,然后将动画加到CALayer

    //演员初始化
    CALayer *scaleLayer = [[CALayer alloc] init];
    scaleLayer.backgroundColor = [UIColor blueColor].CGColor;
    scaleLayer.frame = CGRectMake(100, 100, 50, 50);
    scaleLayer.cornerRadius = 10;
    [self.view.layer addSublayer:scaleLayer];
 
    
    //设定剧本
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    scaleAnimation.toValue = [NSNumber numberWithFloat:1.5];
    scaleAnimation.autoreverses = YES;
    scaleAnimation.fillMode = kCAFillModeForwards;
    scaleAnimation.repeatCount = MAXFLOAT;
    scaleAnimation.duration = 0.8;
    
    //开演
    [scaleLayer addAnimation:scaleAnimation forKey:@"scaleAnimation"];

 

思考:这边要新建一个CALayer,但是如果我想让界面上某个UIView动起来(可能是一个按钮、图片,等)

其实所有的视图都继承UIView,每个UIView都有CALayer的属性(见第一个参考资料的描述)

所以,我们可以直接将动画加在view的layer上。这样代码就变得更易懂了。

下面是个视图旋转的例子,么么哒

    UIView *v = [[UIView alloc]initWithFrame:CGRectMake(200, 100, 50, 50)];
    v.backgroundColor = [UIColor redColor];
    [self.view addSubview:v];
    CABasicAnimation *an2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    an2.fromValue = 0;
    an2.toValue = [NSNumber numberWithFloat:M_PI*2];
    an2.repeatCount = MAXFLOAT;
    an2.duration = 0.8;
    [v.layer addAnimation:an2 forKey:@"caan"];

 

posted @ 2016-07-01 21:31  知吾猪  阅读(226)  评论(0编辑  收藏  举报