这些例子都是CABasicAnimation的一些简单实现的动画,例如移动、透明度、翻转等等。方法里面传入一个CALayer类或者子类就可以了。

下面是用swift实现的,这些方法我们也可以用作公共类来调用:

    //移动动画position
func addLayerAnimationPosition(layer: CALayer) {
let animation = CABasicAnimation(keyPath: "position")
//开始的位置
animation.fromValue = NSValue(CGPoint: layer.position)
//移动到的位置
animation.toValue = NSValue(CGPoint: CGPointMake(, ))
//持续时间
animation.duration =
//运动后的位置保持不变(layer的最后位置是toValue)
animation.removedOnCompletion = false
animation.fillMode = kCAFillModeForwards //添加动画
layer.addAnimation(animation, forKey: "addLayerAnimationPosition")
} //透明度 opacity
func addLayerAnimationOpacity(layer: CALayer) {
let animation = CABasicAnimation(keyPath: "opacity")
animation.fromValue =
animation.toValue =
animation.duration = layer.addAnimation(animation, forKey: "addLayerAnimationOpacity") } //变大与位置 bounds
func addLayerAnimationBounds(layer:CALayer) {
let animation = CABasicAnimation(keyPath: "bounds")
animation.toValue = NSValue(CGRect:CGRectMake(, , , ))
animation.duration =
animation.repeatCount = layer.addAnimation(animation, forKey: "addLayerAnimationBounds")
} //由小变大 bounds.size
func addLayerAnimationBoundsSize(layer:CALayer) {
let animation = CABasicAnimation(keyPath: "bounds.size")
animation.fromValue = NSValue(CGSize: layer.bounds.size)
animation.toValue = NSValue(CGSize:CGSizeMake(layer.bounds.size.width+, layer.bounds.size.height+))
animation.duration =
animation.repeatCount = layer.addAnimation(animation, forKey: "addLayerAnimationBoundsSize")
} //改变颜色 backgroundColor
func addLayerAnimationBackgroundColor(layer:CALayer) {
let animation = CABasicAnimation(keyPath: "backgroundColor")
animation.toValue = UIColor.blueColor().CGColor
animation.duration =
animation.repeatCount = layer.addAnimation(animation, forKey: "addLayerAnimationMargin")
} //渐变圆角 cornerRadius
func addLayerAnimationCornerRadius(layer:CALayer) {
let animation = CABasicAnimation(keyPath: "cornerRadius")
animation.toValue =
animation.duration =
animation.repeatCount = layer.addAnimation(animation, forKey: "addLayerAnimationCornerRadius")
} //改变边框border的大小(图形周围边框,border默认为黑色), borderWidth
func addLayerAnimationBorderWidth(layer:CALayer) {
let animation = CABasicAnimation(keyPath: "borderWidth")
animation.toValue =
animation.duration =
animation.repeatCount = layer.addAnimation(animation, forKey: "addLayerAnimationBorderWidth")
} //改变layer内容(图片),注意如果想要达到改变内容的动画效果,首先在运行动画之前定义好layer的contents contents
func addLayerAnimationContents(layer:CALayer) {
let animation = CABasicAnimation(keyPath: "contents")
let toImage = UIImage.init(named: "通车辆设计矢量素材-06.png")?.CGImage
animation.toValue = toImage
animation.duration =
animation.repeatCount = layer.addAnimation(animation, forKey: "addLayerAnimationBounds")
} //缩放、放大 transform.scale
func addLayerAnimationTransformScale(layer:CALayer) {
let animation = CABasicAnimation(keyPath: "transform.scale")
//开始时的倍率
animation.fromValue = 1.0
//结束时的倍率
animation.toValue = 0.5
animation.duration =
animation.repeatCount =
animation.autoreverses = true layer.addAnimation(animation, forKey: "addLayerAnimationScale")
} //旋转动画(翻转,沿着X轴) transform.rotation.x
func addLayerAnimationTranformRotationX(layer: CALayer) {
let animation = CABasicAnimation(keyPath: "transform.rotation.x")
//旋转180度 = PI
animation.toValue = M_PI
animation.duration =
animation.repeatCount =
//这里我们可以添加可以不添加,添加一个缓慢进出的动画效果(int/out)。当不添加时,匀速运动,会使用kCAMediaTimingFunctionLinear;当添加时,layer会在开始和结束时比较缓慢
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) layer.addAnimation(animation, forKey: "addLayerAnimationTranformRotationX")
} //旋转动画(翻转,沿着Y轴) transform.rotation.y
func addLayerAnimationTranformRotationY(layer: CALayer) {
let animation = CABasicAnimation(keyPath: "transform.rotation.y")
//旋转180度 = PI
animation.toValue = M_PI
animation.duration =
animation.repeatCount =
//这里我们可以添加可以不添加,添加一个缓慢进出的动画效果(int/out)。当不添加时,匀速运动,会使用kCAMediaTimingFunctionLinear;当添加时,layer会在开始和结束时比较缓慢
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) layer.addAnimation(animation, forKey: "addLayerAnimationTranformRotationY")
} //旋转动画(沿着Z轴) transform.rotation.z
func addLayerAnimationTranformRotationZ(layer: CALayer) {
let animation = CABasicAnimation(keyPath: "transform.rotation.z")
//旋转360度 = PI*2
animation.toValue = M_PI*
animation.duration =
animation.repeatCount =
//这里我们可以添加可以不添加,添加一个缓慢进出的动画效果(int/out)。当不添加时,匀速运动,会使用kCAMediaTimingFunctionLinear;当添加时,layer会在开始和结束时比较缓慢
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) layer.addAnimation(animation, forKey: "addLayerAnimationTranformRotationZ")
} //横向移动(沿着X轴) transform.translation.x
func addLayerAnimationTranformTranslationX(layer: CALayer) {
let animation = CABasicAnimation(keyPath: "transform.translation.x")
animation.toValue =
animation.duration =
animation.repeatCount =
//这里我们可以添加可以不添加,添加一个缓慢进出的动画效果(int/out)。当不添加时,匀速运动,会使用kCAMediaTimingFunctionLinear;当添加时,layer会在开始和结束时比较缓慢
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) layer.addAnimation(animation, forKey: "addLayerAnimationTranformTranslationX")
} //纵向移动(沿着Y轴) transform.translation.y
func addLayerAnimationTranformTranslationY(layer: CALayer) {
let animation = CABasicAnimation(keyPath: "transform.translation.y")
animation.toValue =
animation.duration =
animation.repeatCount =
//这里我们可以添加可以不添加,添加一个缓慢进出的动画效果(int/out)。当不添加时,匀速运动,会使用kCAMediaTimingFunctionLinear;当添加时,layer会在开始和结束时比较缓慢
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) layer.addAnimation(animation, forKey: "addLayerAnimationTranformTranslationY")
}

