WWDC2017-advanced_animations_with_uikit
最后修改时间: 2017-12-1
这个Session主要讲一下的几个内容:
- Basic(基础的): 动画工作原理 以及 动画如何计时
- Interactive and Interruptible Animations: 可交互与可中断的动画
- New Property Animator Behaviors: UIPropertyAnimator新的属性
- Coordinating Animations: 协同动画
- Tips And Tricks: 提示和技巧
一、 Basic(基础的)
传统的动画是线性的。
UIView.animate(withDuration: 0.5) {
circle.frame = circle.frame.offsetBy(dx: 100, dy: 0)
}
UIViewPropertyAnimator 可以更好控制动画。包括: 可自定义time function、更好交互(interactive)、 可中断(interruptible) 以及 更好响应Responsive。
时间曲线(time curves): 本质就是一个函数,将时间转换为进度或者将动画所用的时间映射到动画的进度上。
- 线性曲线(Liner Curves): 时间的进度比(%Progress) = 动画进度比(%time)。
动画是按照一定的曲线的来完成的。 该曲线在时间与动画进度上有一定的关系。
例如: 线性动画10s,动画完成50% 所化时间 10 * 50% =
![Snip20170920_27.png-219.4kB][2]
- 非线性曲线也是按照时间进度函数来设置的。例如: easyOut
二、 Interactive and Interruptible Animations: 可交互与可中断的动画
2.1 Interactive(可交互动画)
```swift
// 1. 实例化一个 animator
var animator : UIViewPropertyAnimator!
@objc func handlePan(recognizer: UIPanGestureRecognizer) {
switch recognizer.state {
case .began:
// 2. 手势开始的时候,实例化,并设置相应的动作
animator = UIViewPropertyAnimator(duration: 2.5, curve: .easeOut, animations: {
self.circle.frame = self.circle.frame.offsetBy(dx: 100, dy: 0)
})
// 3. pauseAnimation, 动画未开始的时候, 会开启动画,并暂停
animator?.pauseAnimation()
case .changed:
// 4. 手势 changed,改变 animator 的完成比
let translation = recognizer.translation(in: circle)
animator?.fractionComplete = translation.x / 100
case .ended:
// 5. 从 4 的完成比继续开始动画
animator?.continueAnimation(withTimingParameters: nil, durationFactor: 0)
default:
break
}
}
```
值得一提的是,时间是如何计算呢?
以easyOut为例:用户交互是线性的,是动画进度来算的
当Progress = 0.5的时候,反应到时间曲线上,fractionComplete = 10% = 0.1;
那么剩余90%的time,fractionComplete = 0.1 来完成剩下动画的 50% Progress
2.2 Interruptible Animations(可中断动画)
var animator : UIViewPropertyAnimator!
func animateTransitionIfNeeded(duration: TimeInterval) {
if animator != nil {
return
}
animator = UIViewPropertyAnimator(duration: duration, curve: .easeIn, animations: {
self.circle.frame = self.circle.frame.offsetBy(dx: 300, dy: 0)
})
animator.startAnimation()
}
var progressWhenInterrupted : CGFloat = 0.0
@objc func handlePan(recognizer: UIPanGestureRecognizer) {
switch recognizer.state {
case .began:
animateTransitionIfNeeded(duration: 1)
animator?.pauseAnimation()
progressWhenInterrupted = animator.fractionComplete
case .changed:
let translation = recognizer.translation(in: circle)
animator?.fractionComplete = translation.x / 100 + progressWhenInterrupted
case .ended:
let timing = UICubicTimingParameters(animationCurve: .easeOut)
animator.continueAnimation(withTimingParameters: timing, durationFactor: 0)
default:
break
}
}
思路:
开启动画 -> 监听到手指之后,停止动画,记录动画的fractionComplete -> 手势Changed,改变动画的 fractionComplete -> 继续动画
Pause
- 当Animator 执行动画开始,到 1 (time = 0.5, Progress = 10%) 的时候,用户手势进行了中断;
- 用户交互是线性的,反应到了 2 (time = 0.1, progress = 0.1 因为线性的)的地方;
Continue
- 当用户松开之后,继续以 easyOut进行动画的时候,由 1 位置转变为 2 的位置, Progress = 10%
- 2 位置此时对应的 time = 0.05, fractionComplete = 0.05, 也就是说,剩下 95%的时间来完成 剩下Progress = 90%的动画
三、New Property Animator Behaviors: UIPropertyAnimator新的属性
- scrubsLinearly
- pausesOnCompletion.
3.1 scrubsLinearly
A Boolean value indicating whether a paused animation scrubs linearly or uses its specified timing information.
The default value of this property is true, which causes the animator to use a linear timing function during scrubbing. Setting the property to false causes the animator to use its specified timing curve.
简单的说: 这个参数就是,当你与动画进行交互时候,是按照线性的来做动画还是按照原来的时间曲线来做动画, true 表示利用线性的, false 表示利用指定的
例如: 动画开始是按照 easyIn的,用户进行交互时候,time curve 为线性的,
- scrubsLinearly = true, 那么用户进行交互的时候,是按照线性的来改变;
- scrubsLinearly = false, 那么就按照原来 easyIn的方式来。
3.2 pausesOnCompletion
A Boolean value that indicates whether a completed animation remains in the active state.
When the value of this property is true, the animator remains in the active state when the animation finishes, and it does not execute its completion handler. Keeping the animator in the active state allows you to reverse the animation even after it has finished. When the value of this property is false, the animator automatically transitions to the inactive state when the animation finishes, thereby concluding the animation. The default value of this property is false.
Because the completion handler is not called when this property is true, you cannot use the animator's completion handler to determine when the animations have finished running. Instead, you determine when the animation has ended by observing the isRunning property.
Animator的状态图(pausesOnCompletion = false )
- 开始实例化出来,Animator 是 .Inactive状态
- 当调用 startAnimation() 或 pauseAnimation(), Animator进入active状态;
- 动画结束,animator 再次进入 .Inactive 状态
Animator的状态图(pausesOnCompletion = true )
- 开始实例化出来,Animator 是 .Inactive状态
- 当调用 startAnimation() 或 pauseAnimation(), Animator进入active状态;
- 动画结束,animator 保持 active 状态
注意: 此时不会调用完成的回调, 观察是否动画完成,可以通过 isRunning属性
WWDC2017-advanced_animations_with_uikit的更多相关文章
- 看过WWDC2017的闲谈
2017年6月6日凌晨的138分钟,是属于WWDC2017的. 鉴于时间问题,没有熬夜看,所以早上起来趁着公司不太忙就看了看.整体的内容没有太多变化,依然是苹果的主产品,不过这次的one more t ...
- 你可能需要为你的APP适配iOS11
WeTest 导读 iOS 11 为整个生态系统的 UI 元素带来了一种更加大胆.动态的新风格. 本文介绍了iOS11在UI方面做了哪些更新,有些更新可以为用户提供更加完美的体验,但也有的可能会给目 ...
- 浅酌iOS 11兼容性
WeTest导读 苹果在WWDC2017大会,公布了最新的iOS 11,系统新特性肯定是让不少果粉充满期待.在网上已能看到不少关于iOS 11的体验文章,那么iOS 11的新特性会对APP产生什么兼容 ...
- 你可能需要为你的 APP 适配 iOS 11
本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/AZFrqL9dnlgA6Vt2sVhxIw 作者:s ...
- iOS Password AutoFill开发指南
转载请标明来源:https://www.cnblogs.com/zhanggui/p/9431950.html 引言 在<iPhone User Guide for iOS 11.4>这本 ...
- contentInsetAdjustmentBehavior各个值之间的区别
iOS11也出了不少时候了网上说适配的文章一大堆.关于contentInsetAdjustmentBehavior这个参数之间的区别,好像没什么人能说明.往下看的前提是你已经知道什么是安全区域,没看明 ...
- [SceneKit] 不会 Unity3D 的另一种选择
概述 SceneKit和SpriteKit的区别简单的来说就是二维和三维的区别 详细 代码下载:http://www.demodashi.com/demo/10664.html 上周一, 相信很多人和 ...
- 从iOS 11看怎样设计APP图标
苹果WWDC2017开发者大会已经尘埃落定,除了新产品的发布,iOS 11也正式亮相.新系统中,地图.App Store.时钟.相机.联系人等等原生应用都换了新的图标.此次图标的变化势必也会激发下一个 ...
- ARKit对安卓的提示 ARKit与Google Tango
我们知道安卓是Google开发的,那么关于AR谷歌有哪些作为呢?就是开发了Google Tango,尽管Tango还未开源,但是用户可以免费使用,可是一般的安卓手机是无法运行的,它对硬件有要求,这对它 ...
- Xcode9 新功能
翻译: https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/WhatsNewXcod ...
随机推荐
- MySQL-快速入门(4)MySQL函数
1.函数包括:数学函数.字符串函数.日期和时间函数.条件判断函数.系统信息函数.加密函数. 2.数学函数:绝对值函数.三角函数(正弦函数.余弦函数.正切函数.余切函数等).对数函数.随机数函数. 1& ...
- 部署CM集群首次运行报错:Formatting the name directories of the current NameNode.
1. 报错提示 Formatting the name directories of the current NameNode. If the name directories are not emp ...
- JS中值类型和引用类型
一.值类型 例子: var a=10; var b=a; a=20; console.log(b); 例子中,将a的值赋给了b,b=10,然后改变a的值不会影响b的值,a和b是独立的两份,互不影响. ...
- linux系统管理基础知识
1.linux的安装配置 虚拟机安装 Linux安装和分区 IP地址的配置 ifup eth0,ifdoen eth0 关闭不常用的程序 关闭selinux 远程登录(多用户,多任务) 用户和角色划分 ...
- [LeetCode] 103. 二叉树的锯齿形层次遍历
题目链接 : https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/ 题目描述: 给定一个二叉树,返回其节 ...
- Lock和synchronized的区别和使用(转发)
今天看了并发实践这本书的ReentantLock这章,感觉对ReentantLock还是不够熟悉,有许多疑问,所有在网上找了很多文章看了一下,总体说的不够详细,重点和焦点问题没有谈到,但这篇文章相当不 ...
- 【学习总结】快速上手Linux玩转典型应用-第6章-linux常用命令讲解
课程目录链接 快速上手Linux玩转典型应用-目录 目录 1. 软件操作命令 2. 服务器硬件资源信息 3. 文件操作命令 4. Linux文本编辑神器vim与其他常用命令 5. 系统用户操作命令 6 ...
- 防抖&节流
使用的原因 在前端开发当中有一部分的用户行为会频繁操作触发事件执行,而对于DOM操作,资源加载等耗费性能的处理,很可能导致页面卡顿,甚至浏览器崩溃,函数节流和防抖就是解决类似需求应运而生的 节流 预定 ...
- memset,内存初始化函数
# include <string.h> void *memset(void *s, int c, unsigned long n); 函数的功能是:将指针变量 s 所指向的前 n 字节的 ...
- Spring基础15——通过工厂方法来配置bean
1.什么是工厂方法 这里的工厂方法指的是创建指定bean的方法.工厂方法又分为静态工厂方法和实例工厂方法. 2.静态工厂方法配置bean 调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中 ...