終于空閑下來了缓窜,如今花一些時(shí)間學(xué)習(xí)我喜歡的動(dòng)畫吻育。
今天著重研究UIViewAnimationOptions
1. 動(dòng)畫重復(fù)(Repeating)
.repeat: 讓動(dòng)畫一直重復(fù)執(zhí)行
.autoreverse: 配合.repeat使用捻激,使動(dòng)畫反轉(zhuǎn)并持續(xù)執(zhí)行
2. 動(dòng)畫緩和(Animation easing)
.linear: 讓動(dòng)畫保持勻速
.curveEaseIn: 在動(dòng)畫開始時(shí)加速
.curveEaseOut: 在動(dòng)畫結(jié)束時(shí)減速
.curveEaseInOut: 相當(dāng)于[.curveEaseIn, .curveEaseOut]的組合,在開始加速和在結(jié)束動(dòng)畫時(shí)減速
3. 彈性動(dòng)畫(Spring animations)
// 方法展現(xiàn)與參數(shù)分析
UIView.animate(withDuration: 0.5, delay: 0.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, options: [], animations: {
self.loginButton.center.y -= 30
self.loginButton.alpha = 1.0
}, completion: nil)
? usingSpringWithDamping: 設(shè)置彈性動(dòng)畫的阻尼(范圍:0.0~1.0)栅组,越接近0.0彈性越大宴猾,反之則越小。
? initialSpringVelocity: 控制動(dòng)畫初始速度抵乓。
4. 轉(zhuǎn)換動(dòng)畫(Transitions)
轉(zhuǎn)換動(dòng)畫選項(xiàng)列表
.transitionFlipFromLeft, // 從左邊翻轉(zhuǎn)
.transitionFlipFromRight, // 從右邊翻轉(zhuǎn)
.transitionFlipFromTop, // 從底部翻轉(zhuǎn)
.transitionFlipFromBottom, // 從底部翻轉(zhuǎn)
.transitionCurlUp, // 卷上去
.transitionCurlDown, // 卷下來
.transitionCrossDissolve // 交叉溶解
增加一個(gè)視圖(Adding a new view)
var animationContainerView: UIView?
override func viewDidLoad() {
super.viewDidLoad()
animationContainerView = UIView(frame: view.bounds)
view.addSubview(animationContainerView!)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// create new view
let newView = UIImageView()
newView.image = UIImage(named: "e50")
newView.frame.size = CGSize(width: 50, height: 50)
newView.center = animationContainerView!.center
// add the new view via transition
UIView.transition(with: animationContainerView!,
duration: 0.33,
options: [.curveEaseOut, .transitionFlipFromBottom],
animations: { self.animationContainerView?.addSubview(newView) },
completion: nil)
}
移除一個(gè)視圖(Removing a view)
// remove the view via transition
UIView.transition(with: newView,
duration: 0.33,
options: [.curveEaseOut, .transitionFlipFromBottom],
animations: { self.newView.removeFromSuperview() },
completion: nil)
隱藏/顯示一個(gè)視圖(Hiding/showing a view)
// hide the view via transition
UIView.transition(with: self.newView,
duration: 0.33,
options: [.curveEaseOut, .transitionFlipFromBottom],
animations:{ self.newView.isHidden = true },
completion: nil)
替換一個(gè)視圖(Replacing a view with another view)
注意事項(xiàng)伴挚,需要設(shè)置新視圖frame靶衍,即大小和位置。
// replace via transition
UIView.transition(from: newView,
to: oldView,
duration: 0.33,
options: .transitionFlipFromTop,
completion: nil)
5. 關(guān)鍵幀動(dòng)畫(Keyframe animations)
UIViewKeyframeAnimationOptions選項(xiàng)如下:
.calculationModeLinear, // 連續(xù)運(yùn)算模式茎芋,線性
.calculationModeDiscrete, // 離散運(yùn)算模式颅眶,只顯示關(guān)鍵幀
.calculationModePaced, // 均勻執(zhí)行運(yùn)算模式,線性
.calculationModeCubic, // 平滑運(yùn)算模式
.calculationModeCubicPaced // 平滑均勻運(yùn)算模式
demo
func planeDepart() {
// 記錄初始位置
let originalCenter = planeImage.center
// 設(shè)置動(dòng)畫總時(shí)間為1.5秒, options動(dòng)畫
UIView.animateKeyframes(withDuration: 1.5, delay: 0.0, options: [], animations: {
// add keyframes
// 下面使用的事件都是相對事件(相對于當(dāng)前1.5s)田弥,范圍為(0.0~0.1)
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.25, animations: {
self.planeImage.center.x += 80.0
self.planeImage.center.y -= 10.0
})
UIView.addKeyframe(withRelativeStartTime: 1.0, relativeDuration: 0.4, animations: {
self.planeImage.transform = CGAffineTransform(rotationAngle: .pi * 0.125)
})
UIView.addKeyframe(withRelativeStartTime: 0.25, relativeDuration: 0.25, animations: {
self.planeImage.center.x += 100.0
self.planeImage.center.y -= 50
self.planeImage.alpha = 0.0
})
UIView.addKeyframe(withRelativeStartTime: 0.51, relativeDuration: 0.01, animations: {
self.planeImage.transform = CGAffineTransform.identity
self.planeImage.center = CGPoint(x: 0.0, y: originalCenter.y)
})
UIView.addKeyframe(withRelativeStartTime: 0.55, relativeDuration: 0.45, animations: {
self.planeImage.alpha = 1.0
self.planeImage.center = originalCenter
})
}, completion: nil)
}