UIView animateKeyframes无效

时间:2015-01-27 23:16:29

标签: ios objective-c uiview

我正在尝试关键帧动画,其中视图已淡入,然后转换(平移和缩放),然后在延迟后再次转换,然后淡出。我遇到的问题是第二次转换在第一次转换结束后立即开始,而不是遵守延迟值。这似乎只会在它再次修改变换时发生,所以我怀疑问题来自于尝试在同一个关键帧块中进行两个不同的变换,尽管我不明白为什么不允许这样做因为动画持续时间没有重叠。

以下是代码:

[UIView animateKeyframesWithDuration:15.0 delay:0.0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^
        {
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.1 animations:^
            {
            self.imageViewCurrent.alpha = 1.0;
            }];
        [UIView addKeyframeWithRelativeStartTime:0.1 relativeDuration:0.1 animations:^
            {
            self.imageViewCurrent.transform = self.imageDataCurrent.transform2;
            }];
        [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.4 animations:^
            {
            self.imageViewCurrent.transform = self.imageDataCurrent.transform3;
            }];
        [UIView addKeyframeWithRelativeStartTime:0.9  relativeDuration:0.1 animations:^
            {
            self.imageViewCurrent.alpha = 0.0;
            }];
        }

这些是正在使用的变换:

开始变换:  (a = 5,b = 0,c = 0,d = 5,tx = 950,ty = 850)

transform2:  (a = 5,b = 0,c = 0,d = 5,tx = 1250,ty = -1500)

transform3:  (a = 2.5,b = 0,c = 0,d = 2.5,tx = -250,ty = 0)

更新1:

我没有使用自动布局。

更新2:

我已经完成了一些测试并确定它确实是导致问题的两个转换修改。如果我使用更改为alpha替换其中一个变换修改,则所有动画计时工作正常。在这一点上我尽可能地说,这是UIKit动画的一个错误。

解决方法:

我发现通过直接使用核心动画,我能够避免这个问题。此代码产生大致相同的效果,但没有错误:

// Fade in, pause, fade out.
CAKeyframeAnimation *alphaAnim = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
alphaAnim.keyTimes = @[@(0.0), @(0.1), @(0.9), @(1.0)];
alphaAnim.values = @[@(0.0), @(1.0), @(1.0), @(0.0)];
alphaAnim.duration = 15.0;
[self.imageViewCurrent.layer addAnimation:alphaAnim forKey:@"opacity"];

// Translate, wait on second translation, translate again.
CAKeyframeAnimation *transformAnim = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
transformAnim.keyTimes = @[@(0.3), @(0.4), @(0.5), @(0.8)];
transformAnim.values = @
    [
    [NSValue valueWithCATransform3D:CATransform3DMakeAffineTransform(self.imageDataCurrent.transform1)],
    [NSValue valueWithCATransform3D:CATransform3DMakeAffineTransform(self.imageDataCurrent.transform2)],
    [NSValue valueWithCATransform3D:CATransform3DMakeAffineTransform(self.imageDataCurrent.transform2)],
    [NSValue valueWithCATransform3D:CATransform3DMakeAffineTransform(self.imageDataCurrent.transform3)]
    ];
transformAnim.duration = 15.0;
[self.imageViewCurrent.layer addAnimation:transformAnim forKey:@"transform"];

1 个答案:

答案 0 :(得分:0)

听起来你有一个时间轴,其中有一段动画正在运行并且“休息”。我建议您将休止符输入为具有空动画块的关键帧。

这样的事情:

[UIView animateKeyframesWithDuration:15.0 delay:0.0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^
  {

    [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.1 animations:^
    {
      self.imageViewCurrent.alpha = 1.0;
    }];

    [UIView addKeyframeWithRelativeStartTime:0.1 relativeDuration:0.1 animations:^
    {
      self.imageViewCurrent.transform = self.imageDataCurrent.transform2;
    }];

    //Create an empty keyframe
    [UIView addKeyframeWithRelativeStartTime:0.2 relativeDuration:0.3 animations:^
    {
    }];

    [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.4 animations:^
    {
      self.imageViewCurrent.transform = self.imageDataCurrent.transform3;
    }];

    [UIView addKeyframeWithRelativeStartTime:0.9  relativeDuration:0.1 animations:^
    {
      self.imageViewCurrent.alpha = 0.0;
    }];
  }