How to create complex animations with better performance in iOS?

All animations mean changing properties layer.transform

or frame

. There are two main questions about performance:

  • I have several views that I have to animate at the same time. Is it better to create multiple blocks animateWithDuration

    or create one such block and iterate over it in the view?

  • My animation requires complex calculations and I need to call frequently animateWithDuration

    . Do I have to do all the calculations before the animation block or is there any performance difference?

+3


source to share


1 answer


No one has answered yet. So let me answer my own question and you will correct it if I am wrong.



  • I only recommend one block. Normal animateWithDuration:animations:

    doesn't support concurrent animation (at least in my case it caused some problems). I suggest instead of using RZViewActions to use a more complex approach with dispatch_group_t

    for simultaneous animation (and actions in general). In the latest version, you don't even need to prepare the groups / sequences before the animation starts.

  • The only difference I can see is that the code outside is animateWithDuration:animations:

    executed in the same loop NSRunLoop

    , and if I put it inside the animation block, it will be calculated in the next loop. So, for example, it can be useful to place calculations outside the block if sometimes you don't want to trigger an animation based on the results of those calculations.

    On the other hand, it can slow down the current cycle, which is usually more critical than the slower performance of the next cycle. Also, you have to think about the variables that you need to pass to the block correctly.

    In both cases, the animation does not start until the end of the animation block. So if you set some kind of visual variable (like the view frame), it is set as usual and you can use it right away. But this value is not displayed until the start of the completion block. Thus, you animateWithDuration:animations:

    will only see the start and end values. Even if the animation is interrupted, it will jump straight to the final value (this is the default behavior unless you specify additional parameters).

+1


source







All Articles