我有一个侧面导航控制器并通过 UIButton 显示它。当我直接通过
[self presentviewcontroller: NC animated: YES completion: nil]
使这个NC成为根视图控制器时,出于某种原因,NC的菜单侧被我无法消失的UITransitionView
阻挡。
我试过以下方法:
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在转换过程中发生变化,它有点不稳定并且部分navigationcontroller和右上角变黑了。看起来很糟糕。
要通过
UITransitionView
获取点击事件,请将containerView
的userInteractionEnabled
设置为false
。如果您正在使用 UIViewControllerAnimatedTransitioning
制作自定义过渡动画。
例如,在您的
animateTransition(_:)
中:
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView
containerView.isUserInteractionEnabled = false
...
}
在我的情况下,我需要一个 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
}
}
在按下紫色按钮之前:
按下紫色按钮后:
如果你按下白色背景,红色将消失
您只需 c+p 所有 3 个文件并运行您的项目
我有一个类似的问题,其中 UITransitionView 一直阻止我的视图,阻止任何用户交互。
在我的例子中,这是由于未完成的自定义动画 UIViewController 转换。
我忘记正确完成我的过渡:
TransitionContext.completeTransition(transitionContext.transitionWasCancelled)
或
TransitionContext.completeTransition(!transitionContext.transitionWasCancelled)
在
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {}
来自
UIViewControllerAnimatedTransitioning
协议
我遇到了同样的问题,但情况有所不同。我最终做了一些非常相似的事情来找到视图,但我没有删除可能更成问题的视图,而是禁用了用户交互,所以任何触摸事件都会抛出它,任何其他对象都可以处理用户的交互。
在我的例子中,这仅在将应用程序更新到 iOS 10 后出现,而在 iOS 9 中运行的相同代码不属于此。
我遇到了同样的问题,这解决了我的问题,
navigationController.setNavigationBarHidden(true, animated: false)
这对我有用,因为我在视图控制器中将自定义视图作为导航栏。
我在弹出视图控制器上设置 accessibilityElements 时遇到了这个问题。我通过删除分配元素数组来修复它。