1.CATransition
CATransition是CAAnimation的子類,用于過(guò)渡動(dòng)畫(huà)或轉(zhuǎn)場(chǎng)動(dòng)畫(huà)。為視圖層移入移除屏幕提供轉(zhuǎn)場(chǎng)動(dòng)畫(huà)。首先來(lái)看一下簡(jiǎn)單的Demo
let animation = CATransition.init()
animation.type = .fade
animation.subtype = CATransitionSubtype.fromRight
animation.duration = 1.0
self.view.window?.layer.add(animation, forKey: "kTransitionAnimation")
let vc = AVViewController()
self.present(vc, animated: true, completion: nil)
將該動(dòng)畫(huà)添加到window.layer上,則會(huì)present或push時(shí)使用指定的轉(zhuǎn)場(chǎng)動(dòng)畫(huà)。
其中最主要的兩個(gè)屬性就是type和subtype。
type:轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的類型。
官方SDK只提供了四種轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的類型,即:
extension CATransitionSubtype {
@available(iOS 2.0, *)
public static let fromRight: CATransitionSubtype
@available(iOS 2.0, *)
public static let fromLeft: CATransitionSubtype
@available(iOS 2.0, *)
public static let fromTop: CATransitionSubtype
@available(iOS 2.0, *)
public static let fromBottom: CATransitionSubtype
}
私有的type:
NSString *const kCATransitionCube = @"cube";
NSString *const kCATransitionSuckEffect = @"suckEffect";
NSString *const kCATransitionOglFlip = @"oglFlip";
NSString *const kCATransitionRippleEffect = @"rippleEffect";
NSString *const kCATransitionPageCurl = @"pageCurl";
NSString *const kCATransitionPageUnCurl = @"pageUnCurl";
NSString *const kCATransitionCameraIrisHollowOpen = @"cameraIrisHollowOpen";
NSString *const kCATransitionCameraIrisHollowClose = @"cameraIrisHollowClose";
subtype:動(dòng)畫(huà)類型的方向
CA_EXTERN NSString * const kCATransitionFromRight;
CA_EXTERN NSString * const kCATransitionFromLeft;
CA_EXTERN NSString * const kCATransitionFromTop;
CA_EXTERN NSString * const kCATransitionFromBottom;
上面講的是給window.layer添加transition,這樣使得在present或push時(shí)使用指定的轉(zhuǎn)場(chǎng)動(dòng)畫(huà)。
既然講到這里了,就看一下把transition加在layer上。
func imageswitchClick()
{
let animation = CATransition()
animation.type = .push // CATransitionType(rawValue: "cube")
animation.subtype = CATransitionSubtype.fromRight
animation.duration = 1.0
self.imageView.layer.add(animation, forKey: nil)
let index = Int(arc4random() % 4)
self.imageView.image = UIImage(named: images[index])
}