1. Core animation framework
- Cascrolllayer is a subclass of calayer, which simplifies the display of part of the layer. The range of the scroll area of the cascrolllayer object is defined in its child layer. Cascrolllayer
No keyboard or mouse event processing or visible scroll bars are provided.
- Catextlayer allows you to easily create a layer class content from a string or string.
- Eagledlayer allows you to incrementally display large and complex images.
- Caeagllayer provides an opengles rendering environment.
- Catransition provides a transition effect for layer changes, which can affect the entire content of the layer. Fade, push, and reveal layers during the animation. These transition effects can be extended to your custom core image filter (cifilter, not available for iOS at the moment ).
- Caanimationgroup allows a series of animation effects to be combined to display animations in parallel.
- Capropertyanimation is an abstract subclass that supports attributes specified in the Key Path of the animation display layer.
- Cabasicanimation can be used to modify attributes of a layer.
Cakeyframeanimation supports key frame animation. You can specify the Key Path animation of the layer attribute, including the animation value at each stage, and a series of values of the Key Frame Time and timing function. In
When an animation is running, each value is replaced by a specific insert value.
The caconstraint class is a layout manager used to describe the relationship between layer geometric attributes (left, right, top or bottom edge or horizontal or vertical center.
Catransaction coordinates multiple animation atomic update and display operations in the core animation. Every modification to the animation attribute of a layer is part of the transaction.
Implicit transactions and explicit transactionsAn implicit transaction is automatically created when the animation attribute of a layer is modified by a thread and the modification is automatically submitted during the next iteration of the thread. An explicit transaction sends a start message to catransaction before the program modifies the animation attribute. The message is submitted after the animation attribute is modified.
2. Layer
The anchorpoint attribute of a layer is a cgpoint value, which specifies a token based on the layer bounds.
Position of the coordinate system. Anchor Point specifies the bounds value relative to position (equivalent to the center of the view ).
As the pivot point of the Transformation. The anchor points are represented in the unit space coordinate system (0.0, 0.0) and close to the layer.
(1.0, 1.0) is the diagonal point of the origin.
3. Transformation Matrix catransform3d Transformation Matrix 4*4 cgaffinetransform ry matrix [a B C D TX ty]
The core animation extends the key-value encoding protocol and allows you to obtain and set the value of the catransform3d matrix of a layer through the Key Path. Table 4 describes the attributes of layer transform and sublayertransform.
The corresponding Key Path.
Table 4 catransform3d key paths
You cannot use the objective-C 2.0 attribute method to specify the key path of a structure field. The following code cannot be executed normally ::
myLayer.transform.rotation.x=0;
The alternative method is to use setvalue: forkeypath: Or valueforkeypath:
[myLayer setValue:[NSNumber numberWithInt:0] forKeyPath:@"transform.rotation.x"];
4. Each uiview automatically creates a calayer class instance and assigns the layer attribute masktobounds to determine whether the child layer is cropped relative to the parent layer.
5. Provide content to calayer
By delegat: // The viewdisplaylayer: Or drawlayer: incontext:
Trigger the preceding callback setneedsdisplay and setneedsdisplayinrect: or set the needsdisplayonboundschange attribute of the layer to yes.
-(Void) drawlayer :( calayer *) thelayer incontext :( cgcontextref) thecontext
{Cgmutablepathref thepath = forward (); export cgpathmovetopoint (thepath, null, 15.0f, 15.f); partition (thepath, partition null, parameter 15.f, 2501_f, limit 295.0f, 2501_f, 295.0f, 15.0f);  cgcontextbeginpath (thecontext); cgcontextaddpath (thecontext, thepath); cgcontextsetlinewidth (thecontext, attributes [[thelayer valueforkey: @ "linewidth"] floatvalue]); cgcontextstrokepath (thecontext); then // release the path cfrelease (thepath );}
6. Modify the contentsgravity attribute of the layer.
7,
Layer actionWhen an action trigger occurs, the actionforkey of the layer is called. This method returns a behavior object (an object that meets the caaction Protocol), the corresponding identifier as a parameter, or if the behavior object does not exist, return nil.
When calayer is the actionforkey implemented by an identifier: the method is called, the following search modes will be used:
- If a layer has a delegate, the implementation of the actionforlayer: forkey: method will be called, and the layer and behavior identifier will be used as parameters. The implementation of the delegate actionforlayer: forkey: requires the following response:
- Returns the behavior object corresponding to a behavior identifier.
- Returns nil when the behavior identifier cannot be processed.
- The returned nsnull is returned when the behavior identifier cannot be processed and the search needs to be terminated.
- The actions Dictionary of the layer is searched to find an object corresponding to the behavior identifier.
- The style attribute of the layer is searched to find an actions dictionary containing the behavior identifier.
- A defaultactionforkey: Message occurs in the layer class. It returns a behavior object corresponding to the identifier, and if it does not exist, it returns nil.
Generally, the behavior object is a subclass instance of caanimation, which implements the caaction protocol. However, you can also return any class object that implements the caaction protocol. When the instance receives the message runactionforkey: object: arguments:, it needs to perform the corresponding action.
Code 1Runactionforkey: object: arguments: Implementation: Start Animation
- (void)runActionForKey:(NSString *)key object:(id)anObject arguments:(NSDictionary *)dict{ [(CALayer *)anObject addAnimation:self forKey:key];}
The overload implicit animation inserts an instance of caanimation into the actions Dictionary of the style dictionary, implements the delegate method actionforlayer: forkey: or inherits the layer class and reloads defaactionactionforkey: method returns a behavior object. The example in Code 2 replaces the implicit animation of the contents attribute with a delegate.
- (id<CAAction>)actionForLayer:(CALayer *)theLayer forKey:(NSString *)theKey{ CATransition *theAnimation=nil; if ([theKey isEqualToString:@"contents"]) { theAnimation = [[CATransition alloc] init]; theAnimation.duration = 1.0; theAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; theAnimation.type = kCATransitionPush; theAnimation.subtype = kCATransitionFromRight; } return theAnimation;}
The example in code 3 uses actions dictionary mode to disable the default animation of the sublayers attribute.
// get a mutable version of the current actions dictionaryNSMutableDictionary *customActions=[NSMutableDictionary dictionaryWithDictionary:[theLayer actions]];// add the new action for sublayers[customActions setObject:[NSNull null] forKey:@"sublayers"];// set theLayer actions to the updated dictionary
Thelayer. Actions = customactions;
8. Transaction implicit transactions
When the layer tree is modified by a thread that does not obtain the transaction, an implicit transaction is automatically created, and the transaction is automatically committed when the thread runs the Run-loop for the next iteration.
Important: When modifying layer attributes in a thread without a running loop, you must use explicit transactions, so all UI processing must be placed in the main thread. The subthread does not have runloop by default.
Explicit transactions temporarily disable layer actions. You can temporarily disable layer actions by setting the transaction's kcatransactiondisableactions value to yes when modifying layer attribute values. Any changes made within the transaction scope will not be animated accordingly. Code 2 shows an example where the fade-out animation is disabled when alayer is removed from the visualization layer tree.
[CATransaction begin];[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];[aLayer removeFromSuperlayer];[CATransaction commit];
You can temporarily change the animation time for responding to the layer attribute change by setting the value of the kcatransactionanimationduration key of the transaction to a new time.
Code 4 shows an example of nesting two transactions. The transaction at the outermost layer sets the implicit animation time to 2 seconds, and sets the position attribute value of the layer. The transaction in the inner layer sets the implicit animation time to 5 seconds, and modifies the opacity and zposition attribute values of the layer.
[CATransaction begin]; // outer transaction // change the animation duration to 2 seconds[CATransaction setValue:[NSNumber numberWithFloat:2.0f] forKey:kCATransactionAnimationDuration];// move the layer to a new positiontheLayer.position = CGPointMake(0.0,0.0); [CATransaction begin]; // inner transaction// change the animation duration to 5 seconds[CATransaction setValue:[NSNumber numberWithFloat:5.0f] forKey:kCATransactionAnimationDuration]; // change the zPosition and opacitytheLayer.zPosition=200.0;theLayer.opacity=0.0; [CATransaction commit]; // inner transaction[CATransaction commit]; // outer transaction
9. The key-value encoding compatible container classes calayer and caanimation are both key-value encoding compatible container classes. You can modify the value of the attribute key to provide default values for the key, you can create a subclass and reload defaultvalueforkey :. Subclass checks the corresponding key parameters and returns the appropriate default values. Listing 1 describes an example of defaultvalueforkey: which provides a new default value for maskstobounds.
+ (ID) defaultvalueforkey :( nsstring *) Key {If ([Key isequaltostring: @ "maskstobounds"])
return [NSNumber numberWithBool:YES]; return [super defaultValueForKey:key];
}