Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView Animation Blocks vs CAAnimation

iOS animation experts! What are the pros and cons of each method? I know Apple recommends blocks instead of the old UIView animation methods (UIView beginAnimations, etc), but what about CAAnimation? When would you use one method vs the other? Is there a tradeoff in terms of performance?

like image 925
mjisrawi Avatar asked Aug 11 '11 10:08

mjisrawi


People also ask

What is the difference between UIView and CALayer?

UIView: Views have more complex hierarchy layouts. They can receive user interactions like taps, pinches, cliks and more. Working with UIViews happens on the main thread, it means it is using CPU power. CALayer: Layers on other hand have simpler hierarchy.

Does UIView animate need weak self?

No, it is not needed in this case. animations and completion are not retained by self so there is no risk of strong retain cycle.

Does UIView animate run on the main thread?

The contents of your block are performed on the main thread regardless of where you call [UIView animateWithDuration:animations:] .

What is CATransaction?

CATransaction is the Core Animation mechanism for batching multiple layer-tree operations into atomic updates to the render tree. Every modification to a layer tree must be part of a transaction. Nested transactions are supported.


2 Answers

1) There is not much difference in terms of memory expenses between UIView's animation blocks and CALayers's CAAnimation nor in terms of the objects themselves.

2) There are limitations to the type of animations you can achieve with UIView's animation, so you might be forced to use CAAnimation anyway.

3) For those types of simpler animations that UIView can handle, it is usually a simpler API to use than that of CAAnimation.

like image 193
MiguelB Avatar answered Oct 25 '22 04:10

MiguelB


As miamk said, there are limitations to the types of animations you can achieve using the [UIView animateWith] method series. Personally I use CAAnimation, but that is because I am accustom to setting up animations that way. I feel you have more control. I tend to spend a day making a CAAnimation really really fine-tuned and then add it to my UIView extensions class so any view can use it.

like image 33
fatuous.logic Avatar answered Oct 25 '22 05:10

fatuous.logic