我有一个UIWebView,它已作为子视图添加到UIview中,我想检测何时触摸了该UIWebView,但是touchesBegan不能正常工作。
任何想法?
在子类UIWebView中调用super
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan: touches withEvent: event];
}
子类化并等待触摸,Began不会帮助您,因为它不会被调用。我将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
}