Question

I've tried following the advice on a couple of other threads, but their approaches do not seem to work in this instance. Here is my situation:

I have 2 scroll views. "Scroll view A" has a label that fades in and out when scrolling "Scroll View A". When "Scroll View B" is dragged I want to immediately hide the label on "Scroll View A". However I cannot interrupt the label's slow fade out animation. Here are the two methods I'm using:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{

if (scrollView.tag != 1)
{
    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionBeginFromCurrentState animations:^{

        self.liveOverlayLabel.alpha = .6;
    } completion:^(BOOL finished) {
    }];
}

else
{

    [UIView animateWithDuration:0 delay:0 options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionBeginFromCurrentState animations:^{

        self.liveOverlayLabel.alpha = 0;
    } completion:^(BOOL finished) {
        self.liveOverlayLabel.alpha = 0;
    }];
}
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{

if (scrollView.tag != 1)
{

        NSLog(@"stoppping");
    [UIView animateWithDuration:5 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{

        self.liveOverlayLabel.alpha = 0;
    } completion:^(BOOL finished) {
    }];
}
else
{
    self.liveOverlayLabel.alpha = 0;

}

}
Was it helpful?

Solution

UIView animation ends up using CAAnimation objects on the view's layers "under the covers."

To stop an "in-flight" animation, you can use code like this:

Objective-C:

[aView.layer removeAllAnimations];

Swift:

aView.layer.removeAllAnimations()

I believe that for UIView animations, the view will jump to it's end state when the animation is removed.

The other poster's approach of using a new animation with the UIViewAnimationOptionBeginFromCurrentState option would let you add a new animation that began animating a new change to the view using the animation state at the instant the new animation begins as a starting point.

So if you are animating a view from the bottom left to the bottom right of the screen, and halfway through the animation you issue a new UIView animation that moves the view to the top right, using the UIViewAnimationOptionBeginFromCurrentState option, the view will change directions, starting at the halfway point, and begin moving to the upper-right corner.

OTHER TIPS

In my case, the running view animation could not be interrupted by calling

removeAllAnimations

on the view layer.

According to this post though:

How to interrupt UIView animation before completion?

there is another way. You could call:

UIView.animate(withDuration: 0, delay: 0, options: .beginFromCurrentState, 
    animations: {
       // perhaps a "closing" animation
    }, 
    completion: { _ in
      // reset specific view state
    })

The beginFromCurrentState option intercepts currently running animations and replaces them with the one mentioned in the animations block. If duration is 0 this happens instantly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top