zoukankan      html  css  js  c++  java
  • iOS核心动画Core Animation(二)

    一、 使用核心动画实现动画效果的步骤
    ■1. 创建动画对象
    ■2. 设置动画属性
    ■3. 把动画对象添加到某个 CALayer 对象上
    ■4. 需要停止动画:可以调用 remove 方法移除动画
    具体步骤
    1.使用它需要先添加QuartzCore.framework框架和引入主头文件<QuartzCore/QuartzCore.h>
    2.初始化一个CAAnimation对象,并设置一些动画相关属性
    3.通过调用CALayer的addAnimation:forKey:方法增加CAAnimation对象到CALayer中,这样就能开始执行动画
    4.通过调用CALayer的removeAnimationForKey:方法可以停止CALayer中的动画
    二、使用Core Animation创建动画实例

    1.基本动画

       //1.创建动画对象 
    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"position.y"];
        // 2.设置一些属性
        basic.fromValue = @(50); 
        basic.toValue = @(400);
        // 3.调整时间 默认是0.25s
        basic.duration = 2;
        // 4 核心动画结束后不要移除
        basic.removedOnCompletion = NO;
        basic.fillMode = kCAFillModeForwards;
        // 5.添加
        // key 标记, 作用: 用来区别不同的核心动画  可以写任意的字符串,也可以写nil.
        [self.redBtn.layer addAnimation:basic forKey:nil];

    2.关键帧动画

    // 1.创建关键帧动画
        CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
        // 2.设置属性
        anim.values = @[@(-M_PI_4 * 0.3), @(M_PI_4  * 0.3), @(-M_PI_4 * 0.3)];
        anim.repeatCount = CGFLOAT_MAX;
        anim.duration = 0.15;
        // 3.添加
        [self.redBtn.layer addAnimation:anim forKey:nil];

     3.转场动画

    @interface HMViewController ()
    //添加控件
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    //图片参数
    @property (nonatomic, assign) int index;
    @end
    @implementation HMViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        _index = 1;
    }
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        _index++;
        if (_index == 4) {
            _index = 1;
        }
        NSString *fileName = [NSString stringWithFormat:@"%d",_index];
        _imageView.image = [UIImage imageNamed:fileName];
        //创建动画对象
        CATransition *anim = [CATransition animation];
        //设置动画属性
        anim.type = @"fromleft";
        anim.subtype = kCATransitionFromLeft;
        anim.startProgress = 0.5; 
        anim.duration = 2;
        // 添加动画到图层  
        [_imageView.layer addAnimation:anim forKey:nil];
    }
    
    @end

    4.动画组

      //添加基本动画
    CABasicAnimation *rotation = [CABasicAnimation animation];
        //设置动画属性
        rotation.keyPath = @"transform.rotation"; 
        rotation.toValue = @M_PI_2;
      //添加基本动画
       CABasicAnimation *position = [CABasicAnimation animation];
            //设置动画属性
        position.keyPath = @"position";
        position.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 250)];
        //添加基本动画
        CABasicAnimation *scale = [CABasicAnimation animation];
         //设置动画属性
        scale.keyPath = @"transform.scale";
        scale.toValue = @0.5;
     //添加组动画对象
        CAAnimationGroup *group = [CAAnimationGroup animation];
        //设置组动画
        group.animations = @[rotation,position,scale]; 
        group.duration = 2; 
        // 取消反弹
        group.removedOnCompletion = NO;
        group.fillMode = kCAFillModeForwards;
         //添加到图层
        [_redView.layer addAnimation:group forKey:nil];
  • 相关阅读:
    Spring MVC — @RequestMapping原理讲解-1
    搭建一个SVN
    WebService远程调用技术
    Linux命令的复习总结学习
    电商-购物车总结
    单点登录系统---SSO
    JAVA的设计模式之观察者模式----结合ActiveMQ消息队列说明
    23种设计模式
    使用netty实现的tcp通讯中如何实现同步返回
    rabbitmq集群安装
  • 原文地址:https://www.cnblogs.com/xiejw/p/5204083.html
Copyright © 2011-2022 走看看