视图在被呈现后被UITransitionView阻止

时间:2016-03-21 22:10:00

标签: ios uiviewcontroller uinavigationcontroller uiviewanimationtransition catransition

我有一个侧导航控制器并通过UIButton呈现它。当我通过[self presentviewcontroller: NC animated: YES completion: nil]直接将这个NC作为根视图控制器时,某些原因导致NC的菜单面被UITransitionView阻挡,我无法消失。

我附上了view hierarchy的图片。 Here是另一个。

我尝试了以下内容:

UIWindow *window = [(AppDelegate *)[[UIApplication sharedApplication] delegate] window];
    window.backgroundColor = kmain;


    CATransition* transition = [CATransition animation];
    transition.duration = .5;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromTop;

    [nc.view.layer addAnimation:transition forKey:kCATransition];

    [UIView transitionWithView:window
                      duration:0.5
                       options:UIViewAnimationOptionTransitionNone
                    animations:^{ window.rootViewController = nc; }
                    completion:^(BOOL finished) {
                        for (UIView *subview in window.subviews) {
                            if ([subview isKindOfClass:NSClassFromString(@"UITransitionView")]) {
                                [subview removeFromSuperview];
                            }
                        }
                    }];

但它非常hacky,并且随着窗口的rootviewcontroller在转换过程中发生变化,它有点不稳定,部分导航控制器和右上角变黑。看起来很糟糕。

6 个答案:

答案 0 :(得分:6)

要通过UITransitionView获取点按事件,请将containerView' userInteractionEnabled设置为false。这是因为您使用UIViewControllerAnimatedTransitioning进行自定义过渡动画。

示例,在animateTransition(_:)

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

    let containerView = transitionContext.containerView()        
    containerView.userInteractionEnabled = false

    ...
}

答案 1 :(得分:1)

我有同样的问题,但在一个不同的场景中,我最终做了一些非常相似的事情来找到视图,但是我没有删除可能更有问题的视图,而是禁用了用户交互,因此任何触摸事件都会抛出它任何其他对象都可以处理用户的交互。 在我的情况下,这只是在将应用程序更新到iOS 10之后才出现,在iOS 9中运行的相同代码并不属于此。

答案 2 :(得分:1)

我遇到了同样的问题,这解决了我的问题,

navigationController.setNavigationBarHidden(true,animated:false)

这对我有用,因为我在视图控制器中将自定义视图作为导航栏。

答案 3 :(得分:0)

我在弹出视图控制器上设置accessibilityElements时遇到了这个问题。我通过删除分配元素数组来解决此问题。

答案 4 :(得分:0)

我有一个类似的问题,即UITransitionView不断阻止我的视图,从而阻止了任何用户交互。

在我的情况下,这是由于自定义动画UIViewController转换未完成。

我忘记使用以下方法正确完成转换:

TransitionContext.completeTransition(transitionContext.transitionWasCancelled)

TransitionContext.completeTransition(!transitionContext.transitionWasCancelled)

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {}

来自UIViewControllerAnimatedTransitioning协议

答案 5 :(得分:0)

在我的情况下,我需要一个HalfSize视图控制器。我遵循了this answer which worked great,直到意识到仍然需要与正在呈现的vc(halfSizeVC后面的vc)进行交互。

关键是必须将这两个框架都设置为相同的CGRect值:

halfSizeVC.frame = CGRect(x: 0, y: UIScreen.main.bounds.height / 2, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)

containerView = CGRect(x: 0, y: UIScreen.main.bounds.height / 2, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)

这是从 ViewController HalfSizeController 的代码,并使 HalfSizeController 1/2成为屏幕尺寸。即使屏幕上显示halfSizeVC,您仍然可以与它后面的vc的上半部分进行交互。

如果您想触摸HalfSizeVC内部的某些内容,则还必须制作一个PassthroughView类。我将其包含在底部。

正在显示的vc是白色的,底部带有紫色按钮。点击紫色按钮将弹出红色halfSizeVC。

vc / presentingVC:

import UIKit

class ViewController: UIViewController {

    lazy var purpleButton: UIButton = {
        let button = UIButton(type: .system)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitle("Tap to Present HalfSizeVC", for: .normal)
        button.setTitleColor(UIColor.white, for: .normal)
        button.backgroundColor = UIColor.systemPurple
        button.addTarget(self, action: #selector(purpleButtonPressed), for: .touchUpInside)
        button.layer.cornerRadius = 7
        button.layer.masksToBounds = true
        return button
    }()

    var halfSizeVC: HalfSizeController?

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        // tap gesture on vc will dismiss HalfSizeVC
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissHalfSizeVC))
        view.addGestureRecognizer(tapGesture)
    }


    // tapping the purple button presents HalfSizeVC
    @objc func purpleButtonPressed() {

        halfSizeVC = HalfSizeController()

        // *** IMPORTANT ***
        halfSizeVC!.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height / 2)

        halfSizeVC!.modalPresentationStyle = .custom

        present(halfSizeVC!, animated: true, completion: nil)
    }

    // dismiss HalfSizeVC by tapping anywhere on the white background
    @objc func dismissHalfSizeVC() {

        halfSizeVC?.dismissVC()
    }
}

halfSizeVC / presentedVC

import UIKit

class HalfSizeController: UIViewController {

    init() {
        super.init(nibName: nil, bundle: nil)
        modalPresentationStyle = .custom
        transitioningDelegate = self
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    lazy var topHalfDummyView: PassthroughView = {
        let view = PassthroughView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .clear
        view.isUserInteractionEnabled = true
        return view
    }()

    var isPresenting = false
    let halfScreenHeight = UIScreen.main.bounds.height / 2

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .red

        setAnchors()
    }

    private func setAnchors() {
    
        view.addSubview(topHalfDummyView)
        topHalfDummyView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        topHalfDummyView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        topHalfDummyView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        topHalfDummyView.heightAnchor.constraint(equalToConstant: halfScreenHeight).isActive = true
    }

    public func dismissVC() {
        dismiss(animated: true, completion: nil)
    }
}

extension HalfSizeController: UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {

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

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

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

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

        let containerView = transitionContext.containerView

        // *** IMPORTANT ***
        containerView.frame = CGRect(x: 0, y: halfScreenHeight, width: UIScreen.main.bounds.width, height: halfScreenHeight)

        let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
        guard let toVC = toViewController else { return }
        isPresenting = !isPresenting

        if isPresenting == true {
            containerView.addSubview(toVC.view)

            topHalfDummyView.frame.origin.y += halfScreenHeight

            UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseOut], animations: {

                self.topHalfDummyView.frame.origin.y -= self.halfScreenHeight

            }, completion: { (finished) in
                transitionContext.completeTransition(true)
            })

        } else {
            UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseOut], animations: {
            
            }, completion: { (finished) in
                self.topHalfDummyView.frame.origin.y += self.halfScreenHeight
                transitionContext.completeTransition(true)
            })
        }
    }
}

HalfSizeVC中的topHalfDummyView需要PassthroughView

import UIKit

class PassthroughView: UIView {
    
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        print("Passing all touches to the next view (if any), in the view stack.")
        return false
    }
}

在按下紫色按钮之前:

enter image description here

按下紫色按钮后:

enter image description here

如果您按下白色背景,则红色将消失

您只需c + p所有3个文件并运行您的项目