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

iOS 動畫入門(一):用 UIKit 打造基礎動畫效果

編輯:iOS 開發綜合

iOS 的動畫大致可以分成兩條路線:一般的、邏輯簡單的動畫,直接用 UIKit 提供的 API 就能搞定;若涉及多層時間軸、粒子或較複雜的圖層操作,則要動用 Core Animation。本文聚焦前者。

一、以動畫上下文撰寫 UIKit 動畫

這是最傳統的寫法:用 begin/commit 一對方法把要動畫的屬性包在中間。

-(void)animationOfUIKit
{
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
    [self.view addSubview:redView];

    [UIView beginAnimations:@"test" context:nil]; // 開始動畫
    [UIView setAnimationDuration:1];              // 動畫時長
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; // 淡入淡出的節奏曲線

    /* 這裡放要做動畫的屬性 */
    redView.backgroundColor = [UIColor blueColor];
    redView.frame = CGRectMake(50, 50, 200, 200);
    redView.alpha = 0.5;

    [UIView commitAnimations]; // 提交動畫
}

二、以 Block 撰寫 UIKit 動畫

更現代的寫法是把動畫包進 Block,程式碼可讀性更好,也方便寫完成後的收尾工作。

-(void)animationOfBlock
{
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
    [self.view addSubview:redView];

    [UIView animateWithDuration:1          // 時長
                      delay:0             // 延遲時間
                    options:UIViewAnimationOptionTransitionFlipFromLeft // 動畫效果
                 animations:^{
                     // 動畫設定區
                     redView.backgroundColor = [UIColor blueColor];
                     redView.frame = CGRectMake(50, 50, 200, 200);
                     redView.alpha = 0.5;
                 }
                 completion:^(BOOL finished){
                     // 動畫結束時呼叫
                 }];
}

三、翻轉、旋轉、位移、翻頁、縮放與取反

CoreGraphics 框架裡的 CGAffineTransform 類別可以用來設定 UIView 的 transform 屬性,控制視圖的縮放、旋轉與平移(又稱放射變換矩陣)。這類動畫一律以視圖最初位置的中心點作為參照,操作結束後可以用 view.transform = CGAffineTransformIdentity; 還原。

翻轉

[UIView beginAnimations:@"doflip" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
                       forView:manImageView
                         cache:YES];
[UIView commitAnimations];

旋轉

CGAffineTransform transform;
transform = CGAffineTransformRotate(manImageView.transform, M_PI / 6.0);

[UIView beginAnimations:@"rotate" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
[manImageView setTransform:transform];
[UIView commitAnimations];

位移

[UIView beginAnimations:@"move" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
manImageView.frame = CGRectMake(100, 100, 120, 100); // 改變 frame 的 x、y
[UIView commitAnimations];

翻頁

[UIView beginAnimations:@"curlUp" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; // 該枚舉為預設值,線性則是等速
[UIView setAnimationDuration:1];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp
                       forView:manImageView
                         cache:YES];
[UIView commitAnimations];

縮放

CGAffineTransform transform = CGAffineTransformScale(manImageView.transform, 1.2, 1.2);

[UIView beginAnimations:@"scale" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
[manImageView setTransform:transform];
[UIView commitAnimations];

取反

取反的效果,是把目前的 transform 反過來套用,等於做一個還原動作。

CGAffineTransform transform = CGAffineTransformInvert(manImageView.transform);

[UIView beginAnimations:@"Invert" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
[manImageView setTransform:transform];
[UIView commitAnimations];
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved