First, Calayer
Calayer is included in the Quartzcore framework and is cross-platform , and the essence of developing animations using core animation in iOS is to convert calayer content into bitmaps for hardware operations .
Common Properties:
| Properties |
Description |
| Anchorpoint |
and center position coincident points, called anchor points, in the range (0~1,0~1) |
| Position |
The location of the center point of the layer, equivalent to UIView |
| Bounds |
Layer Size |
| Opacity |
Transparency, equivalent to the alpha of UIView |
To create a custom Calayer:
Custom LayersCalayer *layer = [[Calayer Alloc] Init];layer.bounds =CGRectMake (0,0, Photo_height, photo_height); layer.position =cgpointmake (160, 200); Layer.backgroundcolor = [uicolor Redcolor]. Cgcolor;layer.cornerradius = Photo_height/2; Note that just setting the fillet is normal for the graphic, but the picture that is drawn in the layer does not display correctly, and when you draw a picture onto the layer, a layer is recreated to add to the current layer // If you want to display correctly, you must set maskstobounds = YES, cut sublayer layer.maskstobounds = yes; Set Border Layer.bordercolor = [uicolor Whitecolor]. Cgcolor;layer.borderwidth = 2; Set Layer Proxy layer.delegate = self; Add layer to root layer [self.view.layer addsublayer:layer];
Note: The shadow effect cannot be used in conjunction with Maskstobounds because the purpose of maskstobounds is to cut the outer border, and the shadow effect is just the outer border
Second, Core Animation
CAAnimation: core animation of the base class , can not be used directly, responsible for the animation running time, speed control, itself to achieve the camediatiming protocol.
CAPropertyAnimation: the base class for property animations (animated by properties, note is animated), not directly available.
CAAnimationGroup: animation Group, animation Group is a combination of pattern design, you can use animation groups to do all the animation behavior of the unified control, all the animation effect in the group can be executed concurrently.
CATransition: transitions Animation , which is set primarily with the filter animation effect.
CABasicAnimation: base animation , animate parameter control through property modification, only initial state and end state.
CAKeyframeAnimation: keyframe Animation , which is also controlled by animating parameters with attributes, but unlike the underlying animation, it can have multiple state controls.
1. Basic animation cabasicanimation Basic animation creation is generally divided into the following steps:
①. Initializing animations and setting animation properties
②. Set Animation Property initial value (can be omitted), end value, and other animation properties
③. Adding animations to a layer
Let's start with a moving animation example:
#pragma mark move Animation-(void) Translatonanimation: (Cgpoint) location{1. Create animations and specify animation propertiesCabasicanimation *basicanimation = [Cabasicanimation Animationwithkeypath:@ "position"];//2. Set animation property initial and end values Basicanimation.fromvalue = [NSValue Valuewithcgpoint:cgpointzero]; basicAnimation.toValue = [ nsvalue valuewithcgpoint:location]; //set other animation properties Basicanimation.delegate = self;//set proxy basicanimation.duration = 3.0; Animation time 3 sec. //basicanimation.repeatcount = huge_valf;//Set the number of repetitions, huge_valf can be seen as Infinity //basicanimation.removedoncompletion = no;//Run once to remove animation //3. Add animations to layers, Note that the key is equivalent to naming the animation, which can be used to obtain the [_layer addanimation:basicanimation forkey:
Performing the motion animation above, there is a phenomenon, that is, after the end of the animation layer back to the original position , this is why?
Because the essence of layer animation is that the content inside the layer is converted to a bitmap, the animation effect of the hardware operation, the layer itself in the animation process has not changed any.
What can be done to solve this problem? The answer is Caanimation Agent!!
#pragma mark-Animation agent method#pragma mark Animation begins-(void) Animationdidstart: (caanimation *) anim{if ([Anim iskindofclass:[cabasicanimation class]]) {cabasicanimation *baseanima = (cabasicanimation *) Anim; //use Key-value to maintain end position coordinates [Baseanima setValue:baseAnima.toValue forkey:@" Mtbasicanimationlocation "]; }} #pragma mark Animation End-(void) Animationdidstop: (caanimation *) Anim finished: (bool) flag{if ( Flag) {//remove end position coordinates, set _layer.position = [[Anim valueforkey:@ " Mtbasicanimationlocation "] cgpointvalue]; }}
But then another problem arises when the animation ends with a short animation that goes back to the beginning and then moves to the end, what is the reason?
is an implicit animation , for a non-root layer, setting the animated properties of the layer produces an automatic animation effect (for example position , except frame that, in fact, other properties are animated properties), an animated effect called an implicit animation
- Add : Perhaps someone asked, that we have been so laborious to
CABasicAnimation do animation, directly set position the layer can not be implemented? The answer is that this implicit animation we do not control animation time and other animation properties, it is the system automatically help us to achieve the transition animation effect.
Here we want to eliminate this problem, you can use the transaction to CATransaction temporarily eliminate implicit animation, only need to modify the end-of-animation proxy method:
#pragma mark 动画结束-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{ if( flag ) { [CATransaction begin];//开启事务 [CATransaction setDisableActions:YES];//禁止隐式动画 _layer.position = [[anim valueForKey:@"MTBasicAnimationLocation"] CGPointValue]; [CATransaction commit];//提交事务 }}
About the business CATransaction and this section of the topic is not very relevant, not to speak, is interested in the official Apple document to see how to use the specific
In addition to changing the position, you can also change other deformation properties, the following is an example of a rotating animation
#pragma mark rotate Animation-(void) rotationanimation{//1. Creating animations and specifying animation properties cabasicanimation *basicanimation = [cabasicanimation animationwithkeypath:@ "TRANSFO Rm.rotation.z "]; //2. Set Animation Property initial value, end value Basicanimation.fromvalue = [nsnumber numberwithfloat:m_pi_2]; basicanimation.tovalue = [ nsnumber numberwithfloat:m_pi_2*3]; //Set other animation properties Basicanimation.duration = 6.0; basicanimation.autoreverses = true; Rotate and then rotate to the original position //4. Add animations to layers, note that key is equivalent to naming an animation, and you can use this name to get the [_layer addanimation:basicanimation Forkey later when you get the animation: @ "Mybasicanimation_rotation"];}
2. Keyframe Animation cakeyframeanimation Key-frame animation is developed in 2 ways:
- Keyframe control by setting different property values
- Keyframe control by drawing a path
Here is an example:
#pragma the first method of the Mark Keyframe Animation-(void) translationanimation1{1. Create Keyframe animations and set animation propertiesCakeyframeanimation *keyframeanimation = [Cakeyframeanimation Animationwithkeypath:@ "position"];2. Set keyframes, there are four keyframes, the initial value of the Keyframe animation cannot be omittedNsvalue *key1 = [Nsvalue ValuewithCgpoint:_layer.position];Nsvalue *key2 = [Nsvalue ValuewithCgpoint:Cgpointmake (80,220)];Nsvalue *key3 = [Nsvalue ValuewithCgpoint:Cgpointmake (45,300)];Nsvalue *key4 = [Nsvalue ValuewithCgpoint:Cgpointmake (55,400)];Nsarray *values = @[key1,key2,key3,key4]; Keyframeanimation.values = values;Set other Properties Keyframeanimation.duration =8.0; Keyframeanimation.begintime =Cacurrentmediatime () +2;Setting Delay 2 seconds execution3. Add animations to layers and animate [_layer addanimation:keyframeanimation Forkey:@ "Mykeyframeanimation_position1"];}#pragma the second way of Mark Keyframe Animation-(void) translationanimation2{1. Create Keyframe animations and set animation propertiesCakeyframeanimation *keyframeanimation = [Cakeyframeanimation Animationwithkeypath:@ "position"];2. Setting the pathDraw Bezier CurvesCgpathref Path =Cgpathcreatemutable ();Cgpathmovetopoint (Path,NULL, _layer.position.x, _LAYER.POSITION.Y);Move to start pointcgpathaddcurvetopoint (Path, null, 160, 280, -30, 300, 55, 400); //Set the Path property cgpathrelease (path); //set additional properties keyframeanimation.duration = 8.0; Keyframeanimation.begintime = cacurrentmediatime () + 2;//set delay 2 seconds to execute //3. Add animations to layers, animate after adding animations [_layer addanimation: Keyframeanimation forkey:@ "Mykeyframeanimation_position2"];}
3. Animation Group Caanimationgroup
is a combination of multiple animations, directly to the code:
#pragma mark Create animation group-(void) groupanimation{1. Create an animation groupCaanimationgroup *animationgroup = [Caanimationgroup Animation];//2. Setting animations and other properties in a group cabasicanimation *basicanimation = [selfrotationanimation]; Basic animation Cakeyframeanimation *keyframeanimation = [selftranslationanimation]; Keyframe animation animationgroup.animations = @[basicanimation,keyframeanimation]; Animationgroup.delegate = self ; //Set the animation time, if the animation has been set in animation group animation properties, it is no longer effective animationgroup.duration = 10.0; animationgroup.begintime = Cacurrentmediatime () + 5; Delay five seconds to execute//3. Animate a layer [_layer addanimation:animationgroup forkey:nil];}
4. Transition Animation catransition transition animations using several steps:
①. Creating a transition animation
②. Setting transition types, subtypes (optional), and other properties
③. Set a new view after the transition and add animations to the layer
Directly on the code:
#pragma mark Transitions Animation-(void) Transitionanimation: (BOOL) isnext{//1. Creating a Transition Animation object catransition *transition = [[catransition alloc] init]; //2. To set the transition type, note that for Apple the official not public animation type can only use strings, and there is no corresponding constant definition Transition.type = @ "Cube"; //Set the transition direction type if (isnext) {transition.subtype = kcatransitionfromright;} else{transition.subtype = kcatransitionfromleft;} //Set animation often transition.duration = 1.0f; //3. Set a new view after the transition add transition animation _imageview.image = [selfgetimage:isnext]; [_imageview.layer addanimation:transition Forkey:@ "Kctransitionanimation"];}
iOS Learning note 09-Core animation coreanimation