Swift基础之Animation动画研究
最近研究了一下,Swift语言中关于Animation动画的实现学习,分两次进行相关内容的讲解
用表格列出各种动画情况
Demo首页显示展示了一种动画显示方式,代码如下:
//绘画装饰
func drawDecorate(){
//画出小圆
let smallCenterPoint = CGPointMake(50, 50);
let smallRadiusFloat:CGFloat = sqrt(800);
let smallMaskPath = UIBezierPath.init(ovalInRect: CGRectInset(CGRectMake(smallCenterPoint.x, smallCenterPoint.y, 1, 1), -smallRadiusFloat, -smallRadiusFloat));
//画出大圆
let largeCenterPoint = CGPointMake(50, 50);
let largeRadiusFloat = sqrt(pow(view.bounds.width-largeCenterPoint.x, 2)+pow(view.bounds.height-largeCenterPoint.y, 2));
let largerMaskPath = UIBezierPath.init(ovalInRect: CGRectInset(CGRectMake(largeCenterPoint.x, largeCenterPoint.y, 1, 1), -largeRadiusFloat, -largeRadiusFloat));
//表层图画
let maskLayer = CAShapeLayer();
maskLayer.path = largerMaskPath.CGPath;
view.layer.mask = maskLayer;
//基础动画
let baseAnimation = CABasicAnimation.init(keyPath: "path");
baseAnimation.duration = 0.5;
baseAnimation.fromValue = smallMaskPath.CGPath;
baseAnimation.toValue = largerMaskPath.CGPath;
baseAnimation.delegate = self;
maskLayer.addAnimation(baseAnimation, forKey: "path");
}
然后今天说明的是:1:点击进行滑动动画;2:点击进行两次动画效果;3:基本动画;4:键值动画;5:转场动画
部分代码:
1:点击进行滑动动画;
(可以添加渐变效果)
//# MARK:- 用UIView.animateWithDuration实现更逼真的运动效果
func view1Clicked(tapGesture:UITapGestureRecognizer){
//view1动画,向左移动100
let theView = tapGesture.view!
//usingSpringWithDamping 阻尼,范围0-1,阻尼越接近于0,弹性效果越明显
UIView.animateWithDuration(0.2, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 1, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
theView.frame = CGRect(x: theView.frame.origin.x+100, y: theView.frame.origin.y, width: theView.frame.size.width, height: theView.frame.height)
}, completion: nil)
}
2:点击进行两次动画效果;
//CATransaction 配置隐式动画
func view1Clicked(tapGesture:UITapGestureRecognizer){
CATransaction.begin()
//CATranscation 属性
//设置动画执行时间
CATransaction.setAnimationDuration(1)
//关闭隐式动画,这句话必须放在修改属性之前
// CATransaction.setDisableActions(true)
//设置动画完成后的回调
CATransaction.setCompletionBlock { () -> Void in
NSLog("Animation complete")
}
CATransaction.setAnimationDuration(1)
self.myLayer.backgroundColor = UIColor.greenColor().CGColor
self.myLayer.opacity = 0.5
var moveToPoint = CGPoint(x: myLayer.position.x + 150, y: myLayer.position.y + 50)
if(moveToPoint.x > view.frame.size.width) { moveToPoint.x -= view.frame.size.width}
if(moveToPoint.y > view.frame.size.height) { moveToPoint.y -= view.frame.size.height}
self.myLayer.position = moveToPoint
CATransaction.commit()
performSelector(#selector(TwoDonghuaViewController.pause), withObject: nil, afterDelay: 0.2)
}
3:基本动画;
//移动
func move(theView:UIView){
/*
可选的KeyPath
transform.scale = 比例轉換
transform.scale.x
transform.scale.y
transform.rotation = 旋轉
transform.rotation.x
transform.rotation.y
transform.rotation.z
transform.translation
transform.translation.x
transform.translation.y
transform.translation.z
opacity = 透明度
margin
zPosition
backgroundColor 背景颜色
cornerRadius 圆角
borderWidth
bounds
contents
contentsRect
cornerRadius
frame
hidden
mask
masksToBounds
opacity
position
shadowColor
shadowOffset
shadowOpacity
shadowRadius
*/
let baseAnimation = CABasicAnimation(keyPath: "position")
//baseAnimation.fromValue 初始位置,如果不设就是当前位置
let endPoint = CGPoint(x: theView.layer.position.x+100, y: theView.layer.position.y)
baseAnimation.toValue = NSValue(CGPoint:endPoint)//绝对位置
//baseAnimation.byValue = NSValue(CGPoint:CGPoint(x: 100, y: 0))//相对位置
//动画其他属性
baseAnimation.duration = 0.2
baseAnimation.repeatCount = 1
baseAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)//加速运动
//baseAnimation.timingFunction = CAMediaTimingFunction(controlPoints: 0.5, 0, 0.9, 0.7)//自定义加速的曲线参数
//这两个属性若不设置,动画执行后回复位
baseAnimation.removedOnCompletion = false
baseAnimation.fillMode = kCAFillModeForwards
baseAnimation.delegate = self
//可以在动画中缓存一些
baseAnimation.setValue(NSValue(CGPoint: endPoint), forKey: "endPoint")
baseAnimation.setValue(theView, forKey: "sender")
//开始动画
theView.layer.addAnimation(baseAnimation, forKey: "theViewMoveRight100")
}
4:键值动画
func imageViewClicked(tapGesture:UITapGestureRecognizer){
//键值动画
//keyPath和basicAnimation的类型相同,@see BasicAnimationViewController.swift
let keyframeAnimation = CAKeyframeAnimation(keyPath: "position")
//弧线位置移动
let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, 50, 50)
CGPathAddCurveToPoint(path, nil, 50, 50, 700, 300, 30, 500)
keyframeAnimation.path = path
keyframeAnimation.calculationMode = kCAAnimationLinear
//设置其他属性
keyframeAnimation.duration = 1.0;
// keyframeAnimation.beginTime = CACurrentMediaTime() + 2;//设置延迟2秒执行
tapGesture.view?.layer.addAnimation(keyframeAnimation, forKey: "keyframeAnimation1")
tapGesture.view?.layer.presentationLayer()
tapGesture.view?.layer.modelLayer()
}
5:转场动画
func swipeTransition(subtype:String)->CATransition{
let transfer = CATransition()
/*
kCATransitionFade:淡入淡出,默认效果
kCATransitionMoveIn:新视图移动到就是图上方
kCATransitionPush:新视图推开旧视图
kCATransitionReveal:移走旧视图然后显示新视图
//苹果未公开的私有转场效果
cube:立方体
suckEffect:吸走的效果
oglFlip:前后翻转效果
rippleEffect:波纹效果
pageCurl:翻页起来
pageUnCurl:翻页下来
cameraIrisHollowOpen:镜头开
cameraIrisHollowClose:镜头关
*/
let types = [kCATransitionFade,kCATransitionMoveIn,kCATransitionPush,kCATransitionReveal,"cube","suckEffect","oglFlip","rippleEffect","pageCurl","pageUnCurl","cameraIrisHollowOpen","cameraIrisHollowClose"]
let type = types[Int(arc4random()%11)]
transfer.type = type
NSLog("动画类型。。。。%@", type)
transfer.subtype = subtype
transfer.duration = 1
return transfer
}
源代码:http://download.csdn.net/detail/hbblzjy/9647803,敬请关注,(感觉不错,想要代码的,请留言或关注我)。。。
Swift基础之Animation动画研究的更多相关文章
- 【Android 基础】Animation 动画介绍和实现
在前面PopupWindow 实现显示仿腾讯新闻底部弹出菜单有用到Animation动画效果来实现菜单的显示和隐藏,本文就来介绍下吧. 1.Animation 动画类型 Android的animati ...
- Swift 实现iOS Animation动画教程
这是一篇翻译文章.原文出处:http://www.raywenderlich.com/95910/uiview-animation-swift-tutorial 动画( animation)是iOS用 ...
- android动画效果编程基础--Android Animation
动画效果编程基础--Android Animation 动画类型 Android的animation由四种类型组成 XML中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 tran ...
- 转 iOS Core Animation 动画 入门学习(一)基础
iOS Core Animation 动画 入门学习(一)基础 reference:https://developer.apple.com/library/ios/documentation/Coco ...
- Android物业动画研究(Property Animation)彻底解决具体解释
前p=1959">Android物业动画研究(Property Animation)全然解析具体解释上已经基本展示了属性动画的核心使用方法: ObjectAnimator实现动画 ...
- Android基础夯实--重温动画(二)之Frame Animation
心灵鸡汤:天下事有难易乎,为之,则难者亦易矣:不为,则易者亦难矣. 摘要 当你已经掌握了Tween Animation之后,再来看Frame Animation,你就会顿悟,喔,原来Frame Ani ...
- Android基础夯实--重温动画(三)之初识Property Animation
每个人都有一定的理想,这种理想决定着他的努力和判断的方向.就在这个意义上,我从来不把安逸和快乐看作生活目的的本身--这种伦理基础,我叫它猪栏的理想.--爱因斯坦 一.摘要 Property Anima ...
- Android基础夯实--重温动画(一)之Tween Animation
心灵鸡汤:真正成功的人生,不在于成就的大小,而在于你是否努力地去实现自我,喊出自己的声音,走出属于自己的道路. 摘要 不积跬步,无以至千里:不积小流,无以成江海.学习任何东西我们都离不开扎实的基础知识 ...
- Android基础夯实--重温动画(五)之属性动画 ObjectAnimator详解
只有一种真正的英雄主义 一.摘要 ObjectAnimator是ValueAnimator的子类,它和ValueAnimator一样,同样具有计算属性值的功能,但对比ValueAnimator,它会更 ...
随机推荐
- 51 nod 1188 最大公约数之和 V2
1188 最大公约数之和 V2 题目来源: UVA 基准时间限制:2 秒 空间限制:262144 KB 分值: 160 难度:6级算法题 给出一个数N,输出小于等于N的所有数,两两之间的最大公约数 ...
- hdu 5656 CA Loves GCD(n个任选k个的最大公约数和)
CA Loves GCD Accepts: 64 Submissions: 535 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 2 ...
- BZOJ2989 数列(二进制分组)
这题其实可以cdq分治做,但是如果强制在线的话,这里有个牛逼方法叫二进制分组. 它的基本思想是把修改操作按二进制分组,遇到修改就在尾部加一个,并与之前的合并,比如之前有23(16+4+2+1)个,加了 ...
- Python中生成器和迭代器的功能介绍
生成器和迭代器的功能介绍 1. 生成器(generator) 1. 赋值生成器 1. 创建 方法:x = (variable for variable in iterable) 例如:x = (i f ...
- 记一次java heap space的解决办法
问题缘由:后台上传excel导入到数据库,数据量太大,导致报错. 解决方案: 用jdk自带的性能分析器(jconsole)查看了一下,当excel开始导入的时候,发现堆空间直接爆掉. 增加堆空间,在c ...
- spring的事务配置方法
spring事务的配置有两种方式 1.xml配置的声明式事务配置 (1)配置数据源信息dataSource(使用阿里的数据源) <bean id="dataSource" c ...
- python学习之路基础篇(第七篇)
一.模块 configparser configparser用于处理特定格式的文件,其本质是利用open来对文件进行操作 [section1] # 节点 k1 = v1 # 值 k2:v2 # 值 [ ...
- Flume 读取RabbitMq消息队列消息,并将消息写入kafka
首先是关于flume的基础介绍 组件名称 功能介绍 Agent代理 使用JVM 运行Flume.每台机器运行一个agent,但是可以在一个agent中包含多个sources和sinks. Client ...
- qPCR检测基因表达的引物数据库
老板推荐了一个专门用来做基因表达定量(qPCR)的引物数据库,还蛮好用的,都是别人实验验证过的,感觉比自己设计的更靠谱一下,附上链接:https://pga.mgh.harvard.edu/prime ...
- 01_自动化构建工具之Maven
目前技术中存在问题(为什么使用Maven): 一个项目就是一个工程: 缺陷:如果项目太过庞大,就不适合使用package来划分层次,最好是一个模块就是一个工程,利于分工协作. 解决:Maven可以将一 ...