Created
September 5, 2022 07:45
-
-
Save liuzhida33/876db0b2234cbdc45f4ad68336a684ed to your computer and use it in GitHub Desktop.
适配Uniapp打开方式自定义弹出动画(包裹导航控制器)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Modal 转场动画 | |
class PresentTransition: NSObject, UIViewControllerAnimatedTransitioning { | |
enum Transition { | |
case present | |
case dismiss | |
} | |
private var tran: Transition = .present | |
init(_ tran: Transition) { | |
self.tran = tran | |
} | |
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { | |
return 0.35 | |
} | |
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { | |
switch tran { | |
case .present: | |
presentTransition(transitionContext: transitionContext) | |
case .dismiss: | |
dismissTransition(transitionContext: transitionContext) | |
} | |
} | |
private func presentTransition(transitionContext: UIViewControllerContextTransitioning) { | |
let toVC = transitionContext.viewController(forKey: .to) | |
guard let toView = toVC?.view else { return } | |
transitionContext.containerView.addSubview(toView) | |
toView.transform = CGAffineTransform(translationX: 0, y: UIScreen.main.bounds.height) | |
UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, options: .curveEaseIn, animations: { | |
toView.transform = CGAffineTransform(translationX: 0, y: 0) | |
}, completion: { _ in | |
transitionContext.completeTransition(true) | |
}) | |
} | |
private func dismissTransition(transitionContext: UIViewControllerContextTransitioning) { | |
let fromVC = transitionContext.viewController(forKey: .from) | |
guard let fromView = fromVC?.view else { return } | |
UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, options: .curveEaseOut, animations: { | |
fromView.transform = CGAffineTransform.identity.translatedBy(x: 0, y: UIScreen.main.bounds.height) | |
}, completion: { _ in | |
transitionContext.completeTransition(!transitionContext.transitionWasCancelled) | |
}) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIViewController { | |
static func setupSwizzle() { | |
// 适配Uniapp | |
swizzleMethod(originalCls: UIViewController.self, | |
originalSelector: #selector(UIViewController.present(_:animated:completion:)), | |
swizzledCls: UIViewController.self, | |
swizzledSelector: #selector(UIViewController.swizzledPresent(_:animated:completion:))) | |
// 适配Uniapp | |
swizzleMethod(originalCls: UIViewController.self, | |
originalSelector: #selector(UIViewController.viewWillAppear(_:)), | |
swizzledCls: UIViewController.self, | |
swizzledSelector: #selector(UIViewController.swizzledViewWillAppear(_:))) | |
} | |
@objc func swizzledPresent(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) { | |
if String(describing: type(of: viewControllerToPresent)) == "DCUniMPViewController" { | |
// 替换Uniapp弹出容器为NavigationController | |
let container = UniNavigationController(rootViewController: viewControllerToPresent) | |
self.swizzledPresent(container, animated: flag, completion: completion) | |
} else { | |
self.swizzledPresent(viewControllerToPresent, animated: flag, completion: completion) | |
} | |
} | |
@objc func swizzledViewWillAppear(_ animated: Bool) { | |
if String(describing: type(of: self)) == "DCUniMPViewController" { | |
// 设置Uniapp页面隐藏导航 | |
self.navigationController?.setNavigationBarHidden(true, animated: true) | |
} | |
self.swizzledViewWillAppear(animated) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment