動畫每每能起到加強用戶體驗的做用,在 iOS 開發中,咱們可使用 UIKit 提供的動畫來實現,簡稱 UIView 動畫。UIView 動畫實質上是對 Core Animation(核心動畫)的封裝,提供簡潔的動畫 API。swift
// 最完整
open class func animate(withDuration duration: TimeInterval, delay: TimeInterval, options: UIView.AnimationOptions = [], animations: @escaping () -> Void, completion: ((Bool) -> Void)? = nil) open class func animate(withDuration duration: TimeInterval, animations: @escaping () -> Void, completion: ((Bool) -> Void)? = nil) // 最簡單 open class func animate(withDuration duration: TimeInterval, animations: @escaping () -> Void) 複製代碼
// 兩個參數
// 將須要執行動畫的語句放入閉包便可
UIView.animate(withDuration: 0.5) {
view.backgroundColor = UIColor.red
}
// 閉包中能夠同時執行多個屬性的動畫
UIView.animate(withDuration: 0.5) {
view.backgroundColor = UIColor.red
view.center.y += 100
view.alpha = 0
}
// 三個參數
UIView.animate(withDuration: 0.5) {
view.backgroundColor = UIColor.red
} completion: { _ in
print("動畫執行完畢")
}
// 五個參數
UIView.animate(withDuration: 0.5, delay: 0.5, options: .curveLinear) {
view.backgroundColor = UIColor.red
} completion: { _ in
print("動畫執行完畢")
}
// 放在performWithoutAnimation閉包中就會不執行動畫
UIView.animate(withDuration: 0.5) {
view.backgroundColor = UIColor.red
UIView.performWithoutAnimation {
view.alpha = 0.2
}
}
複製代碼
常見的AnimationOptions
有:api
view.layer.removeAllAnimations()
複製代碼
open class func animate(withDuration duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping dampingRatio: CGFloat, initialSpringVelocity velocity: CGFloat, options: UIView.AnimationOptions = [], animations: @escaping () -> Void, completion: ((Bool) -> Void)? = nil) 複製代碼
// 三個動畫進行對比
UIView.animate(withDuration: 3.0, delay: 0, usingSpringWithDamping: 0.1, initialSpringVelocity: 0, options: .curveEaseIn, animations: {
blueView.center.y += 300
}, completion: nil)
UIView.animate(withDuration: 3.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: .curveEaseIn, animations: {
greenView.center.y += 300
}, completion: nil)
UIView.animate(withDuration: 3.0, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 20, options: .curveEaseIn, animations: {
redView.center.y += 300
}, completion: nil)
複製代碼
// 單個視圖的過渡效果
open class func transition(with view: UIView, duration: TimeInterval, options: UIView.AnimationOptions = [], animations: (() -> Void)?, completion: ((Bool) -> Void)? = nil) // 從舊視圖轉到新視圖的動畫效果 open class func transition(from fromView: UIView, to toView: UIView, duration: TimeInterval, options: UIView.AnimationOptions = [], completion: ((Bool) -> Void)? = nil) 複製代碼
UIView.transition(with: self.redView, duration: 2.0, options: .transitionCurlUp, animations: {
let orangeView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
orangeView.center = self.redView.center
orangeView.backgroundColor = UIColor.orange
self.redView.addSubview(orangeView)
}, completion: nil)
複製代碼
let orangeView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
orangeView.backgroundColor = UIColor.orange
UIView.transition(from: self.redView, to: orangeView, duration: 2.0, options: .transitionFlipFromRight, completion: nil)
複製代碼
思考:在方式一中設置的orangeView.center = self.redView.center
並無實現預期的效果,爲何?markdown
由於
redView
和orangeView
參考的不是同一個座標系,須要進行座標轉換。閉包
座標轉換分爲兩種,一種是 CGPoint 轉換,一種是 CGRect 轉換。ide
// self.view(from參數)的self.redView.center(point參數)轉換到self.redView(調用者)中
orangeView.center = self.redView.convert(self.redView.center, from: self.view)
// self.view(調用者)將self.redView.center(point參數)轉換到self.redView(to參數)中
orangeView.center = self.view.convert(self.redView.center, to: self.redView)
複製代碼
// self.redView(from參數)的orangeView.frame(rect參數)轉換到self.view(調用者)中
orangeView.frame = self.view.convert(orangeView.frame, from: self.redView)
// self.view(調用者)將self.redView.frame(rect參數)轉換到self.redView(to參數)中
self.redView.frame = self.view.convert(self.redView.frame, to: self.redView)
複製代碼
open class func animateKeyframes(withDuration duration: TimeInterval, delay: TimeInterval, options: UIView.KeyframeAnimationOptions = [], animations: @escaping () -> Void, completion: ((Bool) -> Void)? = nil) open class func addKeyframe(withRelativeStartTime frameStartTime: Double, relativeDuration frameDuration: Double, animations: @escaping () -> Void) 複製代碼
class ViewController: UIViewController {
@IBOutlet var leaf: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
UIView.animateKeyframes(withDuration: 6.0, delay: 0, options: .calculationModeCubic, animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.1, animations: {
self.leaf.center = CGPoint(x: 108, y: 260)
})
UIView.addKeyframe(withRelativeStartTime: 0.1, relativeDuration: 0.1, animations: {
self.leaf.center = CGPoint(x: 138, y: 340)
})
UIView.addKeyframe(withRelativeStartTime: 0.2, relativeDuration: 0.1, animations: {
self.leaf.center = CGPoint(x: 168, y: 420)
})
UIView.addKeyframe(withRelativeStartTime: 0.3, relativeDuration: 0.1, animations: {
self.leaf.center = CGPoint(x: 200, y: 490)
})
UIView.addKeyframe(withRelativeStartTime: 0.4, relativeDuration: 0.1, animations: {
self.leaf.center = CGPoint(x: 248, y: 570)
})
UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.1, animations: {
self.leaf.center = CGPoint(x: 278, y: 630)
})
UIView.addKeyframe(withRelativeStartTime: 0.6, relativeDuration: 0.1, animations: {
self.leaf.center = CGPoint(x: 308, y: 690)
})
UIView.addKeyframe(withRelativeStartTime: 0.7, relativeDuration: 0.1, animations: {
self.leaf.center = CGPoint(x: 318, y: 770)
})
UIView.addKeyframe(withRelativeStartTime: 0.8, relativeDuration: 0.1, animations: {
self.leaf.center = CGPoint(x: 328, y: 860)
})
UIView.addKeyframe(withRelativeStartTime: 0.9, relativeDuration: 0.1, animations: {
self.leaf.transform = CGAffineTransform(rotationAngle: CGFloat(Float.pi * 0.25))
})
}, completion: nil)
}
}
複製代碼
常見的KeyframeAnimationOptions
有:動畫