基本信息
源码名称:iOS弹出层设置(Presentation.swift)
源码大小:6.89KB
文件格式:.swift
开发语言:Swift
更新时间:2021-04-20
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 4 元 
   源码介绍

   Present方式弹出设置


public class Presentation: UIPresentationController {

    lazy fileprivate var blackView: UIView = {

        let view = UIView()

        if let frame = self.containerView?.bounds {

            view.frame = frame

        }

        return view

    }()

    override public func presentationTransitionWillBegin() {

        presentedView?.frame = UIScreen.main.bounds

        containerView?.addSubview(blackView)

        containerView?.addSubview(presentedView ?? UIView())


        presentingViewController.transitionCoordinator?.animateAlongsideTransition(in: blackView, animation: { (context) in

            self.blackView.backgroundColor =  colorLiteral(red: 0, green: 0, blue: 0, alpha: 1).withAlphaComponent(0.5)

        }, completion: { (context) in

            

        })

    }

    override public func dismissalTransitionWillBegin() {

        presentingViewController.transitionCoordinator?.animateAlongsideTransition(in: blackView, animation: { (context) in

            self.blackView.alpha = 0

        }, completion: { (context) in

            

        })

    }

    override public func dismissalTransitionDidEnd(_ completed: Bool) {

        if completed {

            presentedView?.removeFromSuperview()

            blackView.removeFromSuperview()

        }

    }

    

}


//动画的类

class AnimatedTransitioning: NSObject, UIViewControllerAnimatedTransitioning {

    

    var present: Bool = true

    var direction: Direction = .Up

    var dismissDirection: Direction = .Up

    

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

        return 0.65

    }

    

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

        if present {

            showDirecton(dir: direction, transitionContext: transitionContext)

        }else{

            showDirecton(dir: dismissDirection, transitionContext: transitionContext)

        }

        

    }

    fileprivate func showDirecton(dir: Direction, transitionContext: UIViewControllerContextTransitioning){

        

        if present {

            //创建控制器

            let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)

            

            switch dir {

            case.down:

                //从上向下

                 toView?.frame.origin.y = -(toView?.frame.height ?? UIScreen.main.bounds.height)

                break

            case .Up:

                //从下向上

                 toView?.frame.origin.y = toView?.frame.height ?? UIScreen.main.bounds.height

                 break

            case .left:

                //从左向右

                 toView?.frame.origin.x = -(toView?.frame.width ?? UIScreen.main.bounds.width)

                 break

            case .right:

                //从右向左

                toView?.frame.origin.x = toView?.frame.width ?? UIScreen.main.bounds.width

                 break


            }


            UIView.animate(withDuration: 0.45, delay: 0.0, usingSpringWithDamping: 0.90, initialSpringVelocity: 1.0 / 0.5, options: .curveEaseInOut, animations: {

                switch dir{

                case .Up, .down:

                    toView?.frame.origin.y = 0

                    break

                case .left, .right:

                    toView?.frame.origin.x = 0

                    break

                }

            }) { (finished) in

                transitionContext.completeTransition(true)

            }

            

        }else{

            

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

            //销毁控制器

            

            UIView.animate(withDuration: 0.45, delay: 0.0, usingSpringWithDamping: 0.90, initialSpringVelocity: 1.0 / 0.5, options: .curveEaseInOut, animations: {

                switch dir{

                case .Up:

                    fromView?.frame.origin.y = fromView?.frame.height ?? UIScreen.main.bounds.height

                    break

                case .down:

                    fromView?.frame.origin.y = -(fromView?.frame.height ?? UIScreen.main.bounds.height)

                    break

                case .left:

                    fromView?.frame.origin.x = -(fromView?.frame.width ?? UIScreen.main.bounds.width)

                    break

                case .right:

                    fromView?.frame.origin.x = fromView?.frame.width ?? UIScreen.main.bounds.width

                    break

                }

            }) { (finished) in

                transitionContext.completeTransition(true)

            }

//            UIView.animate(withDuration: 0.45, delay: 0.0, options: [.curveEaseInOut], animations: {

//

//

//            }) { (finished) in

//                transitionContext.completeTransition(true)

//            }

        }


    }


}