动画

1.UIVIew  

1.1 动画块(改变背景颜色和移动)

static BOOL flag=YES;

    //开始动画

    [UIView beginAnimations:nil context:nil];

    //运动的时间

    [UIView setAnimationDuration:2.f];

    //延时启动

    [UIView setAnimationDelay:2.f];

    //速度曲线

    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

    //代理

    [UIView setAnimationDelegate:self];

    //开始时候执行

    [UIView setAnimationWillStartSelector:@selector(startAction)];

    //结束时候执行

   // [UIView setAnimationDidStopSelector:@selector(stopAction)];

    

    //重复执行

    [UIView setAnimationRepeatAutoreverses:YES];

    //重复执行次数

    [UIView setAnimationRepeatCount:5];

    

    //动画过程(换背景色)

    self.myView.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0  blue:arc4random()%256/255.0  alpha:1];

    

    //改位置

    CGRect myFrame=self.myView.frame;

    if (flag) {

        //向下移动

         myFrame.origin.y+=100;

        myFrame.size.height+=150;

        flag=NO;

    }

    else

    {

        myFrame.origin.y-=100;

        myFrame.size.height-=150;

        flag=YES;

    }

   

    self.myView.frame=myFrame;

    self.myView.alpha=0.5;

    

    //提交动画

    [UIView commitAnimations];

************


 

1.2  block块

//自己的Block

    [self myAnimationDuration:2.f animaton:^{

        self.myView.backgroundColor=[UIColor grayColor];

    }];

    //系统提供的Block

    [UIView animateWithDuration:2.f animations:^{

        self.myView.backgroundColor=[UIColor greenColor];

    }];

    //第一个Block块执行动画

    //第二个Block块是在执行完动画后执行

    [UIView animateWithDuration:2.f animations:^{

        self.myView.backgroundColor=[UIColor greenColor];

    } completion:^(BOOL finished) {

        self.myView.backgroundColor=[UIColor redColor];

    }];

//自己定义的动画Block块方法

-(void)myAnimationDuration:(NSTimeInterval )time animaton:(animation)ani

{

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:time];

    ani();

    [UIView commitAnimations];

}

***********


 

 1.3UIViewYransition

    //第一个方法

    [UIView transitionWithView:self.myView duration:2.f options:(UIViewAnimationOptionTransitionFlipFromBottom) animations:^{

        NSLog(@"动画开始了");

    } completion:^(BOOL finished) {

        NSLog(@"动画结束了");

    }];

    //第二个方法

    [UIView transitionWithView:<#(UIView *)#> duration:<#(NSTimeInterval)#> options:<#(UIViewAnimationOptions)#> animations:<#^(void)animations#> completion:<#^(BOOL finished)completion#>]

 

 


 

2.CGAffineTransfrom

    [UIView animateWithDuration:2.f animations:^{

        //缩小(基于前一次变化)

       self.myView.transform=CGAffineTransformScale(self.myView.transform, 0.2f, 0.2f);

        //旋转(基于前一次变化)

        self.myView.transform=CGAffineTransformRotate(self.myView.transform, M_PI);

               }];


 

3.CALayer 的属性

 //设置圆角

    self.myView.layer.cornerRadius=50;

    //边框颜色

    self.myView.layer.borderWidth=20;

    self.myView.layer.borderColor=[UIColor redColor].CGColor;

    //阴影颜色

    self.myView.layer.shadowColor=[UIColor grayColor].CGColor;

    //阴影偏移

    self.myView.layer.shadowOffset=CGSizeMake(-10, 10);

    //透明度必须设置

    self.myView.layer.shadowOpacity=0.3;

    //模糊程度

    self.myView.layer.shadowRadius=4;

    //设置锚点

    self.myView.layer.anchorPoint=CGPointMake(.5, .5);

*****************************


********************

4.CAPropertyAnimation (两个子类CABasicAnimation和CAKeyframeAnimation)

4.1.CABasicAnimation

//CALayer    

    //UIView的属性动画,会修改属性产生动画.

    //CALayer,没有修改UIView属性

    //创建animation对象

    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"position"];

    //从那儿开始

    animation.fromValue=[NSValue valueWithCGPoint:self.myView.layer.position];

    //到哪儿结束

    animation.toValue=[NSValue valueWithCGPoint:CGPointMake(200, 200)];

    //设置时间

    animation.duration=2.0;

    //自动执行

    animation.autoreverses=YES;

    //执行次数

    animation.repeatCount=3;

     //添加到self.view.layer

    [self.myView.layer addAnimation:animation forKey:@"position动画"];

 

//缩放动画

    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.scale"];

    //初始值

    animation.fromValue=[NSNumber numberWithFloat:1.0];

   //结束值

    animation.toValue=[NSNumber numberWithFloat:3.0];

    //设置时间

    animation.duration=0.1;

    ////自动执行

    animation.autoreverses=YES;

     //执行次数

    animation.repeatCount=100;

    //添加到self.view.layer

    [self.myView.layer addAnimation:animation forKey:@"缩放动画"];


 

4.2CAKeyframeAnimation

//指定位置移动

CAKeyframeAnimation *animation=[CAKeyframeAnimation animationWithKeyPath:@"position"];

    //持续时间

    animation.duration=2.0;

    //自动

    animation.autoreverses=YES;

    //此数

    animation.repeatCount=30;

    NSArray *array=@[[NSValue valueWithCGPoint:CGPointMake(50, 50)],[NSValue valueWithCGPoint:CGPointMake(200, 25)],[NSValue valueWithCGPoint:CGPointMake(25, 200)],[NSValue valueWithCGPoint:CGPointMake(200, 200)]];

        animation.values=array;

    //添加到self.view.layer

     [self.myView.layer addAnimation:animation forKey:@"动画"];

*****************************************


 

5.CAAnimationGroup

    CAAnimationGroup *group=[CAAnimationGroup animation];

    

    //第一个动画

    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.scale"];

    animation.fromValue=[NSNumber numberWithFloat:1.0];

    animation.toValue=[NSNumber numberWithFloat:3.0];

    //第二个动画

    CAKeyframeAnimation *animation1=[CAKeyframeAnimation animationWithKeyPath:@"position"];

 

    NSArray *arr=@[[NSValue valueWithCGPoint:CGPointMake(50, 50)],[NSValue valueWithCGPoint:CGPointMake(200, 50)],[NSValue valueWithCGPoint:CGPointMake(50, 200)],[NSValue valueWithCGPoint:CGPointMake(200, 200)]];

   animation1.values=arr;

   //添加到动画数组

    group.animations=@[animation,animation1];

    group.duration=1;

    group.autoreverses=YES;

    group.repeatCount=500;

    

    //数组添加到view的layer

    [self.myView.layer addAnimation:group forKey:@"组合"];


 

6.CATransition

 /*

    pageCurl            向上翻一页 

     pageUnCurl          向下翻一页 

     rippleEffect        滴水效果 

     suckEffect          收缩效果,如一块布被抽走 

     cube                立方体效果 

     oglFlip             上下翻转效果

    */

    

    CATransition *ani=[CATransition animation];

    ani.type=@"cube";

    ani.subtype=kCATransitionFromLeft;

    ani.duration=2;

    ani.repeatCount=30;

    [self.myView.layer addAnimation:ani forKey:@"动画"];

    


 

UIAnimation包括三个子类:(CAnimationGroup. CAPropertyAnimation(两个子类CABasicAnimation,CAkeyFraneAnimation).CATransition)

 

 

 


 

原文地址:https://www.cnblogs.com/haiying/p/4125303.html