如果我们只要看keypath的值,我们可以看到下面文章

http://www.cnblogs.com/alunchen/p/5300075.html

可以关注本人的公众号,多年经验的原创文章共享给大家。

IOS Animation-CABasicAnimation例子(简单动画实现)的更多相关文章

  1. [iOS Animation]-CALayer 隐式动画

    隐式动画 按照我的意思去做,而不是我说的. -- 埃德娜,辛普森 我们在第一部分讨论了Core Animation除了动画之外可以做到的任何事情.但是动画是Core Animation库一个非常显著的 ...

  2. iOS Animation 主流炫酷动画框架(特效)收集整理 #91

    https://github.com/sxyx2008/DevArticles/issues/91

  3. ios之CABasicAnimation

    博主:最近iOS开发中用到CoreAnimation的framework来做动画效果,虽然以前也用过,但一直没有系统学习过,今天看到一篇非常详细的博文(虽然是日语,但真的写的很好),在此翻译出来供大家 ...

  4. [iOS Animation]-CALayer 显示动画

    显式动画 如果想让事情变得顺利,只有靠自己 -- 夏尔·纪尧姆 上一章介绍了隐式动画的概念.隐式动画是在iOS平台创建动态用户界面的一种直接方式,也是UIKit动画机制的基础,不过它并不能涵盖所有的动 ...

  5. Swift 实现iOS Animation动画教程

    这是一篇翻译文章.原文出处:http://www.raywenderlich.com/95910/uiview-animation-swift-tutorial 动画( animation)是iOS用 ...

  6. iOS CAReplicatorLayer 简单动画

    代码地址如下:http://www.demodashi.com/demo/11601.html 写在最前面,最近在看学习的时候,偶然间发现一个没有用过的Layer,于是抽空研究了下,本来应该能提前记录 ...

  7. iOS开发CABasicAnimation动画理解

    1.CALayer简介 CALayer是个与UIView很类似的概念,同样有backgroundColor.frame等相似的属性,我们可以将UIView看做一种特殊的CALayer.但实际上UIVi ...

  8. iOS Core Animation学习总结(3)--动画的基本类型

    一. CABasicAnimation (基础动画) 移位: CABasicAnimation *animation = [CABasicAnimation animation]; //keyPath ...

  9. ios animation 动画效果实现

    1.过渡动画 CATransition CATransition *animation = [CATransition animation]; [animation setDuration:1.0]; ...

随机推荐

  1. 阿里云 centos 安装apache和php

    mysql使用阿里云的rds httpd服务 1. 安装apr和apr-util 2. 安装 httpd apache.org,apr.apache.org 安装命令: ./configure --p ...

  2. Android PowerImageView实现,可以播放动画的强大ImageView

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/11100315 我个人是比较喜欢逛贴吧的,贴吧里总是会有很多搞笑的动态图片,经常看一 ...

  3. configure: error: *** libpopt not found 解决方法

    ubuntu (deb) $ apt-cache search popt|headlibpopt-dev - lib for parsing cmdline parameters - developm ...

  4. linux下解压被分割的zip文件

    形如被分割的一系列文件:linux.z01, linux.z02, linux.z03, linux.zip 直接右键解压是不行的. 首先合并文件:cat linux.* > linux_all ...

  5. JavaScript-BOM-history:保存当前窗口打开后成功访问过的url历史记录栈

    history:保存当前窗口打开后成功访问过的url历史记录栈history.go(n):前进n步前进一步:history.go(1);后退一步:history.go(-1);刷新:history.g ...

  6. mysql注入读写文件

    mysql <5.0 读文件:load_file() sql-shell select load_file(''); d:/www/xx/index.php /home/webroot/.... ...

  7. 无法打开注册表项 unknown 没有足够的权限访问

    secedit /configure /cfg %windir%\inf\defltbase.inf /db defltbase.sdb /verbose 执行完,重新安装即可.

  8. VMware Workstation虚拟机中的Linux通过NAT模式共享上网配置教程

    VMware Workstation虚拟机中的Linux通过NAT模式共享上网配置教程 在VMware Workstation虚拟机下面,Linux虚机要上网,一般是桥接模式,但我自己的电脑上网的环境 ...

  9. [UCSD白板题] Take as Much Gold as Possible

    Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...

  10. Asp.net Authenticated users 权限问题

    偶然发现Windows,非系统盘 权限具有这个组Authenticated users 拥有这个组会出现的问题: Web站点可以访问非Web目录的文件.