前面介绍了Core Animation基础知识,还有CALayer的简单使用,最终还是有要动画的滴,这里列出几个动画效果,参考下能加深对Core Animation的认识和理解

1、把图片移到右下角变小透明

使用CAAnimationGroup叠加动画效果,就是下面按钮《把图片移到右下角变小透明》描述的效果:

     

上面三个图是动画的三个状态,实现代码如下:

- (void)viewDidLoad
{
[super viewDidLoad];
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"snaguosha.png"]];
self.imageView.frame = CGRectMake(10, 10, 128, 192);
[self.view addSubview:self.imageView];
}
- (IBAction)tranAction:(id)sender {
CGPoint fromPoint = self.imageView.center; //路径曲线
UIBezierPath *movePath = [UIBezierPath bezierPath];
[movePath moveToPoint:fromPoint];
CGPoint toPoint = CGPointMake(300, 460);
[movePath addQuadCurveToPoint:toPoint
controlPoint:CGPointMake(300,0)];
//关键帧
CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnim.path = movePath.CGPath;
moveAnim.removedOnCompletion = YES; //旋转变化
CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
//x,y轴缩小到0.1,Z 轴不变
scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
scaleAnim.removedOnCompletion = YES; //透明度变化
CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"alpha"];
opacityAnim.fromValue = [NSNumber numberWithFloat:1.0];
opacityAnim.toValue = [NSNumber numberWithFloat:0.1];
opacityAnim.removedOnCompletion = YES; //关键帧,旋转,透明度组合起来执行
CAAnimationGroup *animGroup = [CAAnimationGroup animation];
animGroup.animations = [NSArray arrayWithObjects:moveAnim, scaleAnim,opacityAnim, nil];
animGroup.duration = 1;
[self.imageView.layer addAnimation:animGroup forKey:nil];
}

代码解析:上面关键帧设置了动画的路径,scaleAnim设置了缩小,opacityAnim设置了透明度的变化。把三个动画组合到:animGroup。

在把animGroup添加到imageView.layer层的动画里。于是动画效果就有了。

2、旋转并向右移动

- (IBAction)RightRotateAction:(id)sender {
CGPoint fromPoint = self.imageView.center;
UIBezierPath *movePath = [UIBezierPath bezierPath];
[movePath moveToPoint:fromPoint];
CGPoint toPoint = CGPointMake(fromPoint.x +100 , fromPoint.y ) ;
[movePath addLineToPoint:toPoint]; CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnim.path = movePath.CGPath; CABasicAnimation *TransformAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
TransformAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; //沿Z轴旋转
TransformAnim.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI,0,0,1)]; //沿Y轴旋转
// scaleAnim.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI,0,1.0,0)]; //沿X轴旋转
// TransformAnim.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI,1.0,0,0)];
TransformAnim.cumulative = YES;
TransformAnim.duration =3;
//旋转2遍,360度
TransformAnim.repeatCount =2;
self.imageView.center = toPoint;
TransformAnim.removedOnCompletion = YES;
CAAnimationGroup *animGroup = [CAAnimationGroup animation];
animGroup.animations = [NSArray arrayWithObjects:moveAnim, TransformAnim, nil];
animGroup.duration = 6; [self.imageView.layer addAnimation:animGroup forKey:nil];
}

代码解析:可能你没注意到,CATransform3DMakeRotation,这返回的是旋转的值。上面的动画效果里返回的是CATransform3DMakeScale缩放的值。
向右移动是因为关键帧使用了路径为直线的路径。

3、旋转并消除边缘锯齿

- (IBAction)Rotate360Action:(id)sender {
//图片旋转360度
CABasicAnimation *animation = [ CABasicAnimation
animationWithKeyPath: @"transform" ];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; //围绕Z轴旋转,垂直与屏幕
animation.toValue = [ NSValue valueWithCATransform3D:
CATransform3DMakeRotation(M_PI, 0, 0, 1.0) ];
animation.duration = 3;
//旋转效果累计,先转180度,接着再旋转180度,从而实现360旋转
animation.cumulative = YES;
animation.repeatCount = 2; //在图片边缘添加一个像素的透明区域,去图片锯齿
CGRect imageRrect = CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height);
UIGraphicsBeginImageContext(imageRrect.size);
[self.imageView.image drawInRect:CGRectMake(1,1,self.imageView.frame.size.width-2,self.imageView.frame.size.height-2)];
self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); [self.imageView.layer addAnimation:animation forKey:nil];
}

如果你仔细观察,会看到第二个动画里在旋转时,图片边缘是有锯齿的,如何消除呢?在图片边缘添加一个像素的透明区域,去图片锯齿。

