Beginning iOS Animation Series (Swift 2)
2016-07-05 17:44
756 查看
Beginning iOS Animation Series (Swift 2)
对应raywenderlich上Beginning iOS Animation Series (Swift 2)的内容。Constraint Animations
Constraint的一般形式为:其中
multiplier为只读
Constraint Animations的简单使用方式为:
这里修改的是
constant
例如:
@IBAction func actionToggleMenu(sender: AnyObject) { isMenuOpen = !isMenuOpen menuHeightConstrait.constant = isMenuOpen ? 200.0 : 60.0; closeButtonTrailing.constant = isMenuOpen ? 16.0 : 8.0; titleLabel.text = isMenuOpen ? "Select Item" : "Packing List"; UIView.animateWithDuration(0.33, delay: 0, options: [.CurveEaseOut], animations: { let angle = self.isMenuOpen ? CGFloat(M_PI_4) : 0 self.buttonMenu.transform = CGAffineTransformMakeRotation(angle); self.slider.alpha = self.isMenuOpen ? 1 : 0; self.view.layoutIfNeeded() }, completion: nil) }
如何修改mutiplier?
找到对应的constraint,创建一个新的constraint来替换。
for constraint in titleLabel.superview!.constraints { if constraint.identifier == "TitleCenterX" { constraint.constant = isMenuOpen ? -100.0 : 0.0 } if constraint.identifier == "TitleCenterY" { constraint.active = false let newConstraint = NSLayoutConstraint( item: titleLabel, attribute: .CenterY, relatedBy: .Equal, toItem: titleLabel.superview!, attribute: .CenterY, multiplier: isMenuOpen ? 0.67 : 1.0, constant: 5.0); newConstraint.identifier = "TitleCenterY" newConstraint.active = true } }
下面是其对应的Challenge中的动画内容,主要也是修改
constant,但是其创建
constraint的方式很特别,如下:
let conX = imageView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor) let conBottom = imageView.bottomAnchor.constraintEqualToAnchor(view.bottomAnchor, constant: imageView.frame.height) let conWidth = imageView.widthAnchor.constraintEqualToAnchor(view.widthAnchor, multiplier: 0.33, constant: -50.0) let conHeight = imageView.heightAnchor.constraintEqualToAnchor( imageView.widthAnchor) NSLayoutConstraint.activateConstraints([conX, conBottom, conWidth, conHeight]) view.layoutIfNeeded() conBottom.constant = -imageView.frame.size.height/2 conWidth.constant = 0.0 UIView.animateWithDuration(0.33, delay: 0.0, options: [.CurveEaseOut], animations: { self.view.layoutIfNeeded() }, completion: nil); UIView.animateKeyframesWithDuration(0.67, delay: 2.0, options: [], animations: { conBottom.constant = imageView.frame.height conWidth.constant = -50.0 self.view.layoutIfNeeded() }) { _ in imageView.removeFromSuperview() }
动画效果:
Spring Animations
UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.4, initialSpringVelocity: 10.0, options: [.CurveEaseOut], animations: { self.view.layoutIfNeeded() let angle = self.isMenuOpen ? CGFloat(M_PI_4) : 0 self.buttonMenu.transform = CGAffineTransformMakeRotation(angle) }, completion: nil)
Spring damping: the smaller the damping the more the view bounces at the end of the animation. The closer the parameter is to 1.0 the less bouncing you get at the end of the animation; if you try 1.0 you get straight movement from point A to B)
Initial velocity: If you set this parameter to 1.0 and the animation moves the view 50 points across the screen - it will give the view 50 points/sec of initial velocity. If you set the parameter to 2.0 - the view will have 100 points/sec initial velocity.
具体的解释可以参考使用 iOS 8 Spring Animation API 创建动画
usingSpringWithDamping的范围为
0.0f到
1.0f,数值越小「弹簧」的振动效果越明显
initialSpringVelocity则表示初始的速度,数值越大一开始移动越快。
View Transitions
Three things that can trigger a transition:hidden
addSubview()
removeFromParent()
hidden
delay(seconds: 1.0, completion: { UIView.transitionWithView(imageView, duration: 1.0, options: [UIViewAnimationOptions.TransitionFlipFromBottom], animations: { imageView.hidden = true }, completion: { _ in }) })
动画效果:
removeFromParent()
delay(seconds: 0.35, completion: { self.actionToggleMenu(self) }) let titleBar = slider.superview! UIView.transitionWithView(titleBar, duration: 0.5, options: [.CurveEaseOut, .TransitionFlipFromBottom], animations: { self.slider.removeFromSuperview() }, completion: {_ in titleBar.addSubview(self.slider) }) }
动画效果
Keyframe Animations
主要使用方法是UIView.animateKeyframesWithDuration(2, delay: 0, options: [], animations: { // add keyframes }, completion: nil)
在其中嵌套使用
addKeyframeWithRelativeStartTime(_: relativeDuration: animations: )
该方法有三个参数:
startTime:关键帧开始时间,该时间是相对整个关键帧动画持续时间的相对时间,一般值在0到1之间。如果为0,则表明这一关键帧从整个动画的第0秒开始执行,如果设为0.5,则表明从整个动画的中间开始执行。
relativeDuration:关键帧持续时间,该时间同样是相对整个关键帧动画持续时间的相对时间,一般值也在0到1之间。如果设为0.25,则表明这一关键帧的持续时间为整个动画持续时间的四分之一。
animations:设置视图动画属性的动画闭包。
代码为:
UIView.animateKeyframesWithDuration(1.5, delay: 0.0, options: [], animations: { UIView.addKeyframeWithRelativeStartTime(0.0, relativeDuration: 0.25, animations: { self.planeImage.center.x += 80.0 self.planeImage.center.y -= 10.0 }) UIView.addKeyframeWithRelativeStartTime(0.1, relativeDuration: 0.4, animations: { self.planeImage.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_4/2)) }) UIView.addKeyframeWithRelativeStartTime(0.25, relativeDuration: 0.25, animations: { self.planeImage.center.x += 100.0 self.planeImage.center.y -= 50.0 self.planeImage.alpha = 0 }) UIView.addKeyframeWithRelativeStartTime(0.51, relativeDuration: 0.01, animations: { self.planeImage.transform = CGAffineTransformIdentity self.planeImage.center = CGPoint(x: 0, y: originalCenter.y) }) UIView.addKeyframeWithRelativeStartTime(0.55, relativeDuration: 0.45, animations: { self.planeImage.alpha = 1.0 self.planeImage.center = originalCenter }) }, completion: nil)
动画效果为:
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- Apple Swift学习教程
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- Gifski:一个跨平台的高质量 GIF 编码器
- 介绍 Fedora 上的 Swift
- 模仿动画的放大缩小容器
- jQuery 1.9.1源码分析系列(十五)动画处理之缓动动画核心Tween
- Android实现定制返回按钮动画效果的方法
- Android中ViewFlipper的使用及设置动画效果实例详解
- jQuery实现美观的多级动画效果菜单代码
- php判断GIF图片是否为动画的方法
- jQuery实现动画效果circle实例
- HTML5游戏引擎LTweenLite实现的超帅动画效果(附demo源码下载)
- 浅析JavaScript动画