简体   繁体   中英

Recursive UIView animation continues after ViewController destroyed, new ViewController created

UPDATE

ViewController is not destroyed, new ViewController is not created. This:

TimerViewController * timerViewController = (TimerViewController *)[segue destinationViewController];

does not create a new instance. However, this

TimerViewController *timerViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"theID"];

does. So a new instance is now being used, but the same problem.

END UPDATE

I have a simple "analog digital timer" where the minutes and seconds animate smoothly to emulate the turning of a number wheel. This is simply a ParentViewController with a list of times, selecting a time performs a segue to the TimerViewController. The TimerViewController uses a recursive UIView animation to simulate the countdown of a clock. This works well.

I want the user to be able to transition to the next timer in the list without having to go back to the ParentViewController in order to select it. This does not work well.

I've tried many variations, the basic pattern is for TimerViewController to ask its delegate (ParentViewController) to pop it and then to seque again to the TimerViewController using the next time from the list of times. The initial animation never terminates, the initial TimerViewController instance never "dies". When I segue to TimerViewController the second time, using what I thought was a new instance, the first animation appears to still be running, the duration of the animation which started at almost 1.0s is off the charts (closer to 0.).

I've tried many different, increasingly desperate, ways to stop the original animation and kill the original instance.

ParentViewController.m

-(void) timerViewControllerDidSwipe  (TimerViewController *)controller {

  [self.navigationController  popViewControllerAnimated:NO];
  controller = nil; // ?
  [self performSegueWithIdentifier:@"ShowTimer" sender:self];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([[segue identifier] isEqualToString:@"ShowTimer"])
{
    TimerViewController * timerViewController = (TimerViewController *)[segue destinationViewController];
    [timerViewController setDelegate:self];
}


TimerViewController.m  

-(void) swipe:(id)selector {
    // swipe left
     stop = YES; // ivar for ^after to not call [self animate]
    [self.delegate timerViewControllerDidSwipe:self];
}

-(void) animate {

// anim block here..

 void (^after) (BOOL) = ^(BOOL f) {

    if (duration % 60 == 0 && duration >= 60) {

         if (minutesAlt.center.y < minutes.center.y)
         {
             CGPoint a = minutes.center;
             a.y -= 2 * displacement;
             minutes.center = a;
             minutes.text = [NSString stringWithFormat:@"%02d", (duration / 60) -1];
         }
         else
         {
            CGPoint a = minutesAlt.center;
            a.y -= 2 * displacement;
            minutesAlt.center = a;
            minutesAlt.text = [NSString stringWithFormat:@"%02d", (duration / 60) -1];
         }
    }

    if (duration == 0)
    {
        // end
    }
    else if (secondsAlt.center.y < seconds.center.y)
    {
        CGPoint a = seconds.center;
        a.y -= 2 * displacement;
        seconds.center = a;
        duration--;
        seconds.text = [NSString stringWithFormat:@"%02d",duration % 60];

        if (!stop) {[self animate];}
    }
    else 
    {
        CGPoint a = secondsAlt.center;
        a.y -= 2 * displacement;
        duration--;
        secondsAlt.center = a;
        secondsAlt.text = [NSString stringWithFormat:@"%02d", duration % 60];

        if (!stop) {[self animate];}
    }
};


[UIView animateWithDuration:0.50
                      delay:0.50
                    options:UIViewAnimationOptionCurveLinear
                 animations:anim
                 completion:after];
}

Any help greatly appreciated.

Fixed here in a related question. It was a simple syntax error (that took me weeks to find).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM