动画详解

最近发现自己对iOS的动画理解很肤浅,于是执行了一段相对复杂的动画,加深自己对CAKeyframeAnimation关键帧动画的理解。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    //执行事件
    CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animation];
    positionAnimation.keyPath = @"position";
    positionAnimation.values = @[[NSValue valueWithCGPoint:CGPointMake(0,0)], [NSValue valueWithCGPoint:CGPointMake(-110, 73)], [NSValue valueWithCGPoint:CGPointMake(-167, 116)], [NSValue valueWithCGPoint:CGPointMake(-189-30, 133+30)]];
    positionAnimation.keyTimes = @[ @0, @(11 / 30.0), @(22 / 30.0), @1];
    positionAnimation.duration = 3.0;
    positionAnimation.removedOnCompletion = NO;
    positionAnimation.additive = YES;

    positionAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];

    positionAnimation.autoreverses = YES;
    positionAnimation.calculationMode = kCAAnimationLinear;

    [_meteroStar.layer addAnimation:positionAnimation forKey:@"PositionAnimation"];

    //    Alpha
    CAKeyframeAnimation *alphaAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
    alphaAnimation.duration = 3.0;
    alphaAnimation.values = @[ @0.0, @1.0, @0.5, @0.4];
    alphaAnimation.keyTimes = @[ @0, @(11 / 30.0), @(22 / 30.0), @1];
    alphaAnimation.autoreverses = NO;
    alphaAnimation.removedOnCompletion = NO;
    [_meteroStar.layer addAnimation:alphaAnimation forKey:@"alphaPositionAnimation"];

    // Transform
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
         CATransform3D scale1 = CATransform3DMakeScale(0.4, 0.4, 1);
         CATransform3D scale2 = CATransform3DMakeScale(1.2, 1.2, 1);
         CATransform3D scale3 = CATransform3DMakeScale(0.9, 0.9, 1);
         CATransform3D scale4 = CATransform3DMakeScale(0.4, 0.4, 1);

    NSArray *frameValues = [NSArray arrayWithObjects:
                                  [NSValue valueWithCATransform3D:scale1],
                                  [NSValue valueWithCATransform3D:scale2],
                                  [NSValue valueWithCATransform3D:scale3],
                                  [NSValue valueWithCATransform3D:scale4],
                                                               nil];
    [animation setValues:frameValues];

    animation.keyTimes = @[ @0, @(11 / 30.0), @(22 / 30.0), @1];
    animation.fillMode = kCAFillModeForwards;
    animation.duration = 3;
    animation.autoreverses = NO;
    animation.removedOnCompletion = NO;
    [self.meteroStar.layer addAnimation:animation forKey:@"PopUpAnimation"];

Comments