核心动画之CAKeyframeAnimation

1.关键帧动画,也是CAPropertyAnimation的子类,与CABasicAnimation的区别是

CABasicAnimation只能从一个数值(fromValue)变到另一个数值(toValue),而 CAKeyframeAnimation会使用一个NSArray保存这些数值 

2.属性说明:

values:上述的NSArray对象。里面的元素称为“关键帧”(keyframe)。动画对象会在指定 的时间(duration)内,依次显示values数组中的每一个关键帧

path:可以设置一个CGPathRef、CGMutablePathRef,让图层按照路径轨迹移动。path 只对CALayer的anchorPoint和position起作用。如果设置了path,那么values将被忽略

keyTimes:可以为对应的关键帧指定对应的时间点,其取值范围为0到1.0,keyTimes中的 每一个时间值都对应values中的每一帧。如果没有设置keyTimes,各个关键帧的时间是平分的

CABasicAnimation可看做是只有2个关键帧的CAKeyframeAnimation 

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

 

    [self moveAction];

    

}

 

-(void)moveAction{

    

    //1 创建动画对象

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

   

    //2 设置

    animation.duration = 3;

    

    /*

    //关键帧

    CGPoint p0 = self.myImageVIew.center;

    NSValue *v0 = [NSValue valueWithCGPoint:p0];

    

    CGPoint p1 = CGPointMake(300, 100);

    NSValue *v1 = [NSValue valueWithCGPoint:p1];

    

    CGPoint p2 = CGPointMake(200, 400);

    NSValue *v2 = [NSValue valueWithCGPoint:p2];

    

    animation.values = @[v0,v1,v2,v0];

     

     */

    

    //path:沿着路径移动

    CGMutablePathRef path = CGPathCreateMutable();

    

    CGPoint startP = self.myImageVIew.center;

    CGPathMoveToPoint(path, NULL, startP.x, startP.y);

    

    CGPoint cp = CGPointMake(200, 0);

    CGPoint cp1 = CGPointMake(200, 400);

    CGPoint cp2 = CGPointMake(300, 500);

    

    //绘制贝塞尔曲线

    CGPathAddCurveToPoint(path, NULL, cp.x, cp.y, cp1.x, cp1.y, cp2.x, cp2.y);

    

    //设置动画的执行路径

    animation.path = path;

    

    //3 添加

    [self.myImageVIew.layer addAnimation:animation forKey:@"move"];

    

    //注意:release

    CGPathRelease(path);

  

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

posted @ 2016-08-15 20:20  Xcode点点  阅读(150)  评论(0编辑  收藏  举报