iOS轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的三種方式

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])
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容