【问题标题】:Pass touch to other viewcontroller that is clear将触摸传递给其他清晰的视图控制器
【发布时间】:2016-09-02 20:37:04
【问题描述】:

我正在寻找一种从位于另一个 ViewController 之上的清晰 ViewController 传递触摸的方法。我已经看到了以下帖子:

How to ignore touch events and pass them to another subview's UIControl objects?

我按照他们的提示进行操作,但不幸的是无法正常工作。

我从上到下得到如下视图结构:

VideoViewController (note: clear ViewController can see the buttons underneath)
| passThroughView (note: UIView that is using the PassThroughView subclass)
| collectionView
CameraViewController
| controlContainer
| | recordButton

当在collectionview 外部和passThroughView 后面的recordbutton 上点击时,它应该执行一个新记录。问题是使用以下代码我无法做到这一点:

class PassThroughView: UIView {
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let hitView = super.hitTest(point, with: event)!
        let cameraVC = CameraViewController()

        if hitView == self {
            return cameraVC.recordButton
        }

        return hitView
    }
}

一旦我的 passThroughView 被点击,我会在 if 语句中打印,但它不会将我的触摸返回到其他 ViewControllers 按钮。

那么我在这里做错了什么?

【问题讨论】:

    标签: ios swift uiviewcontroller touch


    【解决方案1】:

    我更喜欢使用 TapGestureRecognizer,然后决定在特定坐标中做什么:

    在最顶层 viewController 的 viewDidLoad 内部:

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.didRecognizeTapGesture(gesture:)))        
    self.view.addGestureRecognizer(tapGesture)
    

    在同一个控制器中添加一个方法:

    func didRecognizeTapGesture(gesture: UITapGestureRecognizer)
    {
        let point = gesture.location(in: gesture.view)        
        if(gesture.state == UIGestureRecognizerState.ended) {
            let desiredFrame = CGRect(...) //e.g the portion of screen, where you need to make exceptional touch event
            if(desiredFrame.contains(point)) {
                print("... Caught a gesture")
            }
        }
    }
    

    【讨论】:

    • 我知道这个技巧,但问题是当我使用它时,它会取消我的按钮点击效果,我必须模拟一切。有没有办法在另一个 ViewController 中定位记录按钮?
    • 我认为这可以处理你的情况,能够捕捉到按钮的触摸事件:stackoverflow.com/a/3348532/661022
    • 感谢您的努力,但不幸的是,这不是我想要的。这将添加一个 Tapgesturerecognizer,我什至没有。我需要一个视图控制器来忽略我的触摸并将它们传递给它下面的视图控制器。不是tapgesturerecognizer。我仍然需要在我的 CollectionView 上保持交互性
    • stackoverflow.com/questions/34660095/… 我想这说明了一切,可悲的是哈哈。
    • 出色的搜索技巧。这就是为什么我建议自己的解决方案。希望你能找到更好的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 2013-11-09
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    相关资源
    最近更新 更多