iOS动画之-关键帧动画

关键帧动画

关键帧动画主要是使用CAKeyframeAnimation进行完成动画。该动画主要有以下这些常用的属性:
@property(nullable, copy) NSArray *values;
用于添加关键帧的数据,根据keyPath设定的值 ,可以设置成不同的数据,如下所求,将keyPath设置成position时,可以往values中添加不同的CGPoint

CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"position";
    animation.duration = 4;
    animation.repeatCount = CGFLOAT_MAX;
    animation.values = @[[NSValue valueWithCGPoint:CGPointMake(100, 100)],
                         [NSValue valueWithCGPoint:CGPointMake(300, 100)],
                         [NSValue valueWithCGPoint:CGPointMake(300, 400)],
                         [NSValue valueWithCGPoint:CGPointMake(100, 400)],
                         [NSValue valueWithCGPoint:CGPointMake(100, 100)]];
    

@property(nullable) CGPathRef path;
用于设置动画的路径,当设置该属性时,如果同时设置了values,values中的动画帧将失效。实现路径动画主要是要知道如何绘制路径信息,如下所示,是绘制一个正方形的路径:

    //绘制路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 100, 100);
    CGPathAddLineToPoint(path, NULL, 300, 100);
    CGPathAddLineToPoint(path, NULL, 300, 400);
    CGPathAddLineToPoint(path, NULL, 100, 400);
    CGPathAddLineToPoint(path, NULL, 100, 100);
    CGPathRelease(path);

@property(nullable, copy) NSArray *keyTimes;
用于设置每一帧所执行的时间,取值范围为0-1.如下所示:

    animation.keyTimes = @[@(0),@(0.25),@(0.5),@(0.75),@(1)];

@property(nullable, copy) NSArray *timingFunctions;
用于设置每一帧动画的表现形式,如下所示:

animation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
                                  [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],
                                  [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],
                                  [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];

demo
个人博客

你可能感兴趣的:(iOS动画之-关键帧动画)