CAAnimation

来源:互联网 发布:fifa online3数据库cc 编辑:程序博客网 时间:2024/06/05 07:20

#import "ViewController.h"


@interface ViewController ()


@property(nonatomic,strong)UIView *simpleView;



@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    //oc有垃圾回收机制  ios没有垃圾回收机制

    

    _simpleView=[[UIViewalloc]initWithFrame:CGRectMake(100,100, 100, 100)];

    _simpleView.backgroundColor=[UIColormagentaColor];

    [self.viewaddSubview:_simpleView];

    

    

    //设置边框

    _simpleView.layer.borderColor=[UIColoryellowColor].CGColor;

    

}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    

    //关键帧动画

    CAKeyframeAnimation *keyA=[CAKeyframeAnimationanimationWithKeyPath:@"position"];

    //准备路径

    CGPoint p1=CGPointMake(100,100);

    CGPoint p2=CGPointMake(200,200);

    CGPoint p3=CGPointMake(50,120);

    CGPoint p4=CGPointMake(300,20);

    CGPoint p5=CGPointMake(10,20);

    //将结构体转化成对象类型,用于存放到数组中作为关键帧

    NSValue *v1=[NSValuevalueWithCGPoint:p1];

    NSValue *v2=[NSValuevalueWithCGPoint:p2];

    NSValue *v3=[NSValuevalueWithCGPoint:p3];

    NSValue *v4=[NSValuevalueWithCGPoint:p4];

    NSValue *v5=[NSValuevalueWithCGPoint:p5];

    //设定关键帧

    keyA.values=@[v1,v2,v3,v4,v5];

    

    //设置动画间隔

    keyA.duration=4;

    

    //添加动画

//    [self.simpleView.layer addAnimation:keyA forKey:@"key"];

    

    

    

    //设置什么,就将关键字写成下面方法的属性

    

    //创建CABasicAnimation动画并设定变换的属性

    CABasicAnimation *ba=[CABasicAnimationanimationWithKeyPath:@"transform.rotation"];

    

    //设定变化初始值

    ba.fromValue=@0;

    

    //设定变化结束值

    ba.toValue=@(M_PI_4*2);

    

    //设置动画间隔

    ba.duration=4;

    

    //添加动画到视图的layer

    [self.simpleView.layeraddAnimation:ba forKey:@"hehe"];

    

    //因为CA动画结束后会回到起始位置,可以使用延时器将试图固定在目标状态

//    [self performSelector:@selector(makeNewView) withObject:nil afterDelay:4];

    

    

    

    //组装动画

    CAAnimationGroup *group=[CAAnimationGroupanimation];

    

    //设置同时执行的动画的数组

    group.animations=@[keyA,ba];

    group.duration=4;

//    [self.simpleView.layer addAnimation:group forKey:@"group"];

    

    

    

    //CATransaction

    

    

    //@"cube" @"moveIn" @"reveal" @"fade"(default) @"pageCurl" @"pageUnCurl" @"suckEffect" @"rippleEffect" @"oglFlip"

    

    /**

     *  enum AnimationType:Int {

     case Fade = 1,                   //淡入淡出

     Push,                       //推挤

     Reveal,                     //揭开

     MoveIn,                     //覆盖

     Cube,                       //立方体

     SuckEffect,                 //吮吸

     OglFlip,                    //翻转

     RippleEffect,               //波纹

     PageCurl,                   //翻页

     PageUnCurl,                 //反翻页

     CameraIrisHollowOpen,       //开镜头

     CameraIrisHollowClose,      //关镜头

     CurlDown,                   //下翻页

     CurlUp,                     //上翻页

     FlipFromLeft,               //左翻转

     FlipFromRight             //右翻转

     

     }

     */

    

    CATransition *transtion=[CATransitionanimation];

    //设置动画类型

    transtion.type=@"CameraIrisHollowClose";

    //设置动画间隔

    transtion.duration=4;

    //设置动画路径

//    transtion.subtype=kCATransitionFromLeft;

    //添加动画

//    [self.simpleView.layer addAnimation:transtion forKey:@"transtion"];

    

    

    

    

    

    

    

}


-(void)makeNewView{

    _simpleView.transform=CGAffineTransformMakeRotation(M_PI_4);

    

    //移除动画

    [self.simpleView.layerremoveAnimationForKey:@"1"];

}


0 0