简体   繁体   中英

UIView animation on completion

I have the following code, which works perfectly fine. What it does is just moves a dynamically created image from one point to another.

    [UIView animateWithDuration:duration
                      delay:0
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{
                     [UIView setAnimationBeginsFromCurrentState:YES];
                     CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
                     image.transform = transform;
                 }
                 completion:^(BOOL finished){

                     if (finished) {
                         NSLog(@"finished animation in true");
                         [arrOfTempImages removeObject:image];
                         image.image = nil;
                         // and do some other stuff here
                     } else {
                         NSLog(@"finished animation in false");
                     }
                 }];

However, sometimes I need to change the viewController and so I pause the animation using this method found in apple's documentation when the view is about to disappear:

-(void)pauseLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = pausedTime;
}

That also works perfectly fine. When I go back to the old VC, I resume my animation using the method :

-(void)resumeLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer timeOffset];
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
layer.beginTime = timeSincePause;
}

Which again works fine.... But the problem is that the code "NSLog(@"finished animation in false"); " is executing when I change the VC and when I go back to the original VC and wait for the image to arrive at the end point, neither this code nor "NSLog(@"finished animation in true");" is executed. That means the image, when it arrives at the end point, doesn't get pointed to nil and remains on screen.

What can I do? I want to perform some code in "completion:" but that part of the code isn't called anymore upon re-entry of the original VC

According to Apple's docs , using [UIView setAnimationBeginsFromCurrentState:YES] is discouraged in iOS 4+.

Instead, try changing your options param from UIViewAnimationOptionCurveEaseOut to UIViewAnimationOptionBeginFromCurrentState and it should call the completion block.

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