概述
几种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