问题
I've an UIWebView which is added as a subview in a UIview and I would like to detect when this UIWebView is touched but touchesBegan just don't work.
Any idea ?
回答1:
- Subclass UIWebView in your code and then implement touchesBegan in your class.
- Add the subclassed webview as a subview of your UIView.
Call super in the subclassed UIWebView
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan: touches withEvent: event]; }
回答2:
Subclassing and waiting for touchesBegan won't help you, as it won't get called.
I subclassed UIWebView and just "leeched" onto its gesture recognizers in subviews 2 levels deep (you could go recursively but thats enough for iOS6-7).
Then you can do whatever you want with the touch location and gesture recognizer's state.
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:)];
}
}
}
来源:https://stackoverflow.com/questions/9524011/get-touchesbegan-on-a-subview-uiwebview