0

How do you pause and resume UIView Animations? (without block animations)

I was having the hardest time figuring this out, so here is my source below on how to do it.

Monolo
  • 18,205
  • 17
  • 69
  • 103
John Riselvato
  • 12,854
  • 5
  • 62
  • 89

1 Answers1

2

How to pause and resume UIView Animations:

This will cover how to do the above without block animations. (People do still wish to support 3.0 and up).

This only allows for pausing once the animation has met the set location. For how to pause an animation in the middle of an animation, i suggest using CALayers as such:

CALayer* myLayer = [self.myUIView.layer presentationLayer];
CGRect frameStop = myLayer.frame;
double pausedX = frameStop.origin.x;
double pausedY = frameStop.origin.y;

Now for the actually how to:

startAnimation = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    startAnimation.titleLabel.font = [UIFont systemFontOfSize:22];
    startAnimation.titleLabel.lineBreakMode = UILineBreakModeHeadTruncation;
    [startAnimation setTitle:(@"pause") forState:UIControlStateNormal];
    [startAnimation addTarget:self action:@selector(doAnimation) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:startAnimation];



-(void)doAnimation{
    if (bicyclePad.animationPause == false){
       [restartAnimation setTitle:(@"Resume") forState:UIControlStateNormal];
       [bicyclePad pauseAnimation];
    } else {
       [restartAnimation setTitle:(@"Pause") forState:UIControlStateNormal];
       [bicyclePad resumeAnimation];
    }
}

-(void)resumeAnimation{
    bicyclePad.animationPause = false;
    [restartAnimation setTitle:(@"Resume") forState:UIControlStateNormal];
    objectMotion = [[yourUIView alloc]initWithFrame:CGRectMake((*yourPathPoints)[0].x, (*yourPathPoints)[0].y, 8, 8)];
    [self.view addSubview:objectMotion];
    [self animateBike:nil finished:YES context:nil];


}

-(void)pauseAnimation{
   bicyclePad.animationPause = true;

   [restartAnimation setTitle:(@"Pause") forState:UIControlStateNormal];
   [bicyclePad doAnimation];
}

-(void)animateObject:(NSString*)animationID finished:(BOOL)finished context:(void*)context {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationBeginsFromCurrentState:YES];
    //below suggesting your animation loops
    if (animationPause == true)
       [UIView setAnimationDidStopSelector:@selector(animateStop:finished:context:)];
    else
    [UIView setAnimationDidStopSelector:@selector(animateObject:finished:context:)];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationCurve: UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:yourDelay];
    [UIView commitAnimations];
    animationPause == false;

}

-(void)animateStop:(NSString*)animationID finished:(BOOL)finished context:(void*)context{
}
John Riselvato
  • 12,854
  • 5
  • 62
  • 89
  • `animationPause == false;` and `animationPause == true;` are comparisons and don't do anything. You likely wanted to assign the values instead. – DarkDust May 16 '14 at 14:46
  • I'm afraid you didn't. You replaced one comparison with another comparison. `bicyclePad.animationPause == false;` is still a comparison and the result is thrown away. You want `bicyclePad.animationPause = false;`. Pay attention to `==` vs. `=`. – DarkDust May 17 '14 at 12:38
  • Ah alright. I miss understood your first comment. Yeah this code is about 2 years old. Thanks for pointing that out. @DarkDust – John Riselvato May 19 '14 at 15:42