【发布时间】:2012-03-20 09:47:44
【问题描述】:
我有一个 UIWebView,它作为子视图添加到 UIview 中,我想检测何时触摸此 UIWebView 但 touchesBegan 不起作用。
有什么想法吗?
【问题讨论】:
-
显示相关代码如何?
标签: iphone objective-c ios uiwebview
我有一个 UIWebView,它作为子视图添加到 UIview 中,我想检测何时触摸此 UIWebView 但 touchesBegan 不起作用。
有什么想法吗?
【问题讨论】:
标签: iphone objective-c ios uiwebview
在子类 UIWebView 中调用 super
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan: touches withEvent: event];
}
【讨论】:
子类化和等待 touchesBegan 对您没有帮助,因为它不会被调用。
我对UIWebView 进行了子类化,并且只是在 2 级深的子视图中“吸食”了它的手势识别器(你可以递归地进行,但这对于 iOS6-7 来说已经足够了)。
然后你可以对触摸位置和手势识别器的状态做任何你想做的事情。
for (UIView* view in self.subviews) {
for (UIGestureRecognizer* recognizer in view.gestureRecognizers) {
[recognizer addTarget:self action:@selector(touchEvent:)];
}
for (UIView* sview in view.subviews) {
for (UIGestureRecognizer* recognizer in sview.gestureRecognizers) {
[recognizer addTarget:self action:@selector(touchEvent:)];
}
}
}
【讨论】:
subview.swift
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
self.next?.touchesBegan(touches, with: event)
}
如果您不确定是否应该致电super.touchesBegan(touches, with: event),请参阅文档discussion。
superview.swift
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// handle
}
【讨论】: