将触摸传递给其他清晰的viewcontroller

时间:2016-09-02 20:37:05

标签: ios swift uiviewcontroller touch

我正在寻找一种方法来传递来自另一个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

当点击集合视图外部和passThroughView后面的记录按钮时,它应该执行一条新记录。问题是,使用以下代码我无法实现这一点:

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按钮。

那我在这里做错了什么?

1 个答案:

答案 0 :(得分: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")
        }
    }
}