iOS學習44之動畫

来源:http://www.cnblogs.com/gfxxbk/archive/2016/05/27/5531887.html
-Advertisement-
Play Games

1. 簡單動畫 1> UIImageView GIF 動畫 GIF圖的原理是:獲取圖片,存儲在圖片數組中,按照圖片數組的順序將圖片以一定的速度播放 2> UIActivityIndicatorView 風火輪動畫 在APP中,載入界面的時候我們都會看到一個想風火輪的動畫在不停的轉,這個動畫其實是iO ...


1. 簡單動畫

 1> UIImageView GIF 動畫

  GIF圖的原理是:獲取圖片,存儲在圖片數組中,按照圖片數組順序將圖片以一定的速度播放

  UIImageView *showGifimageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, 300, 300)];
    
    [self.view addSubview:showGifimageView];
    
    // 創建一個存儲圖片的數組
    NSMutableArray *saveImageArray = [NSMutableArray array];
    
    // 獲取圖片
    for (int i = 1; i < 12; i++) {
        // 拼接圖片名
        NSString *imageName = [NSString stringWithFormat:@"%d.tiff", i];
        // 根據圖片名獲取圖片
        UIImage *image = [UIImage imageNamed:imageName];
        // 將圖片加到數組
        [saveImageArray addObject:image];
    }
    // 設置gif的圖片組
    showGifimageView.animationImages = saveImageArray;
    // 設置播放速率
    showGifimageView.animationDuration = 1;
    // 設置播放的次數
    showGifimageView.animationRepeatCount = 0;
    // 開始動畫
    [showGifimageView startAnimating];

 2> UIActivityIndicatorView 風火輪動畫

  在APP中,載入界面的時候我們都會看到一個想風火輪的動畫在不停的轉,這個動畫其實是iOS中的一個類 UIActivityIndicatorView

    // 載入旋轉的菊花效果
    // 無需設置frame
    UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    // 確定位置
    indicatorView.center = self.view.center;
    
    [self.view addSubview:indicatorView];
    
    // 將菊花動畫效果開啟
    [indicatorView startAnimating];

 3> 動畫可以達到的效果

  • 傳達狀態

  • 提高用戶對直接操作的感知

  • 幫助用戶可視化操作的結果

 4> 使用動畫應該註意

  • 謹慎添加動畫,尤其是在那些不能提供沉浸式用戶體驗(讓人專註在當前由設計者營造的情境下感到愉悅和滿足暫時忘記真實世界的情境)的App中。

  如果App主要關註一些嚴肅的任務或者生產性任務,那麼動畫就顯得多餘了,還會無端打亂App的使用流程,降低應用的性能,讓用戶從當前的任務中分心。

  • 開發者的自定義動畫應該適當符合內置iOS應用的動畫

  用戶習慣於內置iOS App使用的精細動畫。事實上,用戶趨向於把視圖之間的平滑轉換,對設備方向改變的流暢響應和基於物理力學的滾動效果看作是iOS體驗的一部分。除非你的應用能夠給用戶沉浸式的體驗—比如游戲(自定義動畫應該可以與內置應用的動畫相媲美)

  • 使用風格類型一致的動畫

  在App中使用風格類型一致的動畫非常重要,可以讓用戶構建基於使用App獲得的用戶體驗。

2. UIView動畫之UIView基礎動畫

 1> 概述

  • UIKit 直接將動畫集成到 UIView 類中,當內部的一些屬性發生改變時,UIView將為這些改變提供動畫支持。

  • 執行動畫的工作由 UIView 類自動完成,但仍希望在執行動畫時通知視圖,為此需要將改變屬性的代碼放在 [UIView beginAnimations:nil context:nil][UIView commitAnimations] 之間。

 2> UIView基礎動畫種類

  • UIView位置大小動畫(改變View的frame)

  • UIView顏色動畫(改變View的color)

  • UIView透明度動畫(改變View的alpha)

  • 仿射-翻轉rotation

  • 仿射-旋轉transform 

 3> UIView基礎動畫步驟

  • 第一步:開始UIView動畫
+ (void)beginAnimations:(nullable NSString *)animationID context:(nullable void *)context;

  參數animationID: 是一個標識符字元串,用於告訴系統要進行哪一個動畫,可以自由定義

  參數context:額外的上下文信息傳遞,沒有就置為 nil

  • 第二步:設置動畫時長
