1. 設(shè)置控制器屬性
// 設(shè)置modal樣式,彈出后原來的view不會(huì)消失,默認(rèn)modal出來的控制器view會(huì)將原來的view移除
popoverVc.modalPresentationStyle = .custom
// 設(shè)置轉(zhuǎn)場代理
popoverVc.transitioningDelegate = self
2. 設(shè)置轉(zhuǎn)場代理方法
// MARK:- transitioningDelegate代理
extension GGHomeViewController : UIViewControllerTransitioningDelegate {
/// 目的:改變彈出view的frame憋槐,需要自定義UIPresentationController
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return GGPresentationController(presentedViewController: presented, presenting: presenting)
}
/// 目的:設(shè)置彈出動(dòng)畫
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
isPresented = true // 由于彈出和消失用的是同一個(gè)協(xié)議婆殿,所需需要設(shè)置一個(gè)標(biāo)志位來區(qū)分是彈出還是消失
return self
}
/// 目的:設(shè)置消失動(dòng)畫
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
isPresented = false
return self
}
}
2.1 自定義UIPresentationController
class GGPresentationController: UIPresentationController {
// MARK:- 懶加載屬性
fileprivate lazy var coverView : UIView = UIView()
// MARK:- 系統(tǒng)函數(shù)回調(diào)
override func containerViewWillLayoutSubviews() {
super.containerViewWillLayoutSubviews()
// 設(shè)置frame
presentedView?.frame = CGRect(x: 100, y: 55, width: 180, height: 250)
setupCoverView()
}
}
// MARK:- 設(shè)置蒙版
extension GGPresentationController {
fileprivate func setupCoverView() {
// 1. 添加蒙版
containerView?.insertSubview(coverView, at: 0)
// 2. 設(shè)置屬性
coverView.frame = containerView!.bounds
coverView.backgroundColor = UIColor(white: 0.6, alpha: 0.2)
// 3. 給蒙版添加手勢事件
let tabGes = UITapGestureRecognizer(target: self, action: #selector(GGPresentationController.coverViewTapGesClk))
coverView.addGestureRecognizer(tabGes)
}
}
// MARK:- 手勢點(diǎn)擊處理
extension GGPresentationController {
@objc fileprivate func coverViewTapGesClk() {
print("coverViewTapGesClk")
presentedViewController.dismiss(animated: true, completion: nil)
}
}
3. 設(shè)置彈出载矿、消失動(dòng)畫的協(xié)議
// MARK:- UIViewControllerAnimatedTransitioning協(xié)議
extension GGHomeViewController : UIViewControllerAnimatedTransitioning {
/// 設(shè)置動(dòng)畫播放事件
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.5
}
/// 設(shè)置動(dòng)畫效果
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
isPresented ? animationToPresentedView(using: transitionContext) : animationToDismissedView(using: transitionContext)
}
// 彈出動(dòng)畫
fileprivate func animationToPresentedView(using transitionContext: UIViewControllerContextTransitioning) {
// 1. 獲取彈出的view
let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
// 2. 添加view
transitionContext.containerView.addSubview(presentedView)
// 3. 設(shè)置動(dòng)畫
presentedView.transform = CGAffineTransform(scaleX: 1.0, y: 0.0)
presentedView.layer.anchorPoint = CGPoint(x: 0.5, y: 0) // 動(dòng)畫的開始位置
UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
presentedView.transform = CGAffineTransform.identity
}) { (_) in
// 4. 完成動(dòng)畫
transitionContext.completeTransition(true)
}
}
// 消失動(dòng)畫
// 返回的對(duì)象是一個(gè)協(xié)議板辽,直接設(shè)置成當(dāng)前對(duì)象
fileprivate func animationToDismissedView(using transitionContext: UIViewControllerContextTransitioning) {
// 1. 獲取彈出的view
let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
// 2. 設(shè)置動(dòng)畫
UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
presentedView.transform = CGAffineTransform(scaleX: 1.0, y: 0.001)
}) { (_) in
// 3. 完成動(dòng)畫
transitionContext.completeTransition(true)
}
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者