0

I have an animation that is first called once a button is tapped, and once it completes I have the animation run again by calling the method that the animation is in again and passing a "TRUE" value. However, when I stop the animation and call it again when I need to it runs faster and faster even though I have the same code (it gets to a point where it completes the whole loop in less than 1 second, and it should take about 10-20 seconds.) Could anyone explain what I need to do to have the animation simply reset itself after I need it to stop?

Also, after I pass a "FALSE" that should stop the animation, I reset the frame of the label I am moving, but that doesn't seem to show when I run the animation again as sometimes it starts in the middle of the screen when it should begin at the left of the screen.

Here is the code:

-(void)updateLabel:(BOOL)startStop
{
    CGFloat CGRectGetMinY ( CGRect rect );
    CGRect screenBound = [[UIScreen mainScreen] bounds];
    CGSize screenSize = screenBound.size;
    CGFloat screenWidth = screenSize.width;

    if(startStop)
    {  
        [UIView animateWithDuration:1.0f
        animations:^{
            progress.frame= CGRectMake(progress.frame.origin.x+20.0f, progress.frame.origin.y, progress.frame.size.width, progress.frame.size.height);
        } completion:^(BOOL finished) {                
            [self updateLabel:true];
        }];
    } else {
        [self.view.layer removeAllAnimations];
        progress.frame= CGRectMake(0.0f, progress.frame.origin.y, progress.frame.size.width, progress.frame.size.height);
    }
}
Wain
  • 118,658
  • 15
  • 128
  • 151
Lehman2020
  • 637
  • 7
  • 22
  • Why are you removing animations from `self.view` when you are adding them to `progress` – Wain Aug 18 '15 at 16:19
  • See also: http://stackoverflow.com/questions/12101708/cancel-a-uiview-animatewithduration-before-completion – Wain Aug 18 '15 at 16:20

1 Answers1

1

try this... instead of passing 'false' to the method, change it so it checks a global variable to see if it should run or not

//'doTask' is previously defined in your program
//set 'doTask' to false when you want the recursion to stop
//and set 'doTask' back to true before calling this again
-(void)recursiveMethod{

   if(doTask){
       //do something
       [self recursiveMethod];
   }
   else{
       //re-set anything that 
       //needs to be re-set
   }
}
Paul
  • 156
  • 9
  • Explain how that will help – Wain Aug 18 '15 at 16:18
  • OP stated that the recursion gets faster each time he stops and starts it. So i was thinking that because OP is calling the recursive method while it is in the middle of recursion, that its not stopping the first call, just the call where OP pass 'false' (and in this case never completely reseting the progression).... so instead of calling the method in the middle of the recursion, change the var the controls the recursion so that way the recursion stops. – Paul Aug 18 '15 at 16:24
  • haha your'e a genius! It worked. Thanks so much, I've had this issue for a while now and I couldn't figure out what was wrong! – Lehman2020 Aug 19 '15 at 03:49