UIGraphicsBeginImageContext 开始图片内容

UIGraphicsGetImageFromCurrentImageContext 获取当前内容作为图片,

UIGraphicsEndImageContext结束。是和UIGraphicsBeginImageContext配套使用的。

4、吃豆人动画

这个有点复杂,首先说下实现的步骤:
  1. 画一个吃豆人开口的路径:pacmanOpenPath
  2. 画一个吃豆人闭口的路径:pacmanClosedPath
  3. 新建一个闭口的吃豆人头的层:shapeLayer
  4. 把开口和闭口路径设置成CABasicAnimation *chompAnimation动画的起点和终点,这样循环就能出现咬牙的动画了。
  5. 最后设置一个路径为关键帧,让吃豆人在这条路径上行动。
代码如下:
- (void)animationInit
{
self.view.backgroundColor = [UIColor blackColor]; CGFloat radius = 30.0f;
CGFloat diameter = radius * 2;
CGPoint arcCenter = CGPointMake(radius, radius);
// Create a UIBezierPath for Pacman's open state
pacmanOpenPath = [UIBezierPath bezierPathWithArcCenter:arcCenter
radius:radius
startAngle:DEGREES_TO_RADIANS(35)
endAngle:DEGREES_TO_RADIANS(315)
clockwise:YES]; [pacmanOpenPath addLineToPoint:arcCenter];
[pacmanOpenPath closePath]; // Create a UIBezierPath for Pacman's close state
pacmanClosedPath = [UIBezierPath bezierPathWithArcCenter:arcCenter
radius:radius
startAngle:DEGREES_TO_RADIANS(1)
endAngle:DEGREES_TO_RADIANS(359)
clockwise:YES];
[pacmanClosedPath addLineToPoint:arcCenter];
[pacmanClosedPath closePath]; // Create a CAShapeLayer for Pacman, fill with yellow
shapeLayer = [CAShapeLayer layer];
shapeLayer.fillColor = [UIColor yellowColor].CGColor;
shapeLayer.path = pacmanClosedPath.CGPath;
shapeLayer.strokeColor = [UIColor grayColor].CGColor;
shapeLayer.lineWidth = 1.0f;
shapeLayer.bounds = CGRectMake(0, 0, diameter, diameter);
shapeLayer.position = CGPointMake(-40, -100);
[self.view.layer addSublayer:shapeLayer]; SEL startSelector = @selector(startAnimation);
UIGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:startSelector];
[self.view addGestureRecognizer:recognizer];
}
- (void)startAnimation {
// 创建咬牙动画
CABasicAnimation *chompAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
chompAnimation.duration = 0.25;
chompAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
chompAnimation.repeatCount = HUGE_VALF;
chompAnimation.autoreverses = YES;
// Animate between the two path values
chompAnimation.fromValue = (id)pacmanClosedPath.CGPath;
chompAnimation.toValue = (id)pacmanOpenPath.CGPath;
[shapeLayer addAnimation:chompAnimation forKey:@"chompAnimation"]; // Create digital '2'-shaped path UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 100)];
[path addLineToPoint:CGPointMake(300, 100)];
[path addLineToPoint:CGPointMake(300, 200)];
[path addLineToPoint:CGPointMake(0, 200)];
[path addLineToPoint:CGPointMake(0, 300)];
[path addLineToPoint:CGPointMake(300, 300)]; CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnimation.path = path.CGPath;
moveAnimation.duration = 8.0f;
// Setting the rotation mode ensures Pacman's mouth is always forward. This is a very convenient CA feature.
moveAnimation.rotationMode = kCAAnimationRotateAuto;
[shapeLayer addAnimation:moveAnimation forKey:@"moveAnimation"];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self animationInit];
}

还需要添加一个宏:

#define DEGREES_TO_RADIANS(x) (3.14159265358979323846 * x / 180.0)    计算角度转换

添加了个手势,点一下屏幕,吃豆人就动起来了。效果:

