编辑:附加代码CAAnimationGroup
,动画按顺序绘制,但我仍然遇到firstAnimation
一旦开始就消失的问题,secondAnimation
我在文档中注意到CAAnimationGroup
它说:
Animations 属性中动画的 removedOnCompletion 属性当前被忽略。
我该如何解决这个问题?
我正在尝试为CAKeyFrameAnimation
同一图层上的多个对象设置动画,以便firstAnimation
完成后,它绘制的路径在启动时仍保留在屏幕上,secondAnimation
因此最终结果是由屏幕上两个对象的路径共同制作的图片。
目前,如果我将两个动画对象(按顺序)放在同一个方法中并调用它,则只会secondAnimation
在屏幕上绘制。如果我将它们分开并按顺序调用它们,firstAnimation
被绘制到屏幕上,然后在secondAnimation
开始时消失。就其本身而言,动画完全按预期工作。
我已经尝试四处寻找CAAnimationGroup
示例,因为它似乎是我正在寻找的东西,但是从我看到的几个示例中,我不太清楚发生了什么,或者如何产生我正在寻找的效果因为,谁能告诉我如何按顺序绘制两个动画并将结果保留在屏幕上?
这是我的图层、我的两个动画和我的组:
CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.frame = self.animationLayer.bounds;
pathLayer.bounds = pathRect;
pathLayer.geometryFlipped = YES;
pathLayer.strokeColor = [[UIColor blackColor] CGColor];
pathLayer.fillColor = nil;
pathLayer.lineWidth = 10.0f;
pathLayer.lineJoin = kCALineJoinBevel;
CAKeyframeAnimation *firstAnimation = [CAKeyframeAnimation animationWithKeyPath:@"path"];
firstAnimation.beginTime = 0.0;
firstAnimation.duration = 4.0;
firstAnimation.removedOnCompletion = NO; //Is being ignored by CAAnimationGroup
firstAnimation.fillMode = kCAFillModeForwards;
firstAnimation.values = [NSArray arrayWithObjects:
(id)path0.CGPath,(id)path1.CGPath,
(id)path2.CGPath,(id)path3.CGPath,
(id)path4.CGPath,(id)path5.CGPath,nil];
firstAnimation.keyTimes = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:0.2],
[NSNumber numberWithFloat:0.21],
[NSNumber numberWithFloat:0.22],
[NSNumber numberWithFloat:0.63],
[NSNumber numberWithFloat:1.0], nil];
CAKeyframeAnimation *secondAnimation = [CAKeyframeAnimation animationWithKeyPath:@"path"];
secondAnimation.beginTime = 4.0; //This should come directly after firstAnimation
secondAnimation.duration = 3.0;
secondAnimation.removedOnCompletion = NO; //Is being ignored by CAAnimationGroup
secondAnimation.fillMode = kCAFillModeForwards;
secondAnimation.values = [NSArray arrayWithObjects:
(id)path00.CGPath,(id)path01.CGPath,
(id)path02.CGPath,nil];
secondAnimation.keyTimes = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:0.8],
[NSNumber numberWithFloat:1.0],nil];
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = [NSArray arrayWithObjects:firstAnimation,secondAnimation,nil];
group.duration = 7.0; //The total time of the animations, don't know if redundant
group.delegate = self;
[self.pathLayer addAnimation:group forKey:@"path"];