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,它会更 ...
随机推荐
- hdu3567 八数码(搜索)--预处理
题意:为你两个状态,求a到b 的最小路径,要求字典序最小. 思路: 最开始想的是目标状态是变化的,所以打表应该不行,然后直接上A*,但是TLE了- -(瞬间无语) 然后看了下别人的思路,预处理出9个状 ...
- 文本分类学习 (五) 机器学习SVM的前奏-特征提取(卡方检验续集)
前言: 上一篇比较详细的介绍了卡方检验和卡方分布.这篇我们就实际操刀,找到一些训练集,正所谓纸上得来终觉浅,绝知此事要躬行.然而我在躬行的时候,发现了卡方检验对于文本分类来说应该把公式再变形一般,那样 ...
- JavaBean toString方式
package object; import java.util.Date; public class ReportDataQo implements java.io.Serializable { p ...
- JAVAEE——BOS物流项目10:权限概述、常见的权限控制方式、apache shiro框架简介、基于shiro框架进行认证操作
1 学习计划 1.演示权限demo 2.权限概述 n 认证 n 授权 3.常见的权限控制方式 n url拦截权限控制 n 方法注解权限控制 4.创建权限数据模型 n 权限表 n 角色表 n 用户表 n ...
- 阿里云部署Node.js项目(CentOS)
Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境,用来方便地搭建快速的易于扩展的网络应用.Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又 ...
- CSS 常用的命名规则
(1)页面结构 容器: container 页头:header 内容:content/container 页面主体:main 页尾:footer 导航:nav 侧栏:sidebar 栏目:column ...
- Python List insert()方法详解
1.功能insert()函数用于将指定对象插入列表的指定位置. 2.语法list.insert(index, obj) 3.参数index: 对象obj需要插入的索引位置.obj: 插入列表中的对象. ...
- 转:rabbitMQ 安装与管理
安装环境 虚拟机:VMware® Workstation 10.0.1 build Linux系统:CentOS6.5 官方安装:http://www.rabbitmq.com/install-rpm ...
- [JCIP笔记](五)JDK并发包
这一节来讲一讲java.util.concurrent这个包里的一些重要的线程安全有关类. synchronized容器 synchronized容器就是把自己的内部状态封装起来,通过把每一个publ ...
- sshpass笔记
sshpass简介 ssh登录的时候使用的是交互式输入,不能预先在命令行使用参数指定密码,sshpass就是为了解决这个问题的.sshpass提供非交互式输入密码的方式,可以用在shell脚本中自动输 ...