First, write a simple animation, so that a uiview from the upper left corner of the screen to move to the lower left corner, the interval time 3S
////VIEWCONTROLLER.M//caanimationtest////Created by on 15-10-27.//Copyright (c) 2015 Va. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller () @property (nonatomic, strong) UIView*Aniview;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; _aniview= [[UIView alloc] Initwithframe:cgrectmake (0,0, -, -)]; _aniview.backgroundcolor=[Uicolor Yellowcolor];//[Self.view Addsubview:_aniview];UIView*backview =[[UIView alloc] initWithFrame:self.view.bounds]; Backview.backgroundcolor=[Uicolor Greencolor]; [Self.view Addsubview:backview]; [Backview Addsubview:_aniview]; Cabasicanimation*animation = [Cabasicanimation animationwithkeypath:@"position"]; Animation.fromvalue= [Nsvalue Valuewithcgpoint:cgpointmake ( -, -)]; Animation.tovalue= [Nsvalue valuewithcgpoint:cgpointmake (Cgrectgetwidth (self.view.bounds)- -, Cgrectgetheight (Self.view.bounds)- -)]; Animation.duration=3; Animation.removedoncompletion=NO; Animation.fillmode=kcafillmodeforwards; Animation.Delegate=Self ; [_aniview.layer addanimation:animation Forkey:@"Testani"];}- (void) Animationdidstart: (Caanimation *) anim{NSLog (@"animation start, Aniview =%@", _aniview);}- (void) Animationdidstop: (Caanimation *) Anim finished: (BOOL) flag{NSLog (@"Animation Stop:flag =%d, Aniview =%@", flag, _aniview);}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
Effect:
Second, the animation interrupted several situations
Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (1.5 * nsec_per_sec)), Dispatch_get_main_queue () , ^{ [_aniview Removefromsuperview]; }); Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (1.5 * nsec_per_sec)), Dispatch_get_main_ Queue (), ^{ [Backview Removefromsuperview]; }); Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (1.5 * nsec_per_sec)), Dispatch_get_main_ Queue (), ^{ [_aniview.layer removeallanimations]; });
All three of the above will be recalled in the following way
-(void) Animationdidstop: (caanimation *) Anim finished: (BOOL) flag
Output:
2015-10-28 01:08:19.330 caanimationtest[3615:6005077] animation start, Aniview = <UIView:0x7fe47b5308b0; frame = (0 0; 100 100); Animations = {testani=<cabasicanimation:0x7fe47b534020>;}; Layer = <CALayer:0x7fe47b530980>>
2015-10-28 01:08:37.300 caanimationtest[3615:6005077] Animation Stop:flag = 1, Aniview = <UIView:0x7fe47b5308b0; frame = (0 0; 100 100); Animations = {testani=<cabasicanimation:0x7fe47b534020>;}; Layer = <CALayer:0x7fe47b530980>>
Third, animation pause
Dispatch_after(dispatch_time(dispatch_time_now, (int64_t) (1.5 * nsec_per_sec) ), Dispatch_get_main_queue(), ^{
Cftimeinterval pausedtime = [_aniview. Layer convertTime: Cacurrentmediatime() Fromlayer:nil];
_aniview. layer. Speed = 0.0;
_aniview. layer. timeoffset = pausedtime;
Dispatch_after(dispatch_time(dispatch_time_now, (int64_t) ( nsec_per_sec ) ), Dispatch_get_main_queue(), ^{
cftimeinterval pausedtime = [_aniview. Layer Timeoffset];
_aniview. layer. Speed = 1.0;
_aniview. layer. timeoffset = 0.0;
_aniview. layer. beginTime = 0.0;
Cftimeinterval timesincepause = [_aniview. Layer convertTime: Cacurrentmediatime() fromlayer :Nil]-pausedtime;
_aniview. layer. beginTime = timesincepause;
});
});
The pausetime here is the time interval at which the animation has moved, and after pausing for a while to reply, it is equivalent to starting at the current point-pausetime animation.
Cabasicanimation several stop callbacks