如何向 UIWebView 子类添加手势识别器?
如果我将手势识别器添加到名为 webView
的子类 UIWebView
实例,例如:
UILongPressGestureRecognizer *_longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(webViewGestureRecognized:)];
_longPressRecognizer.allowableMovement = 20;
_longPressRecognizer.minimumPressDuration = 1.0f;
[webView addGestureRecognizer:_longPressRecognizer];
[_longPressRecognizer release], _longPressRecognizer = nil;
当我执行长操作时,不会调用 -webViewGestureRecognized:
选择器按。
我已经重写了委托方法 -gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
但长按选择器仍然没有被调用。
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
我可以做些什么来在网络视图上启用我自己的手势识别器吗?
If I add a gesture recognizer to a subclassed UIWebView
instance called webView
, e.g.:
UILongPressGestureRecognizer *_longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(webViewGestureRecognized:)];
_longPressRecognizer.allowableMovement = 20;
_longPressRecognizer.minimumPressDuration = 1.0f;
[webView addGestureRecognizer:_longPressRecognizer];
[_longPressRecognizer release], _longPressRecognizer = nil;
The -webViewGestureRecognized:
selector is not called when I perform a long press.
I have overridden the delegate method -gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
but the long-press selector is still not called.
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
Is there anything I can do to enable my own gesture recognizer on the web view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
据我所知 UIWebView 不应该像 Apple 文档中提到的那样进行子类化:
http://developer.apple.com/iphone/library/documentation/uikit/reference/UIWebView_Class/Reference/Reference.html
as far as I know UIWebView shouldnt be subclassed as mentioned in Apple docs:
http://developer.apple.com/iphone/library/documentation/uikit/reference/UIWebView_Class/Reference/Reference.html
只是想如果有人回到这里我会添加答案。
您尚未分配委托,因此不会调用
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
。这样做,效果很好。
Just thought I would add the answer if anyone comes back to here.
You have not assigned the delegate, therefore
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
is not called.Do this and it works well.
您应该在 UIWebView 中使用 javascript 来检测手势。 (如果需要,您可以将其传达回 Objective-C。)这些是 Apple 关于在 Javascript 中检测手势和触摸的文档。我还发现了这篇文章 很有帮助,尽管他使用 javascript 库来处理事件绑定等。
这是一个在独立的 UIWebView 上捏合缩放的工作示例。
请注意,主体元素正在侦听该事件。在一个短页面上,如果您在下面巨大的未渲染的空白区域中执行该事件,则似乎无法捕获它。 (如果有人了解更多,请留言。)
You should use javascript in the UIWebView to detect the gestures. (You can then communicate that back to the Objective-C if you need to.) These are Apple's docs on detecting gestures and touches in Javascript. I also found this article helpful, although he uses a javascript library to deal with event binding, etc.
Here's a working example of pinching to zoom on a UIWebView that stands alone.
Note that it is the body element is listening for the event. On a short page it appears not to catch it if you do the event in the vast un-rendered whitespace below. (If anyone knows more about it, please leave a comment.)