#import "ViewController.h"@interfaceViewcontroller () @property (weak, nonatomic) Iboutlet UIView*Redview;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.}-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{cabasicanimation*anim =[cabasicanimation animation]; Anim.keypath=@"POSITION.Y"; Anim.tovalue= @ -; /** * 1:cabasicanimation: Mainly used for zooming, panning, transitions Catransiton animation is mainly used for scene switching, cakeyframeanimation: Keyframe animation, mainly used for icon jitter, etc. 2: in Cabasicanim ation, set Anim.removedoncompletion, the purpose is to not delete the animation after the end of the animation, Anim.fillmode = Kcafillmodeforwards; Set the animation mode, set these two properties, you can not let the animation after the end of the execution, reverts to the original position, but retains the state of the ending animation 3:caanimationgroup: She automatically performs animations in the array animations*/ //anim.removedoncompletion = NO;//anim.fillmode = kcafillmodeforwards;// //[Self.redView.layer Addanimation:anim forkey:nil];cabasicanimation*ANIM2 =[cabasicanimation animation]; Anim2.keypath=@"Transform.scale"; Anim2.tovalue= @0.5;//anim2.removedoncompletion = NO;//anim2.fillmode = kcafillmodeforwards;//[Self.redView.layer addanimation:anim2 forkey:nil];Caanimationgroup*group =[Caanimationgroup animation]; //automatically executes all animated objects in the animations arrayGroup.animations =@[anim,anim2]; Group.removedoncompletion=NO; Group.fillmode=kcafillmodeforwards; [Self.redView.layer Addanimation:group Forkey:nil]; }@end
You can perform multiple animations at the same time.
Create a group animation
Caanimationgroup *group = [Caanimationgroup animation];
Translation
Cabasicanimation *anim = [cabasicanimation animation];
Anim.keypath = @ "POSITION.Y";
Anim.tovalue = @400;
Scaling
Cabasicanimation *scaleanim = [cabasicanimation animation];
Scaleanim.keypath = @ "Transform.scale";
Scaleanim.tovalue = @0.5;
Set Animation Group Properties
Group.animations = @[anim,scaleanim];
Group.removedoncompletion = NO;
Group.fillmode = Kcafillmodeforwards;
Adding group animations
[Self.redView.layer Addanimation:group Forkey:nil];
The benefit of using animation groups is that you don't need to add animations every time, setting properties when the animation finishes.
Just add the animation you want to perform to the animations array of the animation group,
The animation in the array is automatically executed when the group animation is added to the layer.
The properties that you set when the animation finishes are also set only once.
iOS Development Core Animation VI: Animation Group