2

How to pause uiview animation and just leave it there?

//#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) {

                     }];
}

For example here is what I am trying to do:

When I first tap "Right" a worm goes right.

Then when I tap "Up" during the worm goes right, the worm should stop and go up.

The problem in my code is the removeAllAnimations will first put the worm to the end of first animation end, then begin goes up.

Community
  • 1
  • 1
AlexWei
  • 1,093
  • 2
  • 8
  • 32

2 Answers2

1

You need to change the view's frame to the halting position frame. Just add the following before you do the removeAllAnimation on view.layer.

self.theView.frame = [self.theView.layer.presentationLayer frame];
gabbler
  • 13,626
  • 4
  • 32
  • 44
1
CFTimeInterval pausedTime = [self.gameScene.wormView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
self.gameScene.wormView.layer.speed = 0.0;
self.gameScene.wormView.layer.timeOffset = pausedTime;
Asif Asif
  • 1,511
  • 14
  • 24