如何暂停uiview动画?

时间:2014-10-26 23:50:04

标签: ios objective-c animation uiview

如何暂停uiview动画并将其留在那里?

//#define ANIMATION_WORM_OPTIONS (UIViewAnimationOptionCurveLinear|
// UIViewAnimationOptionBeginFromCurrentState)
- (IBAction)pressUp:(id)sender {
    [self.game wormUp];

    [self.gameScene.wormView.layer removeAllAnimations];
    [UIView animateWithDuration:5 delay:0 options:ANIMATION_WORM_OPTIONS
                     animations:^{
                           [self.gameScene.wormView moveUp];
                     } completion:^(BOOL finished) {

                     }];
}
- (IBAction)pressRight:(id)sender {
     [self.game wormRight];
    [UIView animateWithDuration:5 delay:0 options:ANIMATION_WORM_OPTIONS
                     animations:^{
                         [self.gameScene.wormView moveRight];
                     } completion:^(BOOL finished) {

                     }];
}

例如,这就是我要做的事情:

当我第一次点击"右"蠕虫走对了。

然后当我点击" Up"在蠕虫病发生的过程中,蠕虫应该停止并上升。

我的代码中的问题是removeAllAnimations将首先将蠕虫置于第一个动画结束的末尾,然后开始上升。

2 个答案:

答案 0 :(得分:1)

您需要将视图的框架更改为暂停位置框架。只需在view.layer上执行removeAllAnimation之前添加以下内容。

self.theView.frame = [self.theView.layer.presentationLayer frame];

答案 1 :(得分:1)

CFTimeInterval pausedTime = [self.gameScene.wormView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
self.gameScene.wormView.layer.speed = 0.0;
self.gameScene.wormView.layer.timeOffset = pausedTime;