iOS开发之Core Animation

在IOS中若是使用普通的动画则可使用UIKit提供的动画方式来实现,若是想实现更复杂的效果,则须要使用Core Animation了。数组

在Core Animation中咱们常常使用的是测试

  • CABasicAnimation
  • CAKeyframeAnimation
  • CATransitionAnimation

其中CABasicAnimationCAKeyframeAnimation是对图层中的不一样属性进行动画的。动画

若是要多整个图层进行动画,则应该使用CATransitionAnimationspa

若是要使用组合动画,例如要改变图层的大小和透明度,则能够先为每一个属性建立一个CABasicAnimation对象,再把他们组合到CAAnimationGroup中,最后把这个组合添加到要进行动画的CALayer中。.net

注:CAAnimation(以及CAAnimation的子类),所有都是显式动画,这样动画播放后,表现层回恢复到模型层的原始状态,这就意味着,若是动画播放完后,会恢复到原来的样子,因此在动画播放完后要对模型层进行修改,例如:self.view.layer.backgroundColor=[UIColor blueColor].CGColor;code

一、自定义动画:CABasicAnimation

-(void)animationOfCABasicAnimation
{
    //建立一个CABasicAnimation对象
    CABasicAnimation *animation=[CABasicAnimation animation];
    //设置颜色
    animation.toValue=(id)[UIColor blueColor].CGColor;
    //动画时间
    animation.duration=1;
    //是否反转变为原来的属性值
    animation.autoreverses=YES;
    //把animation添加到图层的layer中,即可以播放动画了。forKey指定要应用此动画的属性
    [self.view.layer addAnimation:animation forKey:@"backgroundColor"];
    
}

二、关键帧动画:CAKeyframeAnimation

 

1. path对象

这是一个 CGPathRef  对象,默认是空的,当咱们建立好CAKeyframeAnimation的实例的时候,能够经过制定一个本身定义的path来让  某一个物体按照这个路径进行动画。这个值默认是nil  当其被设定的时候  values  这个属性就被覆盖 blog

2. valuesip

一个数组,提供了一组关键帧的值,  当使用path的 时候 values的值自动被忽略。开发

下面是改变依次改变view的颜色

-(void)animationOfCAKeyframeAnimation
{
    CAKeyframeAnimation *animation=[CAKeyframeAnimation animation];
    //设置属性值
    animation.values=[NSArray arrayWithObjects:
                      (id)self.view.backgroundColor,
                      (id)[UIColor yellowColor].CGColor,
                      (id)[UIColor greenColor].CGColor,
                      (id)[UIColor blueColor].CGColor,nil];
    animation.duration=3;
    animation.autoreverses=YES;
    //把关键帧添加到layer中
    [self.view.layer addAnimation:animation forKey:@"backgroundColor"];
}

三、使用路径制做动画:CAKeyframeAnimation

-(void)animationOfCAKeyframeAnimationPath
{
    //初始化一个View,用来显示动画
    UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];
    redView.backgroundColor=[UIColor redColor];
    
    [self.view addSubview:redView];
    
    CAKeyframeAnimation *ani=[CAKeyframeAnimation animation];
    //初始化路径
    CGMutablePathRef aPath=CGPathCreateMutable();
    //动画起始点
    CGPathMoveToPoint(aPath, nil, 20, 20);
    CGPathAddCurveToPoint(aPath, nil, 
                          160, 30,//控制点
                          220, 220,//控制点 
                          240, 380);//控制点
    
    ani.path=aPath;
    ani.duration=10;
    //设置为渐出
    ani.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    //自动旋转方向
    ani.rotationMode=@"auto";
    
    [redView.layer addAnimation:ani forKey:@"position"];
}

源码下载:点击下载源码

原文:http://blog.csdn.net/kqjob/article/details/10417461

---文章完---

最后,推荐一个神器。

内测宝

内测宝我的以为比TestFlight更简单好用,开发者只须要简单把打好的ipa包上传上去,生成二维码,测试人员在手机上扫码二维码,就能够直接安装最新的测试版本了,好用的让人想哭。

相关文章
相关标签/搜索