主要用到兩個方法:
/**
* 添加關(guān)鍵幀方法
*
* @param duration 動畫時長
* @param delay 動畫延遲
* @param options 動畫效果選項
* @param animations 動畫執(zhí)行代碼
* @param completion 動畫結(jié)束執(zhí)行代碼
*/
class func animateKeyframes(withDuration
duration: TimeInterval,
delay: TimeInterval,
options: UIViewKeyframeAnimationOptions = [],
animations: @escaping () -> Swift.Void,
completion: (@escaping (Bool) -> Swift.Void)? = nil)
/**
* 添加關(guān)鍵幀
*
* @param frameStartTime 動畫相對開始時間,取值為0-1.0
* @param frameDuration 動畫相對持續(xù)時間鸵隧,取值為0-1.0
* @param animations 動畫執(zhí)行代碼
*/
class func addKeyframe(withRelativeStartTime
frameStartTime: Double,
relativeDuration frameDuration: Double,
animations: @escaping () -> Swift.Void)
例子
func Animation(){
let animationView = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
animationView.backgroundColor = UIColor.green
self.view.addSubview(animationView)
UIView.animateKeyframes(withDuration: 3, delay: 0, options: .calculationModeLinear, animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration:0.2) {
animationView.frame.origin.y = 500
}
UIView.addKeyframe(withRelativeStartTime: 0.2, relativeDuration: 0.5) {
animationView.frame.origin.x = 300
}
UIView.addKeyframe(withRelativeStartTime: 0.7, relativeDuration: 0.3) {
animationView.frame.origin.y = 300
}
}) { (finished) in
//動畫完成
}
}
翻轉(zhuǎn)和翻頁動畫
主要方法
/**
*
* 為view設(shè)置翻轉(zhuǎn)貨翻頁動畫
* @param transition 設(shè)置動畫類型
* @param for view 執(zhí)行動畫的view
* @param cache
*/
class func setAnimationTransition(_ transition: UIViewAnimationTransition,
for view: UIView,
cache: Bool)
設(shè)置動畫類型
public enum UIViewAnimationTransition : Int {
case none
case flipFromLeft //從左往右翻轉(zhuǎn)
case flipFromRight //從右往左翻轉(zhuǎn)
case curlUp //從上往下翻轉(zhuǎn)
case curlDown //從下往上翻轉(zhuǎn)
}
例子:
func Animation6(){
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1)
UIView.setAnimationCurve(.easeInOut)
//從左向右翻轉(zhuǎn)
UIView.setAnimationTransition(UIViewAnimationTransition.flipFromLeft, for: animationView, cache: false)//從左向右翻轉(zhuǎn)
UIView.commitAnimations()
}
旋轉(zhuǎn),縮放,平移,取反
實現(xiàn)方法為設(shè)置View 的transform屬性
創(chuàng)建一個CGAffineTransform實例
//向右向下平移是個單位议谷,中心點位置改變
let transform = CGAffineTransform(translationX: 10, y: 10)
//旋轉(zhuǎn)PI /4 個角度粱锐,中心點位置不變
let transform = CGAffineTransform(rotationAngle: CGFloat(M_PI_4))
//寬縮放為原來的2倍朱嘴,高縮放為原來的3倍巡社,中心點位置不變
let transform = CGAffineTransform(scaleX: 2, y: 3)
//取反是吧CGAffineTransform的值取反山橄,不是恢復(fù)到原來的狀態(tài)
//例如:順時針旋轉(zhuǎn)90度取反就是逆時針旋轉(zhuǎn)90度怀各。
let transform = CGAffineTransform.inverted(animationView.transform)
例子:
//平移
func Animation11(){
let transform = CGAffineTransform(translationX: 10, y: 10)
UIView.animate(withDuration: 2, animations: {
self.animationView.transform = transform
}) { (finished) in
print(NSStringFromCGRect(self.animationView.frame))
}
}
在主線程中調(diào)用Animation()方法即可查看效果。
本文參考文章:
http://www.tuicool.com/articles/BjMrQne
http://blog.csdn.net/mad2man/article/details/17554621