0

I am having some trouble handling animations in some objective C code.

First, here is the relevant code:

BOOL pauseFlag; // Instance variable.
CGFloat animationDuration,pauseDuration; // Instance variables.
......
pauseFlag = NO;
animationDuration = 1.0;
pauseDuration = 1.0;

- (void)animationFunction
{
    [UIView animateWithDuration:animationDuration
                          delay:pauseFlag?pauseDuration:0
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                        ......
                     }
                     completion:^(BOOL finished){
                        ......
                        pauseFlag = Some_New_Value;
                        [self animationFunction];
                     }];
}

Then here is the problem:

The delay supposed to take place when pauseFlag is YES is not happening. Of course, before writing this post I have tried various solutions which came up to my mind, like changing the options, and I also checked that when entering animationFunction pauseFlag had the value YES. But in all cases the delay was ignored.

What did I do wrong? I need to insert a pause in my animation and thought this was the simplest way to do it. Anyone has an idea?

Just for information, beside this issue. This animation code is working fine.

Michel
  • 10,303
  • 17
  • 82
  • 179
  • This is just a suggestion, have you tried to put hardcoded value in delay? – guru Jun 13 '19 at 10:04
  • Yes, I have tested that; and in that case the delay works as expected. But I need it to be present in some cases only, not always. That is my problem. – Michel Jun 13 '19 at 10:13
  • @Michel How do you set value for `pauseFlag`? – Roman Podymov Jun 13 '19 at 10:23
  • pauseFlag=YES; or pauseFlag=NO; in other words, notthing special. – Michel Jun 13 '19 at 10:32
  • The issue should be with pauseFlag = Some_New_Value or delay:pauseFlag?pauseDuration:0 take another bool in animationFunction() for delay and print it, because i tried your code is ok, there may be small thing that you are missing. – guru Jun 13 '19 at 10:37
  • @Michel Or use `pauseFlag == YES?` instead of `pauseFlag?`. I know that it is not recommended, but maybe it will solve the problem. – Roman Podymov Jun 13 '19 at 10:42
  • Well, I have done a simple test when entering animationFunction: if (pauseFlag) NSLog(@"pauseFlag-is-YES"); and was able to see the expected message in the console "pauseFlag-is-YES". As you say there may be a small thing I am missing. – Michel Jun 13 '19 at 10:42
  • @Roman Podymov. OK. I will try but "pauseFlag == YES" and pauseFlag are supposed to be the same (in C). ......... 150 seconds later: I just tried, but there is no difference. – Michel Jun 13 '19 at 10:44
  • @Michel Have you tried the solution described in my answer? – Roman Podymov Jun 14 '19 at 06:45
  • I am trying right now. But there is some issue. The completion requires a different type. A UIViewAnimatingPosition instead of a BOOL.... I am looking into it. – Michel Jun 14 '19 at 07:36
  • I fixed the little completion parameter requirement issue and tried your solution. But unfortunately it does not work, it just behaves exactly like my own original code. Sorry! But thanks anyway for your tip. Let me know if you have some other idea. – Michel Jun 14 '19 at 07:42
  • @Michel I updated my answer. Try to call `pauseAnimation`. – Roman Podymov Jun 17 '19 at 11:14

1 Answers1

1

Try to animate your view with UIViewPropertyAnimator:

UIViewPropertyAnimator* animator = [UIViewPropertyAnimator runningPropertyAnimatorWithDuration:animationDuration
                                                                                         delay:pauseFlag?pauseDuration:0
                                                                                       options:UIViewAnimationOptionBeginFromCurrentState
                                                                                    animations:^{
                                                                                        ......       
                                                                                    }
                                                                                    completion:^(UIViewAnimatingPosition finalPosition) {
                                                                                        ......
                                                                                        pauseFlag = Some_New_Value;
                                                                                        [self animationFunction];                                                 
                                                                                    }];

If you want to pause the animation call pauseAnimation:

[animator pauseAnimation];

To resume it call startAnimation:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [animator startAnimation];
});

All code in this post was tested in Xcode 10.2.1.

Roman Podymov
  • 4,168
  • 4
  • 30
  • 57
  • That sounds interesting, thank you. But this method (pauseAnimation) that you suggest does not seem to have a parameter to allow me to set the length of the pause. This is a big problem. – Michel Jun 18 '19 at 09:16
  • @Michel I updated my answer. Try to call `startAnimation` with a delay. – Roman Podymov Jun 18 '19 at 09:24
  • OK. I also just found another idea on the net that I also would like to try: https://stackoverflow.com/questions/21547241/using-uiview-animatewithduration-delay-xcode – Michel Jun 18 '19 at 09:31
  • 1
    I finally used your idea with dispatch_after(dispatch_time(DISPATCH_TIME_NOW,...., ^{...}); but without using [animator pauseAnimation]; and it works the way I want. Thanks to you! Спасибо. – Michel Jun 20 '19 at 08:27
  • @Michel My pleasure. But how did you replace `[animator startAnimation];`? – Roman Podymov Jun 20 '19 at 08:39
  • 1
    The animation is started by calling a function which in turn starts the animation. I use dispatch_after(dispatch_time(... to make it wait the adequate amount of time before calling this function (in fact this function in fact is calling itself recursively) – Michel Jun 28 '19 at 06:38