我试图链接两个基于关键帧的动画,但由于某些原因,第二个动画不会播放。知道怎么回事吗?
// Create an animation group to contain all album art animations
CAAnimationGroup *albumArtAnimationGroup = [CAAnimationGroup animation];
albumArtAnimationGroup.duration = 3.0;
albumArtAnimationGroup.repeatCount = 0;
// First album art translation animation
CGMutablePathRef albumCoverPath = CGPathCreateMutable();
CGPathMoveToPoint(albumCoverPath, NULL,
albumCover.layer.position.x,
albumCover.layer.position.y);
CGPathAddLineToPoint(albumCoverPath, NULL,
albumCover.layer.position.x-[UIScreen mainScreen].bounds.size.height,
albumCover.layer.position.y);
CAKeyframeAnimation *albumCoverTranslationAnimation;
albumCoverTranslationAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
albumCoverTranslationAnimation.calculationMode = kCAAnimationLinear;
albumCoverTranslationAnimation.path = albumCoverPath;
albumCoverTranslationAnimation.duration = 1.0;
// Second album art translation animation
CGMutablePathRef albumCoverPath1 = CGPathCreateMutable();
CGPathMoveToPoint(albumCoverPath1, NULL,
albumCover.layer.position.x+[UIScreen mainScreen].bounds.size.height,
albumCover.layer.position.y);
CGPathAddLineToPoint(albumCoverPath1, NULL,
albumCover.layer.position.x,
albumCover.layer.position.y);
CAKeyframeAnimation *albumCoverTranslationAnimation1;
albumCoverTranslationAnimation1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
albumCoverTranslationAnimation1.calculationMode = kCAAnimationLinear;
albumCoverTranslationAnimation1.path = albumCoverPath1;
albumCoverTranslationAnimation1.duration = 1.0;
CFTimeInterval localAlbumLayerTime = [albumCover.layer convertTime:CACurrentMediaTime() fromLayer:nil];
albumCoverTranslationAnimation1.beginTime = localAlbumLayerTime + 1.0;
albumArtAnimationGroup.animations = @[albumCoverTranslationAnimation, albumCoverTranslationAnimation1];
[albumCover.layer addAnimation:albumArtAnimationGroup forKey:@"position"];编辑
解决了。事实证明,要么是苹果的文档误导了我,要么是我错误地使用了CACurrentMediaTime。下面的代码起了作用。
albumArtAnimationGroup.duration = 2.0;
albumCoverTranslationAnimation.duration = 1.0;
albumCoverTranslationAnimation1.beginTime = 1;
albumArtAnimationGroup.animations = @[albumCoverTranslationAnimation, albumCoverTranslationAnimation1];
[albumCover.layer addAnimation:albumArtAnimationGroup forKey:@"position"];然而,根据苹果的说法,由于我没有使用CACurrentMediaTime(),我可能会遇到关于时间安排的问题,如下所示。
为了帮助您确保时间值适合于给定的层,CALayer类定义了转换时间:从层:和转换时间:toLayer:方法。可以使用这些方法将固定时间值转换为层的本地时间,或者将时间值从一个层转换到另一个层。这些方法考虑到可能影响层的本地时间的媒体定时属性,并返回可以与另一层一起使用的值。清单5-3显示了一个应该经常使用的示例,用于获取一个层的当前本地时间。CACurrentMediaTime函数是一个方便的函数,它返回计算机当前的时钟时间,该方法获取并转换为该层的本地时间。
清单5-3获取一个层的当前本地时间
CFTimeInterval localLayerTime = [myLayer convertTime:CACurrentMediaTime() fromLayer:nil];发布于 2014-03-20 00:07:11
向动画组添加多个动画时,beginTime属性从0开始,在动画持续时间结束。因此,要链接第二个动画,将它的beginTime设置为第一个动画的持续时间,并使动画组的持续时间足够长到整个动画序列。
顺便说一句,创建一个将两个路径组合在一起的单一CAKeyframeAnimation可能更简单。代码就少多了。
https://stackoverflow.com/questions/22518635
复制相似问题