zoukankan      html  css  js  c++  java
  • iOS CoreAnimation( 核心动画 )

    CABasicAnimation  基础动画

    #define  radin(angle)   (M_PI*(angle)/180) //定义自己需要的弧度
    @interface ViewController () @property(nonatomic,strong)CALayer *layer; @property (strong, nonatomic) IBOutlet UIView *readView; @property (strong, nonatomic) IBOutlet UIImageView *iconView; - (IBAction)begin:(id)sender; - (IBAction)stop:(id)sender;

    //创建CALayer

     CALayer *layer =[CALayer layer];
        layer.position=CGPointMake(100, 100);
        layer.bounds=CGRectMake(0, 0, 100, 100);
        layer.backgroundColor =[UIColor redColor].CGColor;
      
        
        [self.view.layer addSublayer:layer];
        self.layer=layer;
    

     CABasicAnimation  

    //旋转

     //1.创建动画对象
        CABasicAnimation *anim =[CABasicAnimation animation];
        
        //2.设置动画对象
        //keyPath决定了执行怎样的动画,调整那个属性来执行动画
        anim.keyPath=@"transform";
        
        //
        anim.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 1, -1, 0)];
        //动画执行的时间
        anim.duration=2.0;
        
        /**让图层保持动画执行完毕后的状态**/
        //动画执行完毕后不要删除动画
        anim.removedOnCompletion=NO;
        //保持最新的状态
        anim.fillMode= kCAFillModeForwards;
        
        //3.添加动画
        [self.layer addAnimation:anim forKey:nil];
    

     缩放

    //1.创建动画对象
        CABasicAnimation *anim =[CABasicAnimation animation];
        
        //2.设置动画对象
        //keyPath决定了执行怎样的动画,调整那个属性来执行动画
        anim.keyPath=@"bounds";
        
        anim.toValue=[NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
        //动画执行的时间
        anim.duration=2.0;
        
        /**让图层保持动画执行完毕后的状态**/
        //动画执行完毕后不要删除动画
        anim.removedOnCompletion=NO;
        //保持最新的状态
        anim.fillMode= kCAFillModeForwards;
        
        //3.添加动画
        [self.layer addAnimation:anim forKey:nil];
    

     //平移

     
        //1.创建动画对象
        CABasicAnimation *anim =[CABasicAnimation animation];
        
        //2.设置动画对象
        //keyPath决定了执行怎样的动画,调整那个属性来执行动画
        anim.keyPath=@"position";
        
        anim.fromValue=[NSValue valueWithCGPoint:CGPointMake(0, 0)];
        anim.toValue=[NSValue valueWithCGPoint:CGPointMake(200, 300)];
        //动画执行的时间
        anim.duration=2.0;
        
        /**让图层保持动画执行完毕后的状态**/
        //动画执行完毕后不要删除动画
        anim.removedOnCompletion=NO;
        //保持最新的状态
        anim.fillMode= kCAFillModeForwards;
        
        //3.添加动画
        [self.layer addAnimation:anim forKey:nil];
    

     CAKeyframeAnimation :关键帧动画

    //平移

     //第一种方式
        CAKeyframeAnimation * anim=[CAKeyframeAnimation animation];
        
        anim.keyPath =@"position";
        NSValue * v1 =[NSValue valueWithCGPoint:CGPointZero];
        NSValue * v2 =[NSValue valueWithCGPoint:CGPointMake(100, 0)];
        NSValue * v3 =[NSValue valueWithCGPoint:CGPointMake(100, 200)];
        NSValue * v4 =[NSValue valueWithCGPoint:CGPointMake(300, 200)];
        
        anim.values=@[v1,v2,v3,v4];
        anim.duration=2.0;
        
        anim.removedOnCompletion=NO;
        
        anim.fillMode=kCAFillModeForwards;
        
        
        [self.readView.layer addAnimation:anim forKey:nil];
        
        //第二种方式
        CAKeyframeAnimation * anim1=[CAKeyframeAnimation animation];
        
        anim1.keyPath =@"position";
        anim1.duration=2.0;
        
        anim1.removedOnCompletion=NO;
        
        anim1.fillMode=kCAFillModeForwards;
        
        //创建路径
        CGMutablePathRef path=CGPathCreateMutable();
        
        //给路径添加一个圆
        CGPathAddEllipseInRect(path, NULL, CGRectMake(100, 100, 200, 200));
        anim.path= path;
        //释放
        CGPathRelease(path);
        
        //设置动画的执行节奏 (匀速,先快后慢等)
        //kCAMediaTimingFunctionEaseOut :一开始比较慢,中间会加速,临近结束的时候会变慢
        anim.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
        
        
        //监听动画 后面会有两个代理方法
        anim.delegate=self;
        
        
        [self.readView.layer addAnimation:anim1 forKey:nil];
    
    #pragma mark-动画代理方法
    #pragma mark 动画开始的时候调用
    -(void)animationDidStart:(CAAnimation *)anim{
        
        
    }
    
    #pragma mark 动画结束的时候调用
    - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
        
        
    }
    

    //抖动

    //开始抖动
    - (IBAction)begin:(id)sender {
        
        //创建
        CAKeyframeAnimation *anim =[CAKeyframeAnimation animation];
        
        anim.keyPath =@"transform.rotation";
        
        anim.values=@[@(-radin(4)),@(radin(4)),@(-radin(4))];
        anim.duration=0.15;
        
        
        //重复次数
        anim.repeatCount=MAXFLOAT;
        
        anim.removedOnCompletion=NO;
        anim.fillMode =kCAFillModeForwards;
        [self.iconView.layer addAnimation:anim forKey:@"shake"];
        
        
    }
    
    //停止抖动
    - (IBAction)stop:(id)sender {
        
        [self.iconView.layer removeAnimationForKey:@"shake"];
        
        
        
    }
    
  • 相关阅读:
    C# RabbitMQ
    使用HttpClient和WebRequest时POST一个对象的写法
    HTTP中application/x-www-form-urlencoded字符说明
    MVC5 Entity Framework学习
    SQL Server安全
    Entity Framework查询
    COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问。最新解决方案
    js中精度问题以及解决方案
    string.format
    t-sql对被除数为0&除数小于被除数结果为0&除法保留2位小数的处理
  • 原文地址:https://www.cnblogs.com/wangbinbin/p/4795505.html
Copyright © 2011-2022 走看看