iOS转场动画的三种方式

1.CATransition

CATransition是CAAnimation的子类,用于过渡动画或转场动画。为视图层移入移除屏幕提供转场动画。首先来看一下简单的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)

将该动画添加到window.layer上,则会present或push时使用指定的转场动画。

其中最主要的两个属性就是type和subtype。

type:转场动画的类型。
官方SDK只提供了四种转场动画的类型,即:

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:动画类型的方向

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时使用指定的转场动画。
既然讲到这里了,就看一下把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])
}

你可能感兴趣的:(iOS转场动画的三种方式)