【问题标题】:How to tell if an animation finished completely, or was interrupted?如何判断动画是完全完成还是被中断?
【发布时间】:2012-11-26 13:01:42
【问题描述】:

我有一个 UIView 和两个 UISwipeGesture 类,它们在 0 到 -99 之间为 X 旋转设置动画,从而产生翻转板效果。如果用户向下滑动然后立即向上滑动,则“向下滑动”动画会提前结束并开始向上滑动动画。

如何判断它是否由于添加了另一个动画而提前结束? animationDidStop:finished 消息被发送,但完成的值始终为 TRUE。

这是向下滑动的代码:

CATransform3D transform = CATransform3DIdentity;

// This must be set before we calculate the transforms to give the 3D perspective (1.0 / -DISTANCE_FROM_CAMERA)
transform.m34 = 1.0 / -4000;
// Rotate on the X axis
transform = CATransform3DRotate(transform, DegreesToRadians(-99), 1, 0, 0);

// Apply transform in an animation
CABasicAnimation* foldDownAnimatnion = [CABasicAnimation animationWithKeyPath:@"transform"];
foldDownAnimatnion.duration = 1;
foldDownAnimatnion.toValue = [NSValue valueWithCATransform3D:transform];
foldDownAnimatnion.removedOnCompletion = NO;
foldDownAnimatnion.fillMode = kCAFillModeForwards;
foldDownAnimatnion.delegate = self;

// Identify this animation in delegate method
[foldDownAnimatnion setValue:@"foldDown" forKey:@"name"];
[foldDownAnimatnion setValue:theLayer forKey:@"layer"];


[theLayer addAnimation:foldDownAnimatnion forKey:nil];

还有我的委托方法:

- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)finished
{
    if([[animation valueForKey:@"name"] isEqualToString:@"foldDown"])
    {
        // Why is this always YES??
        NSLog(@"Animation finished: %@", (finished)?@"Yes" : @"No");

    }
    else if([[animation valueForKey:@"name"] isEqualToString:@"foldUp"])
    {
        NSLog(@"animationDidStop: foldUp");

    }

}

【问题讨论】:

  • 如果您在使用旧密钥添加新动画调用removeAnimationForKey: 时会发生什么情况。你还收到finished = YES吗?
  • 是的,删除动画会导致设置布尔值,finished = NO。这确实有将我的图层重置回其原始位置的副作用,但我认为这一定是要走的路。当我找到解决方案时会回复。谢谢。

标签: ios core-animation cabasicanimation


【解决方案1】:

添加两个 BOOL 实例变量(BOOL UpAnimationStarted; BOOL DownAnimationStarted;) 在 SwipeUp 的开头设置 UpAnimationStarted 为 YES。并且在 SwipeDown 的开头将 DownAnimationStarted 设置为 YES。您可以使用这些 BOOL 值来检查动画中断。

【讨论】:

    【解决方案2】:

    感谢@DavidRönnqvist 为我指明了正确的方向,我回到了我的O'Reilly book on iOS4 并重新制作了动画,现在代码更清晰了:

    向下滑动:

    CATransform3D transform = CATransform3DIdentity;
    
    // This must be set before we calculate the transforms to give the 3D perspective (1.0 / -DISTANCE_FROM_CAMERA)
    transform.m34 = 1.0 / -4000;
    // Rotate on the X axis
    transform = CATransform3DRotate(transform, DegreesToRadians(-99), 1, 0, 0);
    
    // Apply transform in an animation
    [CATransaction setDisableActions:YES];
    theLayer.transform = transform;
    
    CABasicAnimation* foldDownAnimatnion = [CABasicAnimation animationWithKeyPath:@"transform"];
    foldDownAnimatnion.duration = 1;
    foldDownAnimatnion.delegate = self;
    [foldDownAnimatnion setValue:@"foldDown" forKey:@"name"];
    [foldDownAnimatnion setValue:theLayer forKey:@"layer"];
    
    [theLayer addAnimation:foldDownAnimatnion forKey:@"foldDown"];
    

    向上滑动

    [theLayer removeAnimationForKey:@"foldDown"];
    
    CATransform3D transform = CATransform3DIdentity;
    // This must be set before we calculate the transforms to give the 3D perspective (1.0 / -DISTANCE_FROM_CAMERA)
    transform.m34 = 1.0 / -4000;
    // Rotate on the X axis
    transform = CATransform3DRotate(transform, DegreesToRadians(0), 1, 0, 0);
    
    // Apply transform in an animation
    [CATransaction setDisableActions:YES];
    theLayer.transform = transform;
    
    CABasicAnimation* animatnion = [CABasicAnimation animationWithKeyPath:@"transform"];
    animatnion.duration = 1;
    animatnion.delegate = self;
    [animatnion setValue:@"foldUp" forKey:@"name"];
    
    
    [theLayer addAnimation:animatnion forKey:@"foldUp"];
    

    真正的关键是我将新的变换应用于图层,然后对其进行动画处理。我之前所做的是将动画应用到图层并使其向前填充,当我在中途将其移除时会导致问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-02
      • 1970-01-01
      • 2019-07-14
      • 2017-12-12
      • 2011-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多