標籤:
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)viewDidLoad11 {12 [super viewDidLoad];13 14 }15 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event16 {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)test231 {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)test145 {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 *)anim62 {63 NSLog(@"核心動畫執行之前 %@", NSStringFromCGPoint(self.cutomView.layer.position));64 65 }66 67 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag68 {69 NSLog(@"核心動畫執行完畢 %@", NSStringFromCGPoint(self.cutomView.layer.position));70 }71 72 - (void)test73 {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)didStopAnimatino86 {87 NSLog(@"動畫執行完畢 %@", NSStringFromCGPoint(self.cutomView.center));88 }89 90 91 92 93 @end
ios UIView封裝動畫