前面介绍了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:toPointcontrolPoint: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 = [ CABasicAnimationanimationWithKeyPath: @"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 statepacmanOpenPath = [UIBezierPath bezierPathWithArcCenter:arcCenterradius:radiusstartAngle:DEGREES_TO_RADIANS(35)endAngle:DEGREES_TO_RADIANS(315)clockwise:YES];[pacmanOpenPath addLineToPoint:arcCenter];[pacmanOpenPath closePath];// Create a UIBezierPath for Pacman's close statepacmanClosedPath = [UIBezierPath bezierPathWithArcCenter:arcCenterradius:radiusstartAngle:DEGREES_TO_RADIANS(1)endAngle:DEGREES_TO_RADIANS(359)clockwise:YES];[pacmanClosedPath addLineToPoint:arcCenter];[pacmanClosedPath closePath];// Create a CAShapeLayer for Pacman, fill with yellowshapeLayer = [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 valueschompAnimation.fromValue = (id)pacmanClosedPath.CGPath;chompAnimation.toValue = (id)pacmanOpenPath.CGPath;[shapeLayer addAnimation:chompAnimation forKey:@"chompAnimation"];// Create digital '2'-shaped pathUIBezierPath *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. 小程序 · Animation——淡入淡出动画效果

    淡入淡出动画效果 主要原理是通过控制透明度显示. WXML <!-- 加入居民提示 --><view class="add-tip" animation=&quo ...

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

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

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

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

  4. html 清除animation,简单删除动画效果

    前端入坑纪 24 工作不忙了,悠闲的时候,有想法也特别多.那就,双更再来一个! 老简单的效果 OK,first things first!项目链接 HTML 结构 秘密的甚笃 删除 秘密的甚笃 删除 ...

  5. iOS动画集锦(Core Animation)

    iOS 动画主要是指 Core Animation 框架, Core Animation是 iOS 和 OS X 平台上负责图形渲染与动画的基础框架.Core Animation 可以作用于动画视图或 ...

  6. iOS开发:Core Animation编程指南

    关于Core Animation Core Animation是iOS与OS X平台上负责图形渲染与动画的基础设施.Core Animation可以动画视图和其他的可视元素.Core Animatio ...

  7. Core Animation学习总结

    目录: The Layer Beneath The Layer Tree(图层树) The Backing Image(寄宿层) Layer Geometry(图层几何学) Visual Effect ...

  8. jQuery Easing 动画效果扩展--使用Easing插件,让你的动画更具美感。

    jQuery  Easing 是一款比较老的jQuery插件,在很多网站都有应用,尤其是在一些页面滚动.幻灯片切换等场景应用比较多.它非常小巧,且有多种动画方案供选择,使用简单,而且免费. 引入Eas ...

  9. Array王锐大神力作:osg与PhysX结合系列内容——第5节 角色动画效果(上)

    [Array王锐大神力作]osg与PhysX结合系列内容--角色动画效果(上) 物理引擎先放一边 动画库ozz-animation 动画资源管理 载入和预处理动画 合并到OSG显示 物理引擎先放一边 ...

最新文章

  1. 关于Kanas.Net框架的一些背景
  2. ubuntu java 全屏显示_java 在ubuntu下实现全屏,上面的状态栏依然显示。如下图,不想要上面的状态栏...
  3. ubuntu下安装mysql
  4. 基于语音识别的微博签到系统
  5. linux解决软件依赖的命令,通过yum解决软件的依赖关系
  6. 【C语言简单说】三:整数变量扩展和输出扩展(3)
  7. 广州程序员辞职创业卖菠萝油,放弃30万年薪
  8. 2014年前端开发者如何提升自己
  9. npm login 登录失败,报E500 Internal Server Error - PUT https://registry.npm.taobao.org/-/user/org.couchdb
  10. failed to load ldlinux.c32
  11. Android 本地化翻译插件,解放你的双手! AndroidLocalizePlugin
  12. MM 移动类型-入门篇
  13. Allegro artwork 参数设置
  14. matlab画直线段,如果要在MATLAB中绘制上题中的直线段,要求 ,则对应的MATLAB语句为____________...
  15. android qq版本6.6.1,手机QQ6.6.1有哪些更新内容 手机QQ最新版本详细介绍
  16. 图案设计灵感怎么写_服装设计灵感怎么写
  17. 马斯洛的五大需求层次理论
  18. 磁粉制动器磁滞特性的研究笔记
  19. libyuv windows x64编译方法
  20. 批处理命令goto的使用

热门文章

  1. 电商软件性能测试,实战 | 电商业务的性能测试(一): 必备基础知识
  2. dw选项卡代码_使用DW软件实现html编码转换的详细步骤
  3. ySQL字符串函数:字符串截取
  4. 为什么 MQTT 是最适合物联网的网络协议
  5. Python读取CSV文件:UnicodeDecodeError: 'gbk' codec can't decode byte 0xba ....illegal multibyte sequence
  6. CCIE与HCIE那个含金量高些?
  7. qt小项目 代码实现简易的QQ聊天 对话框的界面实现
  8. 数据库经典基础练习题45道
  9. 企业微信接口开发——通讯录管理(创建、删除)
  10. 51单片机流水灯:控制LED亮暗顺序