如何判断动画是完全完成还是被中断?

时间:2012-11-26 13:01:43

标签: ios core-animation cabasicanimation

我有一个UIView和两个UISwipeGesture类,可以在0和-99之间设置X旋转的动画,从而产生翻转板效果。如果用户向下滑动然后立即向上滑动,则“向下滑动”动画会过早结束,并且会向上滑动动画。

如何判断是否由于添加了另一个动画而过早结束? animationDidStop:已完成的消息已发送,但已完成的值始终为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");

    }

}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

感谢@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"];

关键是我将新变换应用于图层,然后设置动画。我以前做的是将动画应用到图层并使其向前填充,这在我中途移除时会引起问题。

相关问题