Ios 链接关键帧动画

Ios 链接关键帧动画,ios,core-animation,cakeyframeanimation,Ios,Core Animation,Cakeyframeanimation,我正在尝试链接两个基于关键帧的动画,但第二个动画由于某些原因无法播放。知道发生了什么吗 // Create an animation group to contain all album art animations CAAnimationGroup *albumArtAnimationGroup = [CAAnimationGroup animation]; albumArtAnimationGroup.duration = 3.0; albumArtAnimation

我正在尝试链接两个基于关键帧的动画,但第二个动画由于某些原因无法播放。知道发生了什么吗

// 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类定义convertTime:fromLayer:和 convertTime:toLayer:methods。您可以使用这些方法来转换 固定时间值为图层的本地时间或转换时间 从一个图层到另一个图层的值。这些方法考虑了 可能影响层本地时间的媒体定时属性 并返回一个可用于另一层的值。清单5-3 显示了一个示例,您应该定期使用该示例来获取当前 层的本地时间。CACurrentMediaTime函数是一个 返回计算机当前时钟时间的便利功能, 该方法获取并转换为图层的本地时间

清单5-3获取图层的当前本地时间

CFTimeInterval localLayerTime = [myLayer convertTime:CACurrentMediaTime() fromLayer:nil];

将多个动画添加到动画组时,beginTime属性从0开始,在动画持续时间结束。因此,若要链接第二个动画,请将其“开始时间”设置为第一个动画的持续时间,并使动画组的持续时间足够长,可用于整个动画序列


顺便说一句,创建一个CAKeyframeAnimation,将两条路径组合成一条路径可能更简单。这将大大减少代码。

我以前使用animateKeyFramesWithDuration,但我需要更多的灵活性。关键帧动画仍在核心动画编程指南中。有什么见解吗?是否可以使用animateWithDuration链接动画并重复该序列?@Fogmeister,胡说八道。CAKeyframeAnimation仍然完全受支持。它比UIView动画更难使用,但它是沿显式CGPath设置对象动画的唯一方法。有很多复杂的动画仍然需要
cakeyLeanimation
s、
CAAnimationGroup
s、
CABasicAnimation
s等等@duncac抱歉,你说得对。我想的是错误的动画类型。@ratsimihah,区别在于动画组。对于动画组中的动画,“beginTime”属性具有不同的含义。它不是基于currentMediaTime或当前层的时间指定beginTime,而是基于动画组的持续时间。谢谢。我需要在两个动画之间暂停。用一个两条路径的关键帧动画可以实现吗?我找到了一种方法来修复它,这与你的建议是一致的。如果您查看我的编辑,我可能仍然会遇到计时问题。@ratsimihah,不,我不认为如果动画都有相同的路径,您可以在动画之间创建暂停-除非您向同一点添加大量对lineToPoint的调用,并且不使用配速计时。但那将是一次黑客攻击。在这种情况下,最好使用动画组。@ratsimihah,您的计时代码仍然错误。您希望动画组中的第一个动画的起始时间为0,第二个动画的起始时间为上一个动画的持续时间加上所需的暂停时间。当动画是组的一部分时,不应将beginTime基于本地层时间。在这种情况下,开始时间是从零开始的。哈!后一句话是我挣扎的原因。我相信如果我没有为第一个动画设置beginTime,它会隐式地为0,不是吗?