ios – 如何在半屏幕上呈现ViewController

ios – 如何在半屏幕上呈现ViewController,第1张

概述我有一个UIViewController只有一个UIView,从底部覆盖了viewController的1/3.像这样 我想在另一个ViewController上呈现这个viewController.它应该从底部动画出现,它应该忽略到底部动画. 但我不希望它覆盖整个屏幕.显示它的viewController应该在后面可见. 这似乎是一个基本问题但我无法完成它.有人可以指点我的方向吗? 编辑: 这就 我有一个UIVIEwController只有一个UIVIEw,从底部覆盖了vIEwController的1/3.像这样

我想在另一个VIEwController上呈现这个vIEwController.它应该从底部动画出现,它应该忽略到底部动画.

但我不希望它覆盖整个屏幕.显示它的vIEwController应该在后面可见.

这似乎是一个基本问题但我无法完成它.有人可以指点我的方向吗?

编辑:

这就是我迄今为止所尝试过的.我创建了这些类

// MARK: -class MyFadeInFadeOutTransitioning: NSObject,UIVIEwControllerTransitioningDelegate {var backgroundcolorAlpha: CGfloat = 0.5var shouldismiss = falsefunc animationController(forPresented presented: UIVIEwController,presenting: UIVIEwController,source: UIVIEwController) -> UIVIEwControllerAnimatedTransitioning? {    let fadeInPresentAnimationController = MyFadeInPresentAnimationController()        fadeInPresentAnimationController.backgroundcolorAlpha = backgroundcolorAlpha    return fadeInPresentAnimationController}func animationController(fordismissed dismissed: UIVIEwController) -> UIVIEwControllerAnimatedTransitioning? {    let fadeOutdismissAnimationController = MyFadeOutdismissAnimationController()    return fadeOutdismissAnimationController}}// MARK: -class MYFadeInPresentAnimationController: NSObject,UIVIEwControllerAnimatedTransitioning {let kPresentationDuration = 0.5var backgroundcolorAlpha: CGfloat?func TransitionDuration(using TransitionContext: UIVIEwControllerContextTransitioning?) -> TimeInterval {    return kPresentationDuration}func animateTransition(using TransitionContext: UIVIEwControllerContextTransitioning) {    let toVIEwController = TransitionContext.vIEwController(forKey: UITransitionContextVIEwControllerKey.to)!    toVIEwController.vIEw.backgroundcolor = UIcolor.clear    let toVIEwFrame = TransitionContext.finalFrame(for: toVIEwController)    let containerVIEw = TransitionContext.containerVIEw    if let pickerContainerVIEw = toVIEwController.vIEw.vIEwWithTag(kContainerVIEwTag) {        let transform = CGAffinetransform(translationX: 0.0,y: pickerContainerVIEw.frame.size.height)        pickerContainerVIEw.transform = transform    }    toVIEwController.vIEw.frame = toVIEwFrame    containerVIEw.addSubvIEw(toVIEwController.vIEw)    UIVIEw.animate(withDuration: 0.3,delay: 0.0,options: .curvelinear,animations: {        toVIEwController.vIEw.backgroundcolor = UIcolor(white: 0.0,Alpha: self.backgroundcolorAlpha!)        if let pickerContainerVIEw = toVIEwController.vIEw.vIEwWithTag(kContainerVIEwTag) {            pickerContainerVIEw.transform = CGAffinetransform.IDentity        }    }) { (finished) in        TransitionContext.completeTransition(true)    }}}// MARK: -class MYFadeOutdismissAnimationController: NSObject,UIVIEwControllerAnimatedTransitioning {let kdismissalDuration = 0.15func TransitionDuration(using TransitionContext: UIVIEwControllerContextTransitioning?) -> TimeInterval {    return kdismissalDuration}func animateTransition(using TransitionContext: UIVIEwControllerContextTransitioning) {    let fromVIEwController = TransitionContext.vIEwController(forKey: UITransitionContextVIEwControllerKey.from)!    let toVIEwController = TransitionContext.vIEwController(forKey: UITransitionContextVIEwControllerKey.to)!    let containerVIEw = TransitionContext.containerVIEw    containerVIEw.addSubvIEw(toVIEwController.vIEw)    containerVIEw.sendSubvIEw(toBack: toVIEwController.vIEw)    UIVIEw.animate(withDuration: kdismissalDuration,animations: {        //            fromVIEwController.vIEw.backgroundcolor = UIcolor.clearcolor()        //            if let pickerContainerVIEw = toVIEwController.vIEw.vIEwWithTag(kContainerVIEwTag) {        //                let transform = CGAffinetransformMakeTranslation(0.0,pickerContainerVIEw.frame.size.height)        //                pickerContainerVIEw.transform = transform        //            }        fromVIEwController.vIEw.Alpha = 0.0    }) { (finished) in        let canceled: Bool = TransitionContext.TransitionWasCancelled        TransitionContext.completeTransition(true)        if !canceled {            UIApplication.shared.keyWindow?.addSubvIEw(toVIEwController.vIEw)        }    }}}

