iOS入門-46高級動畫

概述

幾種3D效果的動畫,這裏成爲高級動畫。高級動畫的實現原理是利用OPENGL來完成的,具體的矩陣變化是圖形學內容涉及到高數,這裏不做展開說明,只是使用。

示例

要做的準備工作

  • 引入第三方動畫庫HMGLTransitions(用CocoaPods引入依賴,具體操作請參看前面的文章)
  • 添加三個核心庫:CoreGraphics.framework、OpenGLES.framework、QuartzCore.gramework
    添加各個庫依賴之後如下圖:
    在這裏插入圖片描述

先看效果圖

switch3d:
在這裏插入圖片描述
door
在這裏插入圖片描述

cloth
在這裏插入圖片描述

示例代碼

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    //一個父控件和兩個字控件
    UIView* _parentView;
    UIImageView* _imgV01;
    UIImageView* _imgV02;
}

@end

ViewController.m

#import "ViewController.h"
//動畫管理類
#import <HMGLTransitionManager.h>
//開門3d動畫類型
#import <DoorsTransition.h>
//畫布動畫類型
#import <ClothTransition.h>
//3d變換動畫類型
#import <Switch3DTransition.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _parentView = [UIView new];
    _parentView.frame = CGRectMake(50, 50, 300, 500);
    _parentView.backgroundColor = [UIColor yellowColor];
    
    
    _imgV01 = [UIImageView new];
    UIImage* image01 = [UIImage imageNamed:@"timg_1.jpg"];
    _imgV01 = [[UIImageView alloc] initWithImage:image01];
    _imgV01.frame = CGRectMake(0, 0, 300, 500);
    
    UIImage* image02 = [UIImage imageNamed:@"timg_2.jpg"];
    _imgV02 = [[UIImageView alloc] initWithImage:image02];
    _imgV02.frame = CGRectMake(0, 0, 300, 500);
    
    [_parentView addSubview:_imgV01];
    [self.view addSubview:_parentView];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //實例化動畫管理器
    HMGLTransitionManager* manager = [HMGLTransitionManager sharedTransitionManager];
    
    //實例化3d動畫對象(動畫時間是固定的,不能我們手動調整)
    //Switch3DTransition* sAnim = [Switch3DTransition new];
    //設置動畫的方向類型
    //[sAnim setTransitionType:Switch3DTransitionRight];
    
    //開門動畫
    //DoorsTransition* dAnim = [DoorsTransition new];
    //類型:開門/關門
    //[dAnim setTransitionType:DoorsTransitionTypeOpen];
    
    //畫布類型動畫
    ClothTransition* cAnim = [ClothTransition new];
    
    //動畫添加到動畫管理器
    [manager setTransition:cAnim];
    //將目標view添加到動畫管理器中,注意這裏是給父控件添加動畫
    [manager beginTransition:_parentView];
    
    static BOOL isFirst = YES;
    if (isFirst) {
        //讓第一個view消失
        [_imgV01 removeFromSuperview];
        //設置第二個view的位置大小爲第一個view的
        _imgV02.frame = _imgV01.frame;
        //將第二個view添加到父控件中
        [_parentView addSubview:_imgV02];
    }else{
        [_imgV02 removeFromSuperview];
        
        _imgV01.frame = _imgV02.frame;
        
        [_parentView addSubview:_imgV01];
    }
    isFirst = !isFirst;
    
    //執行動畫
    [manager commitTransition];
}

@end
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章