20210716, 发现一些网上的退场动画,跑一下,一脸懵
本文探讨下,怎样解决
入场动画,很简单
-
指定一下动画时间,通过
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval
-
如何动画, 通过
func animateTransition(using transitionContext: UIViewControllerContextTransitioning)
左手一个 from View Controller , 上一个控制器,
右手一个 to View Controller , 下一个控制器,
让上一个消失,让下一个出现
UIView.animate
调一下动画属性,
一般就是改一下视图 frame ,完了
下面的代码,效果比较简单
一般 present 一个控制器,由下而上,
这里可以做到,从上下左右,都可以推出控制器
enum PresentingDirection{
case top, right, left, bottom
var bounds: CGRect{
UIScreen.main.bounds
}
func offsetF(withFrame viewFrame: CGRect) -> CGRect{
let h = bounds.size.height
let w = bounds.size.width
switch self {
case .top:
return viewFrame.offsetBy(dx: 0, dy: -h)
case .bottom:
return viewFrame.offsetBy(dx: 0, dy: h)
case .left:
return viewFrame.offsetBy(dx: -w, dy: 0)
case .right:
return viewFrame.offsetBy(dx: w, dy: 0)
}
}
}
class CustomPresentationController: NSObject, UIViewControllerAnimatedTransitioning{
fileprivate var presentingDirection: PresentingDirection
init(direction orientation: PresentingDirection) {
presentingDirection = orientation
}
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 1
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromCtrl = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from),
let toCtrl = transitionContext.viewController(forK