原作者,容芳志 (http://blog.csdn.net/totogo2010)

Core Animation之多种动画效果的更多相关文章

  1. 实用CSS3的transform实现多种动画效果

    查看效果:http://keleyi.com/a/bjad/b6x9q8gs.htm 以下是代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4. ...

  2. android动画介绍--Animation 实现loading动画效果

    Animation的使用方法并不难.这里简单的介绍一下使用方法. 先看效果图: 效果还是不错的吧. 下面来看看使用方法. 动画效果是通过Animation来实现的,一共有四种,分别为: AlphaAn ...

  3. android xml实现animation 4种动画效果

    animation有四种动画类型 分别为alpha(透明的渐变).rotate(旋转).scale(尺寸伸缩).translate(移动),二实现的分发有两种,一种是javaCode,另外一种是XML ...

  4. Core Animation笔记(动画)

    一.隐式动画 layer默认开启隐式动画 禁用隐式动画 [CATransaction setDisableActions:true]; 设置隐士动画时间 //默认0.25s [CATransactio ...

  5. core Animation之CAAnimationGroup(动画群组)

    可以保存一组动画对象,将CAAnimationGroup对象加入层后,组中所有动画对象可以同时并发运行 属性解析: animations:用来保存一组动画对象的NSArray 默认情况下,一组动画对象 ...

  6. iOS仿写有妖气漫画、视频捕获框架、启动页广告页demo、多种动画效果等源码

    iOS精选源码 以tableview的section为整体添加阴影效果/ta'b'le'vi'e'w顶部悬浮.... 一个可以轻松应用自定义过滤器的视频捕获框架. 基于UITableView的组件,旨 ...

  7. Core Animation一些Demo总结 (动态切换图片、大转盘、图片折叠、进度条等动画效果)

    前一篇总结了Core Animation的一些基础知识,这一篇主要是Core Animation 的一些应用,涉及到CAShapeLayer.CAReplicatorLayer等图层的知识. 先看效果 ...

  8. iOS 动画效果:Core Animation & Facebook's pop

    本文转载至 http://www.cocoachina.com/ios/20151223/14739.html 感谢原创作者分享 前言相信很多人对实现 iOS 中的动画效果都特别头疼,往往懒得动手,功 ...

  9. Core Animation 动画的使用:关键帧动画、基础动画、动画组

    首先让我们了解下什么是 Core Animation,Core Animation 为核心动画,他为图形渲染和动画提供了基础.使用核心动画,我们只需要设置起点.终点.关键帧等一些参数,剩下的工作核心动 ...

随机推荐

  1. 数据库索引的实现原理(笔记)详细http://www.linezing.com/blog/?p=798#nav-1

    数据库索引,是数据库管理系统中一个排序的数据结构,以协助快速查询.更新数据库表中数据.索引的实现通常使用B树及其变种B+树. 在数据之外,数据库系统还维护着满足特定查找算法的数据结构,这些数据结构以某 ...

  2. moodle其他代码

    , $sectionnum=false, $strictness=IGNORE_MISSING):给课程模块一个id,找出coursemoudle的描述 get_coursemodule_from_i ...

  3. CodeForces 667C Reberland Linguistics

    $dp$. 题意中有一个词组:$in$ $a$ $row$,是连续的意思.... 因此这题只要倒着$dp$一下就可以了.$f[i][0]$表示从$i$位置往后割两个能否割,$f[i][1]$表示从$i ...

  4. mysql中datetime和timestamp的区别

    原文地址:http://database.51cto.com/art/200905/124240.htm 相同 显示 TIMESTAMP列的显示格式与DATETIME列相同.换句话说,显示宽度固定在1 ...

  5. 关于Trie Tree简单实现

    最近突然有兴致hiho一下了,实现了下trie tree,感觉而言,还是挺有意思的,个人觉得这货不光可以用来查单词吧,其实也可以用来替代Hash,反正查找,插入复杂度都挺低的,哈哈,啥都不懂,瞎扯.. ...

  6. Atomic 升级

    Atomic 采用类似github的版本管理,  可以使用以下命令升级 ostree remote add --set=gpg-verify=false atomic20160212 http://. ...

  7. Java集合初体验

    背景:        因为对Java的集合完全不了解,所以才在网上找了找能形成初步印象的文章进行学习,大多涉及的是一些概念和基础知识. 一.数组array和集合的区别: (1)数组是大小固定的,并且同 ...

  8. 关于:1.指针与对象;2.深浅拷贝(复制);3.可变与不可变对象;4.copy与mutableCopy的一些理解

    最近对深浅拷贝(复制)做了一些研究,在此将自己的理解写下来,希望对大家有所帮助.本人尚处在摸索阶段,希望各位予以指正. 本文包括如下方向的探索: 1.指针与对象: 2.深/浅拷贝(复制): 3.可变/ ...

  9. Redis哈希相关命令

    hash类型(类似于多维数组)hset key field value 把key中filed域的值设置为value(如果之前存在就覆盖,不存在就添加) hmset key field1 value1[ ...

  10. (十)foreac遍历、break和countinue以及标签和switch循环

    foreach语法,表示不必创建int变量去对由访问项构成的序列进行计数,foreach将自动产生每一项. 例:输出数组的所有元素. float f[]=new float[5]; f[0]=1.0f ...