zoukankan      html  css  js  c++  java
  • Core Animation中的组动画

    实际开发中一个物体的运动往往是复合运动,单一属性的运动情况比较少,但恰恰属性动画每次进行动画设置时一次只能设置一个属性进行动画控制(不管是 基础动画还是关键帧动画都是如此),这样一来要做一个复合运动的动画就必须创建多个属性动画进行组合。对于一两种动画的组合或许处理起来还比较容易,但是 对于更多动画的组合控制往往会变得很麻烦,动画组的产生就是基于这样一种情况而产生的。动画组是一系列动画的组合,凡是添加到动画组中的动画都受控于动画 组,这样一来各类动画公共的行为就可以统一进行控制而不必单独设置,而且放到动画组中的各个动画可以并发执行,共同构建出复杂的动画效果。

    动画组使用起来并不复杂,首先单独创建单个动画(可以是基础动画也可以是关键帧动画),然后将基础动画添加到动画组,最后将动画组添加到图层即可。

    //
    //  ViewController.m
    //  Demo02183
    //
    //  Created by wiseman on 16/2/18.
    //  Copyright (c) 2016年 wiseman. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    {
        CALayer *_layer;
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        //自定义一个图层
        _layer=[[CALayer alloc]init];
        _layer.bounds=CGRectMake(0, 0, 10, 20);
        _layer.position=CGPointMake(50, 150);
        _layer.backgroundColor = [UIColor redColor].CGColor;
        [self.view.layer addSublayer:_layer];
        
    }
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        [self groupAnimation];
    }
    
    #pragma mark - 组动画
    -(void)groupAnimation{
        //1.创建动画组
        CAAnimationGroup *animationGroup=[CAAnimationGroup animation];
        
        //2.设置组中的动画和其他属性
        CABasicAnimation *basicAnimation=[self rotationAnimation];
        CAKeyframeAnimation *keyframeAnimation=[self translationAnimation];
        animationGroup.animations=@[basicAnimation,keyframeAnimation];
        
        animationGroup.delegate=self;
        animationGroup.duration=10.0;//设置动画时间,如果动画组中动画已经设置过动画属性则不再生效
        animationGroup.beginTime=CACurrentMediaTime();//延迟五秒执行
        
        //3.给图层添加动画
        [_layer addAnimation:animationGroup forKey:nil];
    }
    
    #pragma mark - 基础旋转动画
    -(CABasicAnimation *)rotationAnimation{
        //1.创建基本动画
        CABasicAnimation *basicAnima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        
        //2.
        CGFloat toValue = M_PI_2 * 3;
        basicAnima.toValue = [NSNumber numberWithFloat:toValue];
        basicAnima.autoreverses = true;
        basicAnima.repeatCount = HUGE_VALF;
        basicAnima.removedOnCompletion = YES;
        
        return basicAnima;
    }
    
    #pragma mark - 关键帧移动动画
    -(CAKeyframeAnimation *)translationAnimation{
        CAKeyframeAnimation *keyframeAnima = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        UIBezierPath *bezierPath = [UIBezierPath bezierPath];
        [bezierPath moveToPoint:CGPointMake(100, 100)];
        [bezierPath addCurveToPoint:CGPointMake(100, 400) controlPoint1:CGPointMake(0, 250) controlPoint2:CGPointMake(200, 250)];
        
        keyframeAnima.path = bezierPath.CGPath;
        
        return keyframeAnima;
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
  • 相关阅读:
    链表_单链表(插入删除查询)
    OceanBase架构浅析(一)
    电商商品搜索现状
    ASP.NET MVC 源码分析(二) —— 从 IRouteBuilder认识路由构建
    ASP.NET MVC 源码分析(一)
    RPC框架分析
    RPC简介
    Performance Metrics(性能指标2)
    Performance Metrics(性能指标1)
    Introduction(本书简介)
  • 原文地址:https://www.cnblogs.com/iOSDeng/p/5199840.html
Copyright © 2011-2022 走看看