1

我有一个 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");

    }

}
4

2 回答 2

0

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

刷下:

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"];

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

于 2012-11-26T16:24:06.590 回答
0

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

于 2012-11-26T13:59:47.060 回答