ios UIView封装动画
1 #import "NJViewController.h" 2 3 @interface NJViewController () 4 @property (weak, nonatomic) IBOutlet UIView *cutomView; 5 6 @end 7 8 @implementation NJViewController 9 10 - (void)viewDidLoad 11 { 12 [super viewDidLoad]; 13 14 } 15 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 16 { 17 18 [UIView transitionWithView:self.view duration:1.0 options:0 animations:^{ 19 NSLog(@"animations"); 20 // 要执行的动画 21 [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES]; 22 23 } completion:^(BOOL finished) { 24 NSLog(@"completion"); 25 // 执行完毕之后执行的动画 26 }]; 27 28 } 29 30 - (void)test2 31 { 32 [UIView animateWithDuration:2.0 animations:^{ 33 NSLog(@"动画执行之前: %@",NSStringFromCGPoint(self.cutomView.center)); 34 // 需要执行动画的代码 35 self.cutomView.center = CGPointMake(300, 300); 36 37 } completion:^(BOOL finished) { 38 // 动画执行完毕之后执行的代码 39 NSLog(@"动画执行之后: %@",NSStringFromCGPoint(self.cutomView.center)); 40 41 }]; 42 } 43 44 - (void)test1 45 { 46 // 1.创建核心动画 47 // 注意点:如果通过核心动画改变layer的位置状态, 表面上看上去已经改变了, 但是实质上是没有改变的 48 CABasicAnimation *anima = [CABasicAnimation animation]; 49 anima.keyPath = @"position"; 50 anima.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)]; 51 52 anima.removedOnCompletion = NO; 53 anima.fillMode = kCAFillModeForwards; 54 55 anima.delegate = self; 56 57 // 2.添加核心动画 58 [self.cutomView.layer addAnimation:anima forKey:nil]; 59 } 60 61 - (void)animationDidStart:(CAAnimation *)anim 62 { 63 NSLog(@"核心动画执行之前 %@", NSStringFromCGPoint(self.cutomView.layer.position)); 64 65 } 66 67 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag 68 { 69 NSLog(@"核心动画执行完毕 %@", NSStringFromCGPoint(self.cutomView.layer.position)); 70 } 71 72 - (void)test 73 { 74 // 1.UIVIEW封装的动画, 动画执行完毕之后不会反弹 75 NSLog(@"动画执行之前: %@",NSStringFromCGPoint(self.cutomView.center)); 76 [UIView beginAnimations:nil context:nil]; 77 [UIView setAnimationDuration:2.0]; 78 [UIView setAnimationDelegate:self]; 79 [UIView setAnimationDidStopSelector:@selector(didStopAnimatino)]; 80 self.cutomView.center = CGPointMake(300, 300); 81 [UIView commitAnimations]; 82 83 } 84 85 - (void)didStopAnimatino 86 { 87 NSLog(@"动画执行完毕 %@", NSStringFromCGPoint(self.cutomView.center)); 88 } 89 90 91 92 93 @end