1 演示UIImage制作的动画
1.1 问题
UIImage动画是IOS提供的最基本的动画,通常用于制作一些小型的动画,本案例使用UIImage制作一个小狗跑动的动画,如图-1所示:
图-1
1.2 方案
首先在创建好的Xcode项目的Storyboard中拖放一个ImageView控件,并关联成TRViewController的属性imageView。
然后在viewDidLoad方法中使用工厂方法animatedImageNamed:创建UIImage对象image。
最后将imageView的image属性设置为刚才创建的image对象即可。
1.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:拖放ImageView控件
首先在创建好的Xcode项目的Storyboard中拖放一个ImageView控件,并关联成TRViewController的属性imageView,代码如下所示:
- @interface TRViewController ()
- @property (weak, nonatomic) IBOutlet UIImageView *imageView;
- @end
步骤二:使用UIImage生成动画
首先导入图片素材,图片的命名规则是按顺序命名,即每张图片的名称前半部分一样后半部分是序号,例如:0_0_run_00、0_0_run_01、0_0_run_02……
然后在viewDidLoad方法中使用工厂方法animatedImageNamed:创建UIImage对象image,这里需要注意的是imageNamed参数,传入的是图片名称不带序号的部分。
最后将imageView的image属性设置为刚才创建的image对象,代码如下所示:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- UIImage *image = [UIImage animatedImageNamed:@"0_0_run_" duration:2];
- self.imageView.image = image;
- }
1.4 完整代码
本案例中,TRViewController.m文件中的完整代码如下所示:
- #import "TRViewController.h"
- @interface TRViewController ()
- @property (weak, nonatomic) IBOutlet UIImageView *imageView;
- @end
- @implementation TRViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- UIImage *image = [UIImage animatedImageNamed:@"0_0_run_" duration:2];
- self.imageView.image = image;
- }
- @end
2 使用NSTimer做一个图片淡入的效果
2.1 问题
NSTimer是一个计时器类,用于定时向指定对象发送消息,本案例使用NSTimer制作一个图片淡入的效果,即每隔一定的时间改变图片的alpha值,如图-2所示:
图-2
2.2 方案
首先在创建好的Xcode项目的Storyboard中拖放一个和屏幕一样大小的ImageView控件,并将ImageView设置为TRViewController的属性imageView。
然后在viewDidLoad方法中创建一个计时器,每隔1/30秒重复调用方法changeAlpha:。
最后实现方法changeAlpha:,每次调用都改变imageView的alpha值。
2.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:拖放ImageView控件
首先在创建好的Xcode项目的Storyboard中拖放一个和屏幕一样大小的ImageView控件,在右边栏的检查器中给ImageView设置image,并将ImageView设置为TRViewController的属性imageView,代码如下所示:
- @interface TRViewController ()
- @property (weak, nonatomic) IBOutlet UIImageView *imageView;
- @end
步骤二:创建计时器
在viewDidLoad方法中创建一个计时器,每隔1/30秒重复调用方法changeAlpha:,代码如下所示:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- [NSTimer scheduledTimerWithTimeInterval:1/FPS target:self selector:@selector(changeAlpha:) userInfo:nil repeats:YES];
- }
步骤三:实现changeAlpha:方法
首先定义两个宏FPS用来表示帧率,DURATION用来表示动画时长,在changeAlpha:方法中根据FPS和DURATION计算imageView的alpha值,代码如下所示:
- #define FPS 30.0 //帧率
- #define DURATION 5.0 //动画时长
- //当前值=开始值+当前的帧数*(结束值-开始值)/(帧率*时长)
- - (void)changeAlpha:(NSTimer *)timer
- {
- static NSUInteger count = 0;
- count++;
- self.imageView.alpha = count * 1.0/(FPS * DURATION);
- if (count >= FPS * DURATION) {
- [timer invalidate];
- }
- }
2.4 完整代码
本案例中,TRViewController.m文件中的完整代码如下所示:
- #import "TRViewController.h"
- @interface TRViewController ()
- @property (weak, nonatomic) IBOutlet UIImageView *imageView;
- @end
- @implementation TRViewController
- #define FPS 30.0 //帧率
- #define DURATION 5.0 //动画时长
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- [NSTimer scheduledTimerWithTimeInterval:1/FPS target:self selector:@selector(changeAlpha:) userInfo:nil repeats:YES];
- }
- //当前值=开始值+当前的帧数*(结束值-开始值)/(帧率*时长)
- - (void)changeAlpha:(NSTimer *)timer
- {
- static NSUInteger count = 0;
- count++;
- self.imageView.alpha = count * 1.0/(FPS * DURATION);
- if (count >= FPS * DURATION) {
- [timer invalidate];
- }
- }
- @end
3 使用UIView制作动画
3.1 问题
UIView动画是UIKit提供专门制作动画的API,其本质是对CoreAnimation的封装,使用UIView可以轻松方便的实现动画,不需要经过任何计算,本案例使用UIView制作一个飞机移动的动画,如图-3所示:
图-3
3.2 方案
首先在创建好的Xcode项目的Storyboard中拖放一个ImageView控件和一个Label控件,在右边栏的检查器中分别设置ImageView和Label的显示图片和文字,ImageView显示飞机图片,Label显示文字“Welcome To iGame“,并将ImageView和Label关联成TRViewController的属性aircraft和welcomeLabel。
然后在Storyboard中拖放一个Button对象,设置标题为Start,并关联成TRViewController的方法start:。
最后在viewDidLoad方法中使用UIView的动画方法实现aircraft和welcomeLabel的入场动画,并在方法start:中使用UIView的动画方法实现飞机的飞行动画。
3.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:搭建界面
首先在创建好的Xcode项目的Storyboard中拖放一个ImageView控件和一个Label控件,在右边栏的检查器中分别设置ImageView和Label的显示图片和文字,ImageView显示飞机图片,Label显示文字“Welcome To iGame“。
其次将ImageView和Label关联成TRViewController的属性aircraft和welcomeLabel,代码如下所示:
- @interface TRViewController ()
- @property (weak, nonatomic) IBOutlet UILabel *welcomeLabel;
- @property (weak, nonatomic) IBOutlet UIImageView *aircraft;
- @end
然后在Storyboard中拖放一个Button对象,设置标题为Start,并关联成TRViewController的方法start:。
最后Storyboard中完成的界面如图-4所示:
图-4
步骤二:使用UIView实现动画
首先在viewDidLoad方法中使用UIView的动画方法实现welcomeLabel的入场动画,welcomeLabel从屏幕的左方进入屏幕,使用UIView的动画方法animateWithDuration: delay: usingSpringWithDamping: initialSpringVelocity: options:增加弹簧效果,代码如下所示:
- - (void)viewDidAppear:(BOOL)animated
- {
- [super viewDidAppear:animated];
- //结束位置
- CGRect endFrame = self.welcomeLabel.frame;
- //开始位置
- CGRect startFrame = endFrame;
- startFrame.origin.x = - startFrame.size.width;
- //welcome的入场动画
- self.welcomeLabel.frame = startFrame;
- self.welcomeLabel.alpha = 0.1;
- [UIView animateWithDuration:5.0 delay:0.3 usingSpringWithDamping:0.1 initialSpringVelocity:5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
- self.welcomeLabel.frame = endFrame;
- self.welcomeLabel.alpha = 1.0;
- } completion:^(BOOL finished) {
- NSLog(@"动画结束");
- }];
- }
然后实现飞机的入场动画,飞机从屏幕的下方飞入屏幕,代码如下所示:
- - (void)viewDidAppear:(BOOL)animated
- {
- [super viewDidAppear:animated];
- //结束位置
- CGRect endFrame = self.welcomeLabel.frame;
- //开始位置
- CGRect startFrame = endFrame;
- startFrame.origin.x = - startFrame.size.width;
- //welcome的入场动画
- self.welcomeLabel.frame = startFrame;
- self.welcomeLabel.alpha = 0.1;
- [UIView animateWithDuration:5.0 delay:0.3 usingSpringWithDamping:0.1 initialSpringVelocity:5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
- self.welcomeLabel.frame = endFrame;
- self.welcomeLabel.alpha = 1.0;
- } completion:^(BOOL finished) {
- NSLog(@"动画结束");
- }];
- //飞机的入场动画
- endFrame = self.aircraft.frame;
- startFrame = endFrame;
- startFrame.origin.y = self.view.bounds.size.height;
- self.aircraft.frame = startFrame;
- [UIView animateWithDuration:2.0 animations:^{
- self.aircraft.frame = endFrame;
- }];
- }
最后实现方法start:完成飞机的飞行动画,飞机从屏幕下方飞到屏幕上方,并且在动画结束之后反复在屏幕中飞行,代码如下所示:
- - (IBAction)start:(UIButton *)sender
- {
- CGPoint point = self.aircraft.center;
- point.y = 80;
- [UIView animateWithDuration:3.0 delay:1.0 options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse animations:^{
- self.aircraft.center = point;
- } completion:^(BOOL finished) {
- NSLog(@"动画结束");
- }];
- }
3.4 完整代码
本案例中,TRViewController.m文件中的完整代码如下所示:
- #import "TRViewController.h"
- @interface TRViewController ()
- @property (weak, nonatomic) IBOutlet UILabel *welcomeLabel;
- @property (weak, nonatomic) IBOutlet UIImageView *aircraft;
- @end
- @implementation TRViewController
- - (void)viewDidAppear:(BOOL)animated
- {
- [super viewDidAppear:animated];
- //结束位置
- CGRect endFrame = self.welcomeLabel.frame;
- //开始位置
- CGRect startFrame = endFrame;
- startFrame.origin.x = - startFrame.size.width;
- //welcome的入场动画
- self.welcomeLabel.frame = startFrame;
- self.welcomeLabel.alpha = 0.1;
- [UIView animateWithDuration:5.0 delay:0.3 usingSpringWithDamping:0.1 initialSpringVelocity:5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
- self.welcomeLabel.frame = endFrame;
- self.welcomeLabel.alpha = 1.0;
- } completion:^(BOOL finished) {
- NSLog(@"动画结束");
- }];
- //飞机的入场动画
- endFrame = self.aircraft.frame;
- startFrame = endFrame;
- startFrame.origin.y = self.view.bounds.size.height;
- self.aircraft.frame = startFrame;
- [UIView animateWithDuration:2.0 animations:^{
- self.aircraft.frame = endFrame;
- }];
- }
- - (IBAction)start:(UIButton *)sender
- {
- CGPoint point = self.aircraft.center;
- point.y = 80;
- [UIView animateWithDuration:3.0 delay:1.0 options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse animations:^{
- self.aircraft.center = point;
- } completion:^(BOOL finished) {
- NSLog(@"动画结束");
- }];
- }
- @end
4 CALayer的简单应用
4.1 问题
任何UIView及子类对象都有一个属性叫layer,此属性就是UIView的CoreAnimation层,类型是CALayer,本案例演示CALayer如何使用,如图-5所示:
图-5
4.2 方案
首先在创建好的Xcode项目的Storyboard中拖放一个ImageView控件,在右边栏的检查器中设置ImageView的显示图片,并将ImageView和Label关联成TRViewController的属性imageView。
然后在viewDidLoad方法中设置self.view的layer的背景颜色和圆角。再设置imageView的layer属性,将layer设置完圆角之后要将遮罩打开。
最后在viewDidLoad方法中分别给self.view.layer添加一个无图片的子层和有图片的子层。
4.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:搭建Storyboard界面
首先在创建好的Xcode项目的Storyboard中拖放一个ImageView控件,在右边栏的检查器中设置ImageView的显示图片,并将ImageView和Label关联成TRViewController的属性imageView,代码如下所示:
- @interface TRViewController ()
- @property (weak, nonatomic) IBOutlet UIImageView *imageView;
- @end
步骤二:设置layer属性
首先在viewDidLoad方法中设置self.view的layer的背景颜色和圆角,代码如下所示:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //设置view的layer
- CALayer *layer = self.view.layer;
- layer.backgroundColor = [[UIColor orangeColor] CGColor];
- layer.cornerRadius = 30.0;
- }
运行程序发现背景变成圆角的,如图-6所示:
图-6
其次再设置imageView的layer属性,将layer设置完圆角之后要将遮罩打开,代码如下所示:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //设置view的layer
- CALayer *layer = self.view.layer;
- layer.backgroundColor = [[UIColor orangeColor] CGColor];
- layer.cornerRadius = 30.0;
- //将imageView设置成圆角
- self.imageView.layer.cornerRadius = 10.0;
- //打开遮罩,否则无效
- self.imageView.layer.masksToBounds =YES;
- }
然后在viewDidLoad方法中给self.view.layer添加一个无图片的子层,代码如下所示:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //设置view的layer
- CALayer *layer = self.view.layer;
- layer.backgroundColor = [[UIColor orangeColor] CGColor];
- layer.cornerRadius = 30.0;
- //将imageView设置成圆角
- self.imageView.layer.cornerRadius = 10.0;
- //打开遮罩,否则无效
- self.imageView.layer.masksToBounds =YES;
- //加子层
- CALayer *subLayer = [CALayer layer];
- subLayer.backgroundColor = [UIColor purpleColor].CGColor;
- subLayer.frame = CGRectMake(30, 200, 100, 120);
- //设置阴影
- subLayer.shadowColor = [UIColor greenColor].CGColor;
- subLayer.shadowOffset = CGSizeMake(2, 2);
- subLayer.shadowRadius = 5.0;
- subLayer.shadowOpacity = 0.8;
- //设置圆角
- subLayer.cornerRadius = 10.0;
- [layer addSublayer:subLayer];
- }
最后在viewDidLoad方法中给self.view.layer添加一个有图片的子层,代码如下所示:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //设置view的layer
- CALayer *layer = self.view.layer;
- layer.backgroundColor = [[UIColor orangeColor] CGColor];
- layer.cornerRadius = 30.0;
- //将imageView设置成圆角
- self.imageView.layer.cornerRadius = 10.0;
- //打开遮罩,否则无效
- self.imageView.layer.masksToBounds =YES;
- //加子层
- CALayer *subLayer = [CALayer layer];
- subLayer.backgroundColor = [UIColor purpleColor].CGColor;
- subLayer.frame = CGRectMake(30, 200, 100, 120);
- subLayer.shadowColor = [UIColor greenColor].CGColor;
- subLayer.shadowOffset = CGSizeMake(2, 2);
- subLayer.shadowRadius = 5.0;
- subLayer.shadowOpacity = 0.8;
- subLayer.cornerRadius = 10.0;
- [layer addSublayer:subLayer];
- //加有内容的子层
- CALayer *imageLayer = [CALayer new];
- imageLayer.frame = CGRectMake(180, 300, 100, 120);
- //设置层的内容
- imageLayer.contents = (id)[UIImage imageNamed:@"d.jpg"].CGImage;
- imageLayer.cornerRadius = 10.0;
- imageLayer.masksToBounds = YES;
- [layer addSublayer:imageLayer];
- }
4.4 完整代码
本案例中,TRViewController.m文件中的完整代码如下所示:
- #import "TRViewController.h"
- @interface TRViewController ()
- @property (weak, nonatomic) IBOutlet UIImageView *imageView;
- @end
- @implementation TRViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //设置view的layer
- CALayer *layer = self.view.layer;
- layer.backgroundColor = [[UIColor orangeColor] CGColor];
- layer.cornerRadius = 30.0;
- //将imageView设置成圆角
- self.imageView.layer.cornerRadius = 10.0;
- //打开遮罩,否则无效
- self.imageView.layer.masksToBounds =YES;
- //加子层
- CALayer *subLayer = [CALayer layer];
- subLayer.backgroundColor = [UIColor purpleColor].CGColor;
- subLayer.frame = CGRectMake(30, 200, 100, 120);
- subLayer.shadowColor = [UIColor greenColor].CGColor;
- subLayer.shadowOffset = CGSizeMake(2, 2);
- subLayer.shadowRadius = 5.0;
- subLayer.shadowOpacity = 0.8;
- subLayer.cornerRadius = 10.0;
- [layer addSublayer:subLayer];
- //加有内容的子层
- CALayer *imageLayer = [CALayer new];
- imageLayer.frame = CGRectMake(180, 300, 100, 120);
- imageLayer.contents = (id)[UIImage imageNamed:@"d.jpg"].CGImage;
- imageLayer.cornerRadius = 10.0;
- imageLayer.masksToBounds = YES;
- [layer addSublayer:imageLayer];
- }
- @end
5 使用CoreAnimation制作动画
5.1 问题
CoreAnimation是一个图形渲染和动画的底层框架,能够提供更多更强大的图形渲染显示效果。本案例使用CoreAnimation的关键帧动画方法和基础动画方法制作动画,如图-7所示:
图-7
5.2 方案
首先在创建好的Xcode项目的Storyboard中拖放一个ImageView控件和一个Button控件,在右边栏的检查器中设置ImageView和Button的属性,并将ImageView关联成TRAnimationViewController的属性imageView,将Button关联成TRAnimationViewController的方法start:。
然后在viewDidLoad方法中设置imageView的layer属性,将imageView设置成圆角。
最后实现start:方法,当点击按钮imageView开始执行动画。首先制定一个imageView的动画路径path,是一个UIBezierPath类型,然后分别创建关键帧动画、缩放动画以及透明度改变动画,最后将以上动画放进一个动画群组里面,即CAAnimationGroup类型的group,并将动画添加到imageView的layer中。
5.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:搭建Storyboard界面
首先在创建好的Xcode项目的Storyboard中拖放一个ImageView控件和一个Button控件,在右边栏的检查器中设置ImageView和Button的属性,并将ImageView关联成TRAnimationViewController的属性imageView,将Button关联成TRAnimationViewController的方法start:,代码如下所示:
- @interface TRAnimationViewController ()
- @property (weak, nonatomic) IBOutlet UIImageView *imageView;
- @end
- - (IBAction)start:(id)sender
- {
- }
步骤二:实现imageView的移动动画
首先在viewDidLoad方法中设置imageView的layer属性,将imageView设置成圆角,代码如下所示:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //设置imageView的layer属性
- self.imageView.layer.cornerRadius = 8.0;
- self.imageView.layer.masksToBounds = YES;
- }
然后实现start:方法,当点击按钮imageView开始执行动画。首先制定一个imageView的动画路径path,是一个UIBezierPath类型,代码如下所示:
- - (IBAction)start:(id)sender
- {
- UIBezierPath *path = [UIBezierPath bezierPath];
- [self createCurve:path];
- }
- - (IBAction)start:(id)sender
- {
- UIBezierPath *path = [UIBezierPath bezierPath];
- [self createCurve:path];
- //创建关键帧
- CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
- //设置相关属性
- moveAnimation.path = path.CGPath;
- moveAnimation.removedOnCompletion = YES;
- }
然后创建关键帧动画,并设置相关属性,代码如下所示:
- - (IBAction)start:(id)sender
- {
- UIBezierPath *path = [UIBezierPath bezierPath];
- [self createCurve:path];
- //创建关键帧
- CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
- //设置相关属性
- moveAnimation.path = path.CGPath;
- moveAnimation.removedOnCompletion = YES;
- }
再创建缩放动画以及透明度改变动画,代码如下所示:
- - (IBAction)start:(id)sender
- {
- UIBezierPath *path = [UIBezierPath bezierPath];
- [self createCurve:path];
- //创建关键帧
- CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
- //设置相关属性
- moveAnimation.path = path.CGPath;
- moveAnimation.removedOnCompletion = YES;
- //创建缩放动画
- CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
- scaleAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
- scaleAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
- scaleAnimation.removedOnCompletion = YES;
- //创建透明度动画
- CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
- alphaAnimation.fromValue = @1.0;
- alphaAnimation.toValue = @0.0;
- alphaAnimation.removedOnCompletion = YES;
- }
最后将以上动画放进一个动画群组里面,即CAAnimationGroup类型的group,并将动画添加到imageView的layer中,代码如下所示:
- - (IBAction)start:(id)sender
- {
- UIBezierPath *path = [UIBezierPath bezierPath];
- [self createCurve:path];
- //创建关键帧
- CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
- //设置相关属性
- moveAnimation.path = path.CGPath;
- moveAnimation.removedOnCompletion = YES;
- //创建缩放动画
- CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
- scaleAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
- scaleAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
- scaleAnimation.removedOnCompletion = YES;
- //创建透明度动画
- CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
- alphaAnimation.fromValue = @1.0;
- alphaAnimation.toValue = @0.0;
- alphaAnimation.removedOnCompletion = YES;
- //将以上动画放进一个动画组
- CAAnimationGroup *group = [CAAnimationGroup animation];
- group.animations = @[moveAnimation, scaleAnimation, alphaAnimation];
- group.duration = 2.0;
- group.delegate = self;
- //加入到Layer中
- [self.imageView.layer addAnimation:group forKey:nil];
- }
实现animation的生命周期方法,当动画结束时将imageView从界面上移除,代码如下所示:
- - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
- {
- [self.imageView removeFromSuperview];
- }
5.4 完整代码
本案例中,TRAnimationViewController.m文件中的完整代码如下所示:
- #import "TRAnimationViewController.h"
- @interface TRAnimationViewController ()
- @property (weak, nonatomic) IBOutlet UIImageView *imageView;
- @end
- @implementation TRAnimationViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //设置imageView的layer属性
- self.imageView.layer.cornerRadius = 8.0;
- self.imageView.layer.masksToBounds = YES;
- }
- //创建图片运动的曲线
- - (void)createCurve:(UIBezierPath *)path
- {
- [path moveToPoint:self.imageView.center];
- CGPoint targetPoint = CGPointMake(self.view.bounds.size.width - self.imageView.frame.size.width - 20, self.view.bounds.size.height - self.imageView.frame.size.height - 20);
- CGPoint control1 = CGPointMake(self.view.bounds.size.width - self.imageView.frame.size.width - 20, self.imageView.frame.origin.y);
- CGPoint control2 = CGPointMake(self.imageView.frame.origin.x, self.view.bounds.size.height - self.imageView.frame.size.height - 20);
- [path addCurveToPoint:targetPoint controlPoint1:control1 controlPoint2:control2];
- }
- - (IBAction)start:(id)sender
- {
- UIBezierPath *path = [UIBezierPath bezierPath];
- [self createCurve:path];
- //创建关键帧
- CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
- //设置相关属性
- moveAnimation.path = path.CGPath;
- moveAnimation.removedOnCompletion = YES;
- //创建缩放动画
- CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
- scaleAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
- scaleAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
- scaleAnimation.removedOnCompletion = YES;
- //创建透明度动画
- CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
- alphaAnimation.fromValue = @1.0;
- alphaAnimation.toValue = @0.0;
- alphaAnimation.removedOnCompletion = YES;
- //将以上动画放进一个动画组
- CAAnimationGroup *group = [CAAnimationGroup animation];
- group.animations = @[moveAnimation, scaleAnimation, alphaAnimation];
- group.duration = 2.0;
- group.delegate = self;
- //加入到Layer中
- [self.imageView.layer addAnimation:group forKey:nil];
- }
- - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
- {
- [self.imageView removeFromSuperview];
- }
- @end
6 使用CATransform3D制作3D动画
6.1 问题
CATransform3D是一个结构体,是一个4x4的矩阵,用于描述一个3D图形的变形,包括旋转、缩放以及位移,本案例使用CATransform3D制作3D旋转动画,如图-8所示:
图-8
6.2 方案
首先在创建好的Xcode项目的Storyboard中拖放一个ImageView控件和四个Button控件,在右边栏的检查器中设置ImageView和Button的属性,并将ImageView关联成TRRotationViewController的属性imageView。
再将四个Button关联成TRRotationViewController的四个方法rotationX:、rotationY:、rotationZ:以及rotation:。
然后在viewDidLoad方法中设置imageView的layer属性,将imageView设置成圆角。
最后实现四个Button的动作方法,rotationX:实现功能围绕X轴旋转,rotationY:实现功能围绕Y轴旋转,rotationZ:实现功能围绕Z轴旋转,rotation:实现功能围绕着XYZ轴同时旋转。
6.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:搭建Storyboard界面
首先在创建好的Xcode项目的Storyboard中拖放一个ImageView控件和四个Button控件,在右边栏的检查器中设置ImageView和Button的属性,并将ImageView关联成TRRotationViewController的属性imageView,代码如下所示:
- @interface TRViewController ()
- @property (weak, nonatomic) IBOutlet UIImageView *imageView;
- @end
Storyboard中界面完成的结果如图-9所示:
图-9
步骤二:实现3D旋转动画
首先将四个Button关联成TRRotationViewController的四个方法rotationX:、rotationY:、rotationZ:以及rotation:,
在viewDidLoad方法中设置imageView的layer属性,将imageView设置成圆角,代码如下所示:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.imageView.layer.cornerRadius = 8.0;
- self.imageView.layer.masksToBounds = YES;
- }
然后实现四个Button的动作方法,在rotationX:方法中通过layer的transform属性,实现功能围绕X轴旋转,然后使用类似的方法实现方法rotationY:和rotationZ:,代码如下所示:
- - (IBAction)rotationX:(id)sender
- {
- //创建基本动画
- CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
- transformAnimation.fromValue = [NSValue valueWithCATransform3D:self.imageView.layer.transform];
- //计算目标值
- transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.imageView.layer.transform, M_PI, 1.0, 0, 0)];
- transformAnimation.duration = 3.0;
- //将动画添加到imageView的layer上面
- [self.imageView.layer addAnimation:transformAnimation forKey:nil];
- }
- - (IBAction)rotationY:(id)sender
- {
- CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
- transformAnimation.fromValue = [NSValue valueWithCATransform3D:self.imageView.layer.transform];
- transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.imageView.layer.transform, M_PI, 0.0, 1.0, 0)];
- transformAnimation.duration = 3.0;
- [self.imageView.layer addAnimation:transformAnimation forKey:nil];
- }
- - (IBAction)rotationZ:(id)sender
- {
- CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
- transformAnimation.fromValue = [NSValue valueWithCATransform3D:self.imageView.layer.transform];
- transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.imageView.layer.transform, M_PI, 0.0, 0.0, 1.0)];
- transformAnimation.duration = 3.0;
- [self.imageView.layer addAnimation:transformAnimation forKey:nil];
- }
最后实现rotation:方法,该方法实现功能围绕着XYZ轴同时旋转,代码如下所示:
- - (IBAction)rotation:(id)sender
- {
- CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
- transformAnimation.fromValue = [NSValue valueWithCATransform3D:self.imageView.layer.transform];
- transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.imageView.layer.transform, M_PI, 1.0, 1.0, 1.0)];
- transformAnimation.duration = 3.0;
- transformAnimation.delegate = self;
- [self.imageView.layer addAnimation:transformAnimation forKey:nil];
- }
实现animation的生命周期方法,当rotation动画结束时将imageView的transform属性值保持不变,从界面上移除,代码如下所示:
- - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
- {
- self.imageView.layer.transform = CATransform3DRotate(self.imageView.layer.transform, M_PI, 1.0, 1.0, 1.0);
- }
6.4 完整代码
本案例中,TRRotationViewController.m文件中的完整代码如下所示:
- #import "TRRotationViewController.h"
- @interface TRRotationViewController ()
- @property (weak, nonatomic) IBOutlet UIImageView *imageView;
- @end
- @implementation TRRotationViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.imageView.layer.cornerRadius = 8.0;
- self.imageView.layer.masksToBounds = YES;
- }
- - (IBAction)rotationX:(id)sender
- {
- //创建基本动画
- CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
- transformAnimation.fromValue = [NSValue valueWithCATransform3D:self.imageView.layer.transform];
- //计算目标值
- transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.imageView.layer.transform, M_PI, 1.0, 0, 0)];
- transformAnimation.duration = 3.0;
- //将动画添加到imageView的layer上面
- [self.imageView.layer addAnimation:transformAnimation forKey:nil];
- }
- - (IBAction)rotationY:(id)sender
- {
- CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
- transformAnimation.fromValue = [NSValue valueWithCATransform3D:self.imageView.layer.transform];
- transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.imageView.layer.transform, M_PI, 0.0, 1.0, 0)];
- transformAnimation.duration = 3.0;
- [self.imageView.layer addAnimation:transformAnimation forKey:nil];
- }
- - (IBAction)rotationZ:(id)sender
- {
- CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
- transformAnimation.fromValue = [NSValue valueWithCATransform3D:self.imageView.layer.transform];
- transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.imageView.layer.transform, M_PI, 0.0, 0.0, 1.0)];
- transformAnimation.duration = 3.0;
- [self.imageView.layer addAnimation:transformAnimation forKey:nil];
- }
- - (IBAction)rotation:(id)sender
- {
- CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
- transformAnimation.fromValue = [NSValue valueWithCATransform3D:self.imageView.layer.transform];
- transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.imageView.layer.transform, M_PI, 1.0, 1.0, 1.0)];
- transformAnimation.duration = 3.0;
- transformAnimation.delegate = self;
- [self.imageView.layer addAnimation:transformAnimation forKey:nil];
- }
- - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
- {
- self.imageView.layer.transform = CATransform3DRotate(self.imageView.layer.transform, M_PI, 1.0, 1.0, 1.0);
- }
- @end
动画(Animation) 、 高级动画(Core Animation)的更多相关文章
-
iOS动画-从UIView到Core Animation
首先,介绍一下UIView相关的动画. UIView普通动画: [UIView beginAnimations: context:]; [UIView commitAnimations]; 动画属性设 ...
-
简析iOS动画原理及实现——Core Animation
本文转载至 http://www.tuicool.com/articles/e2qaYjA 原文 https://tech.imdada.cn/2016/06/21/ios-core-animati ...
-
IOS动画(Core Animation)总结 (参考多方文章)
一.简介 iOS 动画主要是指Core Animation框架.官方使用文档地址为:Core Animation Guide. Core Animation是IOS和OS X平台上负责图形渲染与动画的 ...
-
核心动画——Core Animation
一. CALayer (一). CALayer简单介绍 在iOS中,你能看得见摸得着的东西基本上都是UIView,比方一个button.一个文本标签.一个文本输入框.一个图标等等.这些都是UIView ...
-
Core Animation编程指南
本文是<Core Animation Programming Guide>2013-01-28更新版本的译文.本文略去了原文中关于OS X平台上Core Animation相关内容.因为原 ...
-
Core Animation学习总结
文件夹: The Layer Beneath The Layer Tree(图层树) The Backing Image(寄宿层) Layer Geometry(图层几何学) Visual Effec ...
-
说说Core Animation
前言 本次分享将从以下方面进行展开: 曾被面试官问倒过的问题:层与视图的关系 CALayer类介绍及层与视图的关系 CAShapeLayer类介绍 UIBezierPath贝塞尔曲线讲解 CoreAn ...
-
Core Animation Manages Your App’s Content
Core Animation 是对现有图片的操作函数. https://developer.apple.com/library/content/documentation/Cocoa/Conceptu ...
-
iOS动画1 — UIView动画
iOS动画基础是Core Animation核心动画.Core Animation是iOS平台上负责图形渲染与动画的基础设施.由于核心动画的实现比较复杂,苹果提供了实现简单动画的接口—UIView动画 ...
-
iOS核心动画以及UIView动画的介绍
我们看到很多App带有绚丽狂拽的特效,别出心裁的控件设计,很大程度上提高了用户体验,在增加了实用性的同时,也赋予了app无限的生命力.这些华丽的效果很多都是基于iOS的核心动画原理实现的,本文介绍一些 ...
随机推荐
-
从linux系统mysql导出数据库
原文:http://blog.csdn.net/lifuxiangcaohui/article/details/50763674 1.MySQL数据库导出 /usr/local/mysql/bin/m ...
-
C++ 中的C_str()函数用法
语法: const char *c_str(); c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同. 这是为了与c语言兼容,在c语言中没有string类型,故必须通过 ...
-
如何解决加载动态链接库DLL失败,返回0,GetLastError返回错误码126
通常情况下使用LoadLibrary加载DLL都可以成功,但是当被加载的DLL内部依赖其他DLL无法被找到时,该函数会返回126(ERROR_MOD_NOT_FOUND)错误. 解决办法有2种: 1) ...
-
Slider 滑动条效果
转载自:http://www.cnblogs.com/cloudgamer/archive/2008/12/24/Slider.html 这个滑动条(拖动条)效果,一开始是参考了BlueDestiny ...
-
使用开源库PhotoView
一.下载地址:https://github.com/chrisbanes/PhotoView 点击DownLoad ZIP,进行下载. 二.解压,然后复制PhotoView-master\librar ...
-
C#微信公众号开发 -- (七)自定义菜单事件之VIEW及网页(OAuth2.0)授权
通俗来讲VIEW其实就是我们在C#中常用的a标签,可以直接在自定义菜单URL的属性里面写上需要跳转的链接,也即为单纯的跳转. 但更多的情况下,我们是想通过VIEW来进入指定的页面并进行操作. 举一个简 ...
-
C++ - Identifier not found
This is because forward declaration in C++: Compiler needs to know function prototype when functi ...
-
Design Principle, Design Patterns And Refactoring
https://refactoring.guru/smells/feature-envy https://*.com/questions/1242994/effective-c ...
-
【原创】大数据基础之Logstash(3)应用之http(in和out)
一个logstash很容易通过http打断成两个logstash实现跨服务器或者跨平台间数据同步,比如原来的流程是 logstash: nginx log -> kafka 打断成两个是 log ...
-
Kubernetes 1.12公布:Kubelet TLS Bootstrap与Azure虚拟机规模集(VMSS)迎来通用版本号
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/M2l0ZgSsVc7r69eFdTj/article/details/82880341 https: ...