+ (void)setAnimationDuration:(NSTimeInterval)duration;

  參數duration:表示動畫持續的時間長度,系統預設為0.2s,具體值可以根據需求自行設置

  • 第三步:設置UIView動畫的回調代理
+ (void)setAnimationDelegate:(nullable id)delegate; 

  參數delegate:設置代理變數,一般為nil,代理主要用於監聽動畫的開始和結束

  • 第四步:處理相關的動畫

  主要是對framecoloralpha翻轉方向旋轉角度的操作

  • 第五步:提交動畫效果
+ (void)commitAnimations;

  該方法主要是提交動畫,也就是告訴系統動畫執行完成

 4> UIView位置大小動畫(改變View的frame)

    // UIView動畫有開始beginAnimation,有結束commitAnimations
    // 第一步:開始UIView動畫
    [UIView beginAnimations:@"mov" context:nil];
    // 第二步:設置動畫時常
    [UIView setAnimationDuration:3];
    // 第三步:設置UIView動畫的回調代理
    [UIView setAnimationDelegate:self];
    // 第四步:設置相關的對象的frame
    _showView.frame = CGRectMake(100, 100, 200, 100);
    // 第五步:提交動畫效果
    [UIView commitAnimations]; 

 5> UIView顏色動畫(改變View的color)

    [UIView beginAnimations:@"color" context:nil];
    [UIView setAnimationDuration:4];
    [UIView setAnimationDelegate:self];
    _showView.backgroundColor = [UIColor purpleColor];
    [UIView commitAnimations];

 6> UIView透明度動畫(改變View的alpha)

    [UIView beginAnimations:@"alpha" context:nil];
    [UIView setAnimationDuration:5];
    [UIView setAnimationDelegate:self];
    _showView.alpha = 0.1;
    [UIView commitAnimations];

 7> 仿射-翻轉rotation

    // 第一步:開始UIView動畫
    [UIView beginAnimations:@"rotation" context:nil];
    // 第二步:設置動畫時常
    [UIView setAnimationDuration:0.5];
    // 第2.5步:設置淡入的效果(感覺效果不明顯)
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    // 第三步:設置UIView動畫的回調代理
    [UIView setAnimationDelegate:self];
    // 第4步:設置翻轉的方式
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:_showView cache:YES];
    [UIView commitAnimations];

 8> 仿射-旋轉transform

    [UIView beginAnimations:@"transform" context:nil];
    [UIView setAnimationDuration:2];
    [UIView setAnimationDelegate:self];
    // 第4步:設置旋轉角度
    CGAffineTransform transform = CGAffineTransformMakeRotation(-M_PI_2);
    // 第4.5步:設置旋轉角度的對象
    [_showView setTransform:transform];
    [UIView commitAnimations];

 9> UIView基礎動畫回調方法

+ (void)setAnimationWillStartSelector:(nullable SEL)selector;
+ (void)setAnimationDidStopSelector:(nullable SEL)selector;

  這兩個方法預設是 nil,不能使用,一般使用它們的替代方法

- (void)animationWillStart:(NSString *)animationID context:(void *)context;
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;

  實際操作的代碼

#pragma mark - UIViewAnimationDelegate的協議方法
- (void)animationWillStart:(NSString *)animationID context:(void *)context
{
    NSLog(@"%s__%d--ID = %@, context = %@", __FUNCTION__, __LINE__, animationID, context);
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    NSLog(@"%s__%d--ID = %@, context = %@", __FUNCTION__, __LINE__, animationID, context);
}

  註:UIViewAnimationDelegate 代理不需要我們在遵循,系統已經封裝好了

