自定义弹出消失视图

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/Draven__/article/details/89380049

自定义弹出消失视图 

git 分支仓库 https://gitee.com/zjf1998/DeviceManage-swift/tree/%E5%BC%B9%E5%87%BA%E6%B6%88%E5%A4%B1%E8%A7%86%E5%9B%BE/

目录

自定义弹出消失视图... 1

1.     新建弹出视图ViewController 2

2.点击标题弹出控制器... 2

3.改变弹出View的frame. 3

4.改变弹出View的frame. 4

5. 弹出和消失动画代理的方法... 5

6. 改变titleBtn的状态... 6

 

  1. 新建弹出视图ViewController

import UIKit



class PopoverViewController: UIViewController {



    override func viewDidLoad() {

        super.viewDidLoad()

    }

}

2.点击标题弹出控制器

@objc private func titleBtnClick(titleBtn : TitleButton) {

        // 1.创建弹出的控制器

        let popoverVc = PopoverViewController()

       

        // 2.设置控制器的modal样式

        popoverVc.modalPresentationStyle = .custom

       

        // 3.设置转场的代理

        popoverVc.transitioningDelegate = popoverAnimator

        popoverAnimator.presentedFrame = CGRect(x: 100, y: 55, width: 180, height: 250)

       

        // 4.弹出控制器

        present(popoverVc, animated: true, completion: nil)

}

3.改变弹出View的frame

封装一个XMGPresentationController,继承自UIPresentationController转场动画ViewController。

import UIKit



class XMGPresentationController: UIPresentationController {

    // MARK:- 对外提供属性

    var presentedFrame : CGRect = CGRect.zero

   

    // MARK:- 懒加载属性

    private lazy var coverView : UIView = UIView()

   

    // MARK:- 系统回调函数

    override func containerViewWillLayoutSubviews() {

        super.containerViewWillLayoutSubviews()

       

        // 1.设置弹出View的尺寸

        presentedView?.frame = presentedFrame

       

        // 2.添加蒙版

        setupCoverView()

    }

}



// MARK:- 设置UI界面相关

extension XMGPresentationController {

    private func setupCoverView() {

        // 1.添加蒙版

        containerView?.insertSubview(coverView, at: 0)

       

        // 2.设置蒙版的属性

        coverView.backgroundColor = UIColor(white: 0.8, alpha: 0.2)

        coverView.frame = containerView!.bounds

       

        // 3.添加手势

        let tapGes = UITapGestureRecognizer(target: self, action: #selector(coverViewClick))

        coverView.addGestureRecognizer(tapGes)

    }

}



// MARK:- 事件监听

extension XMGPresentationController {

    @objc private func coverViewClick() {

        presentedViewController.dismiss(animated: true, completion: nil)

    }

}

4.改变弹出View的frame

// MARK:- 自定义转场代理的方法

extension PopoverAnimator : UIViewControllerTransitioningDelegate {

    // 目的:改变弹出View的尺寸

    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {

        let presentation = XMGPresentationController(presentedViewController: presented, presenting: presenting)

        presentation.presentedFrame = presentedFrame

       

        return presentation

    }

   

    // 目的:自定义弹出的动画

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {

       

        isPresented = true

        callBack!(isPresented)

       

        return self

    }

   

    // 目的:自定义消失的动画

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {

        isPresented = false

        callBack!(isPresented)

       

        return self

    }

   

}

 

5. 弹出和消失动画代理的方法

extension PopoverAnimator : UIViewControllerAnimatedTransitioning {

    /// 动画执行的时间

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {

        return 0.5

    }

   

    /// 获取`转场的上下文`:可以通过转场上下文获取弹出的View和消失的View

    // UITransitionContextFromViewKey : 获取消失的View

    // UITransitionContextToViewKey : 获取弹出的View

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

        isPresented ? animationForPresentedView(transitionContext: transitionContext) : animationForDismissedView(transitionContext: transitionContext)

    }

   

    /// 自定义弹出动画

    private func animationForPresentedView(transitionContext: UIViewControllerContextTransitioning) {

        // 1.获取弹出的View

        let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to)!

       

        // 2.将弹出的View添加到containerView中

        transitionContext.containerView.addSubview(presentedView)

      

        // 3.执行动画

        presentedView.transform = CGAffineTransform(scaleX: 1.0, y: 0.0)

        presentedView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)

        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {

             presentedView.transform = CGAffineTransform.identity

        }) { (_) in

            // 必须告诉转场上下文你已经完成动画

            transitionContext.completeTransition(true)

        }

    }

   

    /// 自定义消失动画

    private func animationForDismissedView(transitionContext: UIViewControllerContextTransitioning) {

        // 1.获取消失的View

        let dismissView = transitionContext.view(forKey: UITransitionContextViewKey.from)

       

        // 2.执行动画

        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {

            dismissView?.transform = CGAffineTransform(scaleX: 1.0, y: 0.00001)

        }) { (_) in

            dismissView?.removeFromSuperview()

            // 必须告诉转场上下文你已经完成动画

            transitionContext.completeTransition(true)

        }

    }

}

6. 改变titleBtn的状态

1.自定义构造函数

// 注意:如果自定义了一个构造函数,但是没有对默认构造函数init()进行重写,那么自定义的构造函数会覆盖默认的init()构造函数

    init(callBack : @escaping (Bool) -> ()) {

        self.callBack = callBack

}

2.调用闭包

  // 目的:自定义弹出的动画

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {

       

        isPresented = true

        callBack!(isPresented)

       

        return self

    }

   

    // 目的:自定义消失的动画

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {

        isPresented = false

        callBack!(isPresented)

       

        return self

}

3.调用初始化方法

private lazy var popoverAnimator : PopoverAnimator = PopoverAnimator {[weak self] (presented) -> () in

        self?.titleBtn.isSelected = presented

    }

 

 

 

 

猜你喜欢

转载自blog.csdn.net/Draven__/article/details/89380049