Coreanimation is a function of Calayer, this section summarizes the Coreanimation Calayer and Catransaction
One,calayer
UIView can display the key is that it has a Calayer property, we can set the Calayer object to easily adjust the UIView interface effects such as shadows, fillets and so on.
UIView inherits Uiresponder, which responds to user events. And Calayer directly inherits the NSObject, just a display component. For example, if you want to display a picture, we can use Uiimageview, or you can use Calayer to set its contents property. UIView and Calayer, how do we choose to use it? For the adapter does not understand the use of iOS7 to see the detailed instructions
If the displayed things need to interact with the user, with UIView, if you do not need to interact with the user, it is recommended to use Calayer, because the Calayer no event processing, performance is higher.
Second, several important properties and methods of Calayer
(1) Position and Anchorpoint
These two properties determine the fixed position of the Calayer
Maskstobounds,zposition
(2) Drawincontext:
If you are inheriting the custom Calayer class of Calayer, implement-(void) Drawincontext: (Cgcontextref) CTx method. However, this method is not automatically invoked and requires execution of the Calayer Setneedsdisplay method trigger.
In addition, we can also use Calayer's proxy method to implement:
-(void) Drawlayer: (Calayer *) layer Incontext: (cgcontextref) Ctx;//nsobject already contains the declaration of calayerdelegate, no need to add the protocol anymore
Both of these methods require an explicit execution of setneedsdisplay to trigger.
Three, calayer animation
There are many places where Calayer animations are classified as implicit animations and explicit animations, Before Tekuba.net did not understand how the matter, today wrote a demo test the next effect, basically understand what it means. This is understood as follows:
UIView is associated with a calayer,tekuba.net in many documents, called it "Root Calayer", all non-Calayer "animatable" properties will be animated by default when modified. We can open CALayer.h to see that all attributes with "animatable" have so-called "implicit animations", such as Self.subLayer.position = Cgpointmake (100, 100), This code does not change the position directly to (100, 100) in the next runloop, but rather it is done by an animation. Note, however, that this refers only to the non-"root calayer", and if it is "root calayer", the result of the tekuba.net test is that it cannot produce animations.
If you want to customize the animation, you must use the explicit animation, this time the use of caanimation. About Caanimation the next part of the summary again.
Four,catransaction affairs
The setting of the Calayer "animatable" attribute should belong to a catransaction transaction, and Catransaction's role is to ensure that multiple "animatable" changes take place simultaneously. That is to say, Calayer property modification relies on catransaction.
The catransaction is also divided into implicit and explicit expressions.
An "animatable" attribute is set in a runloop, and if a transaction is not currently set, a catransaction is automatically created and commits the catransaction in the next runloop of the current thread.
[Explicit] is a direct call to Catransaction's [Catransaction Begin],[catransaction commit] and other related methods. For example, we do not want self.subLayer.position = cgpointmake (100, 100) to produce animation, you can set it in Catransaction:
<textarea class="crayon-plain print-no" style="line-height: 15px !important; overflow: hidden; font-size: 12px !important; z-index: 0; opacity: 0; -moz-tab-size: 4; -o-tab-size: 4; -webkit-tab-size: 4; tab-size: 4;" wrap="soft" readonly="" data-settings="dblclick">[catransaction begin]; [Catransaction setdisableactions:yes];//Close animation//[catransaction setvalue:[nsnumber numberwithfloat:5.0f] ForKey: kcatransactionanimationduration];//animation Time self.subLayer.position = cgpointmake (100, 100); [Catransaction commit]; </textarea>
| 12345 |
[catransactionbegin]; [catransactionsetdisableactions:YES];//Close animation//[catransaction setvalue:[nsnumber numberwithfloat:5.0f] forkey:kcatransactionanimationduration];//animation time Self.sublayer.position= Cgpointmake( -, -); [catransactionCommit]; |
In addition, transactions can be nested. When a transaction is nested, the entire animation starts only when the outermost transaction commits.
iOS research Coreanimation (2) detailed tutorials for-calayer and catransaction