3. UIView動畫之Block動畫

 1> Block簡單動畫

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion;
  • 參數duration:設置動畫時長

  • 參數animations:是一個Block,主要用於設置動畫

  • 參數completion:也是一個Block,主要用於設置在動畫結束後的一些操作

    __weak typeof(self)weakSelf = self;
    // 第1個參數:設置動畫時長
    // 第2個參數:設置動畫
    // 第3個參數:動畫完成時進行的事情
    [UIView animateWithDuration:2 animations:^{
        weakSelf.playImageView.frame = CGRectMake(67, 74, 240, 286);
    } completion:^(BOOL finished) {
        NSLog(@"finished");
    }];

 2> Block複雜動畫

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion;
  • 參數duration:設置動畫時長
  • 參數delay:動畫執行的延遲時間

  • 參數options:枚舉值動畫效果,有很多枚舉值,大家可以根據需要進行選取

  • 參數animations:是一個Block,主要用於設置動畫

  • 參數completion:也是一個Block,主要用於設置在動畫結束後的一些操作

    // 第1個參數:設置動畫時長
    // 第2個參數:動畫的延遲時間
    // 第3個參數:枚舉值動畫效果
    // 第4個參數:設置動畫
    // 第5個參數:動畫完成時進行的事情
    __weak typeof(self)weakSelf = self;
    [UIView animateWithDuration:2 delay:1 options:UIViewAnimationOptionOverrideInheritedOptions animations:^{
        weakSelf.playImageView.center = weakSelf.view.center;
    } completion:^(BOOL finished) {
        NSLog(@"finished");
    }];

 3> Block關鍵幀動畫

+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewKeyframeAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion 
  • 參數duration:設置動畫時長
  • 參數delay:動畫執行的延遲時間

  • 參數options:關鍵幀枚舉值動畫效果,有很多枚舉值,大家可以根據需要進行選取

  • 參數animations:是一個Block,主要用於設置動畫。在這裡需要添加一個方法,即創建Block的關鍵幀

    + (void)addKeyframeWithRelativeStartTime:(double)frameStartTime relativeDuration:(double)frameDuration animations:(void (^)(void))animations;
  •   參數frameStartTime:幀動畫的開始時間

  •   參數frameDuration:幀動畫的持續時間

  •   參數animations:Block,用於設置動畫

  • 參數completion:也是一個Block,主要用於設置在動畫結束後的一些操作
    // 第1個參數:設置動畫時長
    // 第2個參數:動畫的延遲時間
    // 第3個參數:關鍵幀枚舉值動畫效果
    // 第4個參數:開始動畫
    // 第5個參數:動畫完成時進行的事情
    __weak typeof(self)weakSelf = self;
    [UIView animateKeyframesWithDuration:2 delay:1 options:UIViewKeyframeAnimationOptionAllowUserInteraction animations:^{
        // 在這裡需要添加一個方法,即創建Block的關鍵幀
        // 幀動畫的開始時間
        // 幀動畫的持續時間
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.5 animations:^{
            weakSelf.playImageView.center = weakSelf.view.center;
        }];
        
    } completion:^(BOOL finished) {
        NSLog(@"finished");
    }];

4. Spring動畫

 1> 概述

  Spring Animation 是一種特殊的動畫曲線,自從 iOS 7 開始被廣泛應用在系統動畫中。

  事實上,從 iOS 7 起幾乎所有的系統動畫都用的是 Spring Animation,包括 App 文件夾打開/關閉效果、鍵盤彈出效果、UISwitch 控制項的開關效果、不同 View Controller 之間的 Push 動畫、Modal 出現和消失的動畫、Siri 的出現和消失動畫,等等

 2> Spring動畫API

+ (void)animateWithDuration:(NSTimeInterval)duration //動畫時長參數
                      delay:(NSTimeInterval)delay //動畫延遲參數
     usingSpringWithDamping:(CGFloat)dampingRatio //動畫阻尼參數,0.0~1.0,越小動畫越明顯
      initialSpringVelocity:(CGFloat)velocity //動畫初始變化速率
                    options:(UIViewAnimationOptions)options //動畫可選參數
                 animations:(void (^)(void))animations //動畫最終效果代碼塊
                 completion:(void (^)(BOOL finished))completion //動畫播放完成後執行的代碼塊

  Spring Animation 是線性動畫或 ease-out 動畫的理想替代品。由於 iOS 本身大量使用的就是 Spring Animation,用戶已經習慣了這種動畫效果,因此使用它能使 App 讓人感覺更加自然,用 Apple 的話說就是「instantly familiar」。此外,Spring Animation 不只能針對位置變化使用,它適用於所有可被添加動畫效果的屬性

 3> 實例代碼

   __weak typeof(self)weakSelf = self; 
    [UIView animateWithDuration:3.0 // 動畫時長
                          delay:0.0 // 動畫延遲
         usingSpringWithDamping:1.0 // 類似彈簧振動效果 0~1
          initialSpringVelocity:15.0 // 初始速度                        options:UIViewAnimationOptionCurveEaseInOut // 動畫過渡效果
                     animations:^{
                         CGPoint point = _imageView.center;
                         point.y += 150;
                         [_imageView setCenter:point];
                     } completion:^(BOOL finished) {  // 動畫完成後執行
                         NSLog(@"finished");
                     }];

5. CoreAnimation動畫(CALayer動畫)

 1> CoreAnimation基本介紹

  • CoreAnimation 動畫位於 iOS 框架的 Media

  • CoreAnimation 動畫實現需要添加 QuartzCore.Framework

  • CoreAnimation 基本上是 Layer Animation

 2> CALayer基本介紹

 ① CALayer與UIView

  • CALayer 負責繪製,提供 UIView 需要展示的內容,不能交互

  • UIView 負責交互,顯示 CALayer 繪製的內容

  • UIViewiOS 系統中界面元素的基礎,所有的界面元素都是繼承自它。它本身完全是由 CoreAnimation 來實現的。它真正的繪圖部分,是由一個 CALayer 類來管理。UIView 本身更像是一個 CALayer 的管理器,訪問它的跟繪圖和跟坐標有關的屬性,例如frame,bounds等,實際上內部都是在訪問它所包含的 CALayer 的相關屬性。

  • UIView 有個重要屬性 layer ,可以返回它的主 CALayer 實例。

  • UIViewCALayer 類似 UIView子View樹形結構,也可以向它的 layer 上添加 子layer,來完成某些特殊的表示。即 CALayer 層是可以嵌套的。

 ② CALayer介紹

  • CALayer(層) 是屏幕上的一個矩形區域,在每一個 UIView 中都包含一個 根CALayer,在 UIView 上的所有視覺效果都是在這個 Layer 上進行的。

  • CALayer 外形特征主要包括:

   層的大小尺寸

   背景色

   內容(可以填充圖片或者使用Core Graphics繪製的內容)

   矩形是否使用圓角

   矩形是否有陰影

 ③ Layer的種類

  Layer 有很多種,最常用也是最基本的是 CALayer,當然還包括其他的子類:

   CAScrollerLayer 簡化顯示層的一部分

   CATextLayer 文本層

   CAGradientLayerCAShapeLayer等等

 3> CALayer的常用屬性

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 新建了一個音樂播放器,在實現seekbar時用到了Timer和TimerTask: 在OnDestory中我註銷了播放器,但沒有管Timer,所以當程式退出後Timer還在執行,程式出現了這樣的錯誤: 因為在OnDestory中我已經註銷了MediaPlayer,但Timer運行的過程中調用Medi ...
  • 對Rxjava不熟悉的同學可以先看我之前寫的幾篇文章 RxJava 和 RxAndroid 一 (基礎) RxJava 和 RxAndroid 二(操作符的使用) RxJava 和 RxAndroid 三(生命周期控制和記憶體優化) 另外推薦幾篇比較好的文章,有助於理解Rxjava 安卓客戶端是如何使 ...
  • 指定其中LinearLayout的寬度就能夠實現你GridView的長寬變化,如果它的長超過屏幕,則自動添加水平滾動條。 ...
  • 1. ERROR: While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/pod 蘋果系統升級 OS X EL Capitan 後會出現的插件錯誤,將 安裝 CocoaPods 的 sudo gem ins ...
  • 一、音頻 1、iOS 裡面共有四種專門實現播放音頻的方式: System Sound Services(系統聲音服務)。 OpenAL(跨平臺的開源的音頻處理介面)。 Audio Queue Services(播放和錄製音頻服務)。 AVAudioPlayer(高級音頻播放器)。 在這裡我們主要介紹 ...
  • // // ViewController.m // CNBlogs // // Created by PXJ on 16/5/27. // Copyright © 2016年 PXJ. All rights reserved. // #import "ViewController.h" @inter ...
  • Bitmap aa = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); InputStream input = datastream(aa);//把bitmap轉換為流 Bitmap bitmap = tra ...
  • #pragma mark - InterpolatedUIImage=因為生成的二維碼是一個CIImage,我們直接轉換成UIImage的話大小不好控制,所以使用下麵方法返回需要大小的UIImage - (UIImage *)createNonInterpolatedUIImageFormCIIma ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...