iOS-動畫效果(首尾式動畫,代碼快動畫,核心動畫,序列幀動畫)

一.各個動畫的優缺點數組

1.首尾動畫:若是隻是修改空間的屬性,使用首尾動畫比較方便,若是在動畫結束後作後續處理,就不是那麼方面了。動畫

2.核心動畫:有點在於對後續的處理方便。spa

3.塊動畫:代理

(1)在實際的開發中更經常使用的時block代碼塊來處理動畫操做。code

(2)塊動畫相對來講比較靈活,尤其重要的是可以將動畫相關的代碼編寫在一塊兒,便於代碼的閱讀和理解.orm

4.使用序列幀動畫:對UIImageview和button按鈕進行連線。blog

 

  1 #import "ViewController.h"
  2 
  3 @interface ViewController ()
  4 {
  5     UIView             *_view;
  6     UIImageView        *_imageView;
  7 }
  8 @end
  9 
 10 @implementation ViewController
 11 
 12 - (void)viewDidLoad {
 13     [super viewDidLoad];
 14 
 15     //用於block代碼快動畫,核心動畫,UIView封裝的首尾式動畫
 16     _view = [[UIView alloc] init];
 17     _view.frame = CGRectMake(0, 20, 40, 40);
 18     _view.backgroundColor = [UIColor redColor];
 19     [self.view addSubview:_view];
 20     
 21     //用於序列幀動畫
 22     _imageView = [[UIImageView alloc] init];
 23     _imageView.frame = CGRectMake(60, 20, 40, 40);
 24     _imageView.backgroundColor = [UIColor orangeColor];
 25     [self.view addSubview:_imageView];
 26 }
 27 
 28 #pragma mark 序列幀動畫
 29 //- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
 30 //{
 31 //    NSMutableArray * arrayM = [NSMutableArray array];
 32 //    
 33 //    
 34 //    for (int i = 0; i < 4; i ++) {
 35 //        [arrayM addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]]];
 36 //    }
 37 //    //設置動畫數組
 38 //    [_imageView setAnimationImages:arrayM];
 39 //    
 40 //    //設置動畫播放次數 MAXFLOAT:無窮大數,表示一直循環下去
 41 //    [_imageView setAnimationRepeatCount:MAXFLOAT];
 42 //    
 43 //    //設置動畫播放時間(圖片個數 * 計劃的每張照片動畫時長)
 44 //    [_imageView setAnimationDuration:4 * 0.75];
 45 //    
 46 //    //開始動畫
 47 //    [_imageView startAnimating];
 48 //}
 49 
 50 
 51 #pragma mark block代碼快動畫
 52 //- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
 53 //{
 54 //    
 55 //    [UIView animateWithDuration:2.0 animations:^{
 56 //         NSLog(@"動畫開始執行前的位置: %@",NSStringFromCGPoint(_view.center));
 57 //        _view.center = CGPointMake(self.view.bounds.size.width - 20, self.view.bounds.size.height - 20);
 58 //        
 59 //        
 60 //    } completion:^(BOOL finished) {
 61 //        NSLog(@"動畫執行完畢後的位置: %@",NSStringFromCGPoint(_view.center));
 62 //    }];
 63 //}
 64 
 65 #pragma mark 核心動畫
 66 //- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
 67 //{
 68 //    //建立核心動畫
 69 //    CABasicAnimation * animation = [CABasicAnimation animation];
 70 //    
 71 //    //平移
 72 //    animation.keyPath = @"position";
 73 //
 74 //    //設置執行的動畫
 75 //    animation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.view.bounds.size.width - 20, self.view.bounds.size.height - 20)];
 76 //    
 77 //    //設置動畫的時長
 78 //    animation.duration = 2.0f;
 79 //    
 80 //    //設置動畫執行完畢以後不刪除動畫
 81 //    animation.removedOnCompletion = NO;
 82 //    
 83 //    //設置保存動畫的最新狀態
 84 //    animation.fillMode = kCAFillModeForwards;
 85 //    
 86 //    //設置動畫的代理
 87 //    animation.delegate = self;
 88 //    
 89 //    //給控件添加核心動畫
 90 //    [_view.layer addAnimation:animation forKey:nil];
 91 //}
 92 //#pragma mark 核心動畫開始時調用的方法
 93 //- (void)animationDidStart:(CAAnimation *)anim
 94 //{
 95 //    NSLog(@"動畫開始執行前的位置: %@",NSStringFromCGPoint(_view.center));
 96 //}
 97 //#pragma mark 核心動畫結束時調用的方法
 98 //- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
 99 //{
100 //    NSLog(@"動畫執行完畢後的位置: %@",NSStringFromCGPoint(_view.center));
101 //}
102 
103 
104 
105 #pragma mark UIView封裝的首尾式動畫
106 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
107 {
108     NSLog(@"動畫執行以前的位置: %@",NSStringFromCGPoint(_view.center));
109     
110     //首尾式動畫
111     //執行動畫
112     [UIView beginAnimations:nil context:nil];
113     
114     //動畫時間
115     [UIView setAnimationDuration:2.0f];
116     
117     //設置動畫的代理
118     [UIView setAnimationDelegate:self];
119     
120     //設置動畫執行完畢調用的事件
121     [UIView setAnimationDidStopSelector:@selector(didStopAnimation)];
122     
123     _view.center = CGPointMake(self.view.bounds.size.width - 20, self.view.bounds.size.height - 20);
124     
125     //動畫結束
126     [UIView commitAnimations];
127 }
128 
129 #pragma mark 動畫執行完畢調用的方法
130 - (void)didStopAnimation
131 {
132     NSLog(@"動畫執行完畢");
133     
134     NSLog(@"動畫執行以後的位置: %@",NSStringFromCGPoint(_view.center));
135 }
136 
137 @end
相關文章
相關標籤/搜索