在正在呈现的vIEwController中,我正在做如下

var customTransitioningDelegate: MYFadeInFadeOutTransitioning? = MYFadeInFadeOutTransitioning()    init() {    super.init(nibname: "SomeNibname",bundle: Bundle.main)    TransitioningDelegate = customTransitioningDelegate    modalPresentationStyle = .custom    customTransitioningDelegate?.backgroundcolorAlpha = 0.0}

它确实呈现了vIEwController,我也可以看到背景vIEwController.但我希望它能从底部呈现动画效果.用动画解雇底部.我怎样才能做到这一点 ?

解决方法 如果你想在半个屏幕上呈现一个视图控制器,我建议使用UIPresentationController类,它允许你在显示时设置视图控制器的框架.一个建议,这个方法将停止presentVIEwController的用户交互,直到你解雇presentVIEwController,所以如果你想在屏幕的一半显示视图控制器同时保持用户与presentsVIEwController的交互,你应该像其他答案一样使用容器视图建议.
这是UIPresentationController类的一个例子,可以执行您想要的 *** 作
import UIKitclass ForgotPassWordPresentationController: UIPresentationController{    let blurEffectVIEw: UIVisualEffectVIEw!    var tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer()    func dismiss(){        self.presentedVIEwController.dismiss(animated: true,completion: nil)    }    overrIDe init(presentedVIEwController: UIVIEwController,presenting presentingVIEwController: UIVIEwController?) {        let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)        blurEffectVIEw = UIVisualEffectVIEw(effect: blurEffect)        super.init(presentedVIEwController: presentedVIEwController,presenting: presentingVIEwController)        tapGestureRecognizer = UITapGestureRecognizer(target: self,action: #selector(self.dismiss))        blurEffectVIEw.autoresizingMask = [.flexibleWIDth,.flexibleHeight]        self.blurEffectVIEw.isUserInteractionEnabled = true        self.blurEffectVIEw.addGestureRecognizer(tapGestureRecognizer)    }    overrIDe var frameOfPresentedVIEwInContainerVIEw: CGRect{        return CGRect(origin: CGPoint(x: 0,y: self.containerVIEw!.frame.height/2),size: CGSize(wIDth: self.containerVIEw!.frame.wIDth,height: self.containerVIEw!.frame.height/2))    }    overrIDe func dismissalTransitionWillBegin() {        self.presentedVIEwController.TransitionCoordinator?.animate(alongsIDeTransition: { (UIVIEwControllerTransitionCoordinatorContext) in            self.blurEffectVIEw.Alpha = 0        },completion: { (UIVIEwControllerTransitionCoordinatorContext) in            self.blurEffectVIEw.removeFromSupervIEw()        })    }    overrIDe func presentationTransitionWillBegin() {        self.blurEffectVIEw.Alpha = 0        self.containerVIEw?.addSubvIEw(blurEffectVIEw)        self.presentedVIEwController.TransitionCoordinator?.animate(alongsIDeTransition: { (UIVIEwControllerTransitionCoordinatorContext) in            self.blurEffectVIEw.Alpha = 1        },completion: { (UIVIEwControllerTransitionCoordinatorContext) in        })    }    overrIDe func containerVIEwWillLayoutSubvIEws() {        super.containerVIEwWillLayoutSubvIEws()        presentedVIEw!.layer.masksToBounds = true        presentedVIEw!.layer.cornerRadius = 10    }    overrIDe func containerVIEwDIDLayoutSubvIEws() {        super.containerVIEwDIDLayoutSubvIEws()        self.presentedVIEw?.frame = frameOfPresentedVIEwInContainerVIEw        blurEffectVIEw.frame = containerVIEw!.bounds    }}

当您在presentVIEwController框架外部点击时,这还会添加模糊视图和点按以关闭.您需要设置presentVIEwController的TransitioningDelegate并实现

presentationController(forPresented呈现:UIVIEwController,呈现:UIVIEwController?,source:UIVIEwController) – > UIPresentationController?

在那里的方法.别忘了也设置
modalPresentationStyle = .custom
presentVIEwController的内容

我发现UIPresentationController的用法是一种更清洁的方法.祝好运

总结

以上是内存溢出为你收集整理的ios – 如何在半屏幕上呈现ViewController全部内容,希望文章能够帮你解决ios – 如何在半屏幕上呈现ViewController所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1103796.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存