将触摸传递给其他 viewcontroller 是明确的
Pass touch to other viewcontroller that is clear
我正在寻找一种方法来传递来自另一个 ViewController 之上的透明 ViewController 的触摸。我已经遇到以下 post:
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 语句中得到一个打印,但它不会 return 我触摸另一个 ViewControllers 按钮。
那么我做错了什么?
我更愿意使用 TapGestureRecognizer,然后决定在特定坐标下做什么:
在你最顶层的 viewDidLoad 里面 viewController:
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 之上的透明 ViewController 的触摸。我已经遇到以下 post:
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 语句中得到一个打印,但它不会 return 我触摸另一个 ViewControllers 按钮。
那么我做错了什么?
我更愿意使用 TapGestureRecognizer,然后决定在特定坐标下做什么:
在你最顶层的 viewDidLoad 里面 viewController:
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")
}
}
}