您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 開發學習:簡單動畫實作整理

編輯:iOS 開發綜合

本文整理 iOS 開發中幾種常見的簡單動畫:UIActivityIndicatorView、UIImageView 序列幀動畫、UIView 動畫與時鐘動畫。

一、UIActivityIndicatorView

1. 屬性:isAnimating 用來判斷是否正在播放動畫。

2. 第三方庫 SVProgressHUD:

[SVProgressHUD dismiss];
[SVProgressHUD showWithStatus:@"網絡加載中" maskType:SVProgressHUDMaskTypeGradient]; // 後面的參數表示是否遮擋,可省略

3. NSTimer:

[NSTimer scheduledTimerWithTimeInterval:1 target:2 selector:3 userInfo:4 repeats:5];
// 1. 時間間隔 2. self 3. 觸發時呼叫的方法 4. 使用者資訊 5. 是否重複
[timer invalidate]; // 關閉時鐘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // 使用 SVProgressHUD 的 maskType 屬性後,當前視圖無法接受觸摸事件
    // 使用 NSTimer 定時關閉
    if ([_indicatorView isAnimating]) {
        [SVProgressHUD dismiss];
        [_indicatorView stopAnimating];
    } else {
        [SVProgressHUD showWithStatus:@"等會~~~" maskType:SVProgressHUDMaskTypeGradient];
        // 指示器啟動後就無法交互,要把時鐘放在這裡
        [_indicatorView startAnimating];
        [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:NO];
    }
}

// NSTimer 呼叫方法的參數只能是 NSTimer
- (void)updateTimer:(NSTimer *)timer {
    [SVProgressHUD dismiss];
    [_indicatorView stopAnimating];
    [timer invalidate]; // 關閉時鐘
}

二、UIImageView 的序列幀動畫

1. 屬性說明:

  • animationImages:要顯示的一組圖片。
  • animationDuration:顯示所有圖片所需的時間(若要中途修改速度,需先停止、設定、再開始)。
  • animationRepeatCount:動畫的執行次數。

2. 相關方法:startAnimating、stopAnimating、isAnimating。

趙雲動畫:

// 建立趙雲的序列幀動畫
- (void)createZhaoyunImage {
    // 1. 需要一個陣列
    NSMutableArray *images = [NSMutableArray array];
    for (int i = 1; i < 11; i++) {
        NSString *fileName = [NSString stringWithFormat:@"/images/zy/%d.png", i];
        UIImage *image = [UIImage imageNamed:fileName];
        [images addObject:image];
    }

    [_zhaoyunImage setImage:images[0]];
    [_zhaoyunImage setAnimationImages:images]; // 設定圖像陣列
    [_zhaoyunImage setAnimationDuration:1.0f]; // 10 張圖片的播放時長
    [_zhaoyunImage startAnimating]; // 開始動畫
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([_zhaoyunImage isAnimating]) {
        // 序列幀動畫每次停止時,都會顯示設定的圖像,而不是當前播放到的圖像
        [_zhaoyunImage stopAnimating];
    } else {
        [_zhaoyunImage startAnimating];
    }
}

燕子動畫:

#pragma mark - 拖拽燕子
- (void)dragBird:(UIPanGestureRecognizer *)sender {
    // 先取得手指的位置
    CGPoint location = [sender locationInView:self.view];

    // 手勢狀態為 UIGestureRecognizerStateChanged 時,手指還沒離開螢幕
    if (sender.state == UIGestureRecognizerStateChanged) {
        // 若要修改播放中的序列幀動畫,需先停止動畫
        [_birdImage stopAnimating];
        // 修改動畫頻率
        [_birdImage setAnimationDuration:0.2f];
        // 重新啟動動畫
        [_birdImage startAnimating];
        // 設定燕子的位置
        [_birdImage setCenter:location];
    } else if (sender.state == UIGestureRecognizerStateEnded) {
        // 恢復燕子慢悠悠
        [_birdImage stopAnimating];
        [_birdImage setAnimationDuration:1.2f];
        [_birdImage startAnimating];
    }
}

// 設定燕子的序列幀動畫,並加上拖拽手勢監聽
- (void)createBirdImage {
    NSArray *images = @[[UIImage imageNamed:@"/images/素材/燕子1.png"],
                        [UIImage imageNamed:@"/images/素材/燕子2.png"]];
    [_birdImage setAnimationImages:images];
    [_birdImage setAnimationDuration:1.2f];
    [_birdImage startAnimating];

    // 定義手勢
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragBird:)];
    [_birdImage addGestureRecognizer:pan];

    // 設定燕子圖片允許使用者交互
    [_birdImage setUserInteractionEnabled:YES];
}

三、UIView 動畫

此處可依需求使用 UIView 的 animateWithDuration 系列方法,實作各種過場與屬性動畫。

四、時鐘動畫(CADisplayLink)

CADisplayLink 是一種以螢幕刷新觸發的時鐘機制,每秒大約執行 60 次,使用前需導入 QuartzCore 框架。

1. 使用方法:定義 CADisplayLink 並指定觸發呼叫的方法,再將顯示鏈接加入主運行循環佇列。

2. 兩種判斷時間間隔的方法:以成員變數判斷,或以全局變數判斷。

@interface ViewController () {
    // 遊戲時鐘
    CADisplayLink *_gameTimer;
    // 時鐘觸發的初始時間
    CFTimeInterval _startTime;
}
@end

@implementation ViewController

#pragma mark - 用時間戳判斷觸發間隔的方法(1)
- (void)updateTimer:(CADisplayLink *)sender {
    // 若 _startTime = 0,表示第一次觸發,需記錄時間戳
    if (_startTime == 0) {
        _startTime = sender.timestamp;
    }
    CFTimeInterval deltaTime = sender.timestamp - _startTime;
    if (deltaTime > 1.0) {
        NSLog(@"時鐘觸發了 %f", sender.timestamp);
        _startTime = sender.timestamp; // 記錄本次執行的時間
    }
}

#pragma mark - 用計數判斷觸發間隔的方法(2)
- (void)step {
    // 假定每隔一秒觸發一次
    if (steps % (10 * 60 * 60) == 1) {
        NSLog(@"時鐘觸發了! %ld", steps);
    }
    steps++;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _startTime = 0.0f;
    steps = 0;
    // 初始化遊戲時鐘
    _gameTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(step)];
    // 初始化後必須把時鐘加入主運行循環
    [_gameTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}

3. 使用步驟:

方法一:初始化遊戲時鐘並加入主佇列,自己指定時間間隔去執行操作——用一個成員變數記錄時鐘起始的「時間點」(CFTimeInterval _startTime),計算它與時鐘觸發時的時間差,若大於指定間隔就執行操作。

方法二:定義一個全局的每秒執行次數,達到該次數時執行操作。以下以雪花飄落為例:

steps = 0;
// 初始化雪花圖像
_snowImage = [UIImage imageNamed:@"/images/素材/雪花"];

// 初始化時鐘
_gameTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(step)];
[_gameTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
#pragma mark - 時鐘處理方法
- (void)step {
    // 假設每秒鐘繪製 6 個雪花
    if (steps % 10 == 0) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:_snowImage];

        // 指定雪花出現的位置與結束位置
        CGPoint startPoint = CGPointMake(arc4random() % 320, -30.0);
        CGPoint endPoint = CGPointMake(arc4random() % 320, 490);

        CGFloat r = arc4random() % 5 + 10;
        [imageView setFrame:CGRectMake(0, 0, r, r)];
        [imageView setCenter:startPoint];
        [self.view addSubview:imageView];

        [UIView animateWithDuration:6.0f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
            [imageView setCenter:endPoint];
            [imageView setAlpha:0.2f];
            [imageView setTransform:CGAffineTransformMakeRotation(M_PI)];
        } completion:^(BOOL finished) {
            // 從父視圖刪除,這步千萬別忘了
            [imageView removeFromSuperview];
        }];
    }
    steps++;
}
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved