2

I am using [UIView animateWithDuration... ] in order to display a text for each page of my app. Each page got its own text. I'm swiping to navigate between pages. I am using a 1 second dissolve effect to have the text fading in after the page is displayed.

Here's the problem: If I'm swiping during that 1 second (during which the text is fading in) the animation will complete when the next page appears and 2 texts will overlap (the previous and the current).

The solution I'd like to implement is to interrupt the animation if I happen to swipe during its occurrence. I just cannot make it happen. [self.view.layer removeAllAnimations]; does not work for me.

Here's my animation code:

   - (void) replaceContent: (UITextView *) theCurrentContent withContent: (UITextView *) theReplacementContent {

    theReplacementContent.alpha = 0.0;
    [self.view addSubview: theReplacementContent];


    theReplacementContent.alpha = 0.0;

    [UITextView animateWithDuration: 1.0
                              delay: 0.0
                            options: UIViewAnimationOptionTransitionCrossDissolve
                         animations: ^{
                             theCurrentContent.alpha = 0.0;
                             theReplacementContent.alpha = 1.0;
                         }
                         completion: ^(BOOL finished){
                             [theCurrentContent removeFromSuperview];
                             self.currentContent = theReplacementContent;
                             [self.view bringSubviewToFront:theReplacementContent];
                         }];

   }

Do you guys know how to make this work? Do you know any other way to address this issue?

3
  • possible duplicate of Cancel a UIView animation?
    – matt
    Mar 29, 2013 at 0:21
  • Did you try sending removeAllAnimations to theCurrentContent.layer and theReplacementContent.layer?
    – rob mayoff
    Mar 29, 2013 at 1:00
  • @matt, it's a little bit different though
    – Armand
    Mar 30, 2013 at 16:09

3 Answers 3

11

You can not directly cancel animations created via +animateWithDuration.... What you want to do is replace the running animation with an instant new one.

You could write the following method, that get's called when you want to show the next page:

- (void)showNextPage
{
    //skip the running animation, if the animation is already finished, it does nothing
    [UIView animateWithDuration: 0.0
                          delay: 0.0
                        options: UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionBeginFromCurrentState
                     animations: ^{
                         theCurrentContent.alpha = 1.0;
                         theReplacementContent.alpha = 0.0;
                     }
                     completion: ^(BOOL finished){
                         theReplacementContent = ... // set the view for you next page
                         [self replaceContent:theCurrentContent withContent:theReplacementContent];
                     }];
}

Notice the additional UIViewAnimationOptionBeginFromCurrentState passed to options:. What this does is, it basically tells the framework to intercept any running animations for the affected properties and replace them with this one. By setting the duration: to 0.0 the new values are set instantly.

In the completion: block you can then create and set your new content and call your replaceContent:withContent: method.

1
  • Still working on it. It does not quite fix the issue yet but I suspect I might be doing something wrong adapting your code to mine.
    – Armand
    Mar 30, 2013 at 16:35
2

So, another possible solution would be to just disable interaction during the animation.

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

[[UIApplication sharedApplication] endIgnoringInteractionEvents];
0

I would declare a flag like shouldAllowContentToBeReplaced. Set it to false when the animation starts and back true when it's complete. Then say if (shouldAllowContentToBeReplaced) { before you start the animation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.