UIGestureRecognizer 是否适用于 UIWebView? [英] Does UIGestureRecognizer work on a UIWebView?

查看:32
本文介绍了UIGestureRecognizer 是否适用于 UIWebView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让 UIGestureRecognizer 与 UIWebview 一起工作,UIWebview 是 UIScrollView 的子视图.这听起来很奇怪,但是当我将 numberOfTouchesRequired 设置为 2 时,选择器会触发,但是当 numberOfTouchesRequired 设置为 1 时,选择器不会触发.

I am attempting to get a UIGestureRecognizer working with a UIWebview which is a subview of a UIScrollView. This sounds odd but when I have the numberOfTouchesRequired set to 2 the selector fires, but when numberOfTouchesRequired is set to one the selector doesn't fire.

这是我的代码:

UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(select1:)];
    tap1.numberOfTouchesRequired = 2;
    tap1.numberOfTapsRequired = 1;
    tap1.delegate = self;
    [self.ans1WebView addGestureRecognizer:tap1];
    [tap1 release];

- (void) select1:(UILongPressGestureRecognizer *)sender {
    //Do Stuff
}

我已经通过使用 UIGestureRecognizer 的 Apple 示例并在他们的笔尖中插入一个 webview 来确认这一点.他们的点击代码在任何地方都有效,但在 webview 区域内.

I've confirmed this by using the Apple sample for UIGestureRecognizer and inserting a webview in their nib. Their tap code works everywhere but inside the area of the webview.

推荐答案

就我所见,UIWebView 不能很好地与其他人一起玩.对于手势识别器,您可以尝试从以下位置返回 YES:

From what I have seen, UIWebView does not play well with others. For gesture recognizers, you could try returning YES from:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

我必须向后兼容 3.0,所以我最终在 UIWebView 中使用 Javascript 完成所有手势处理.不是一个好的解决方案,但它满足了我的需求.我能够捕捉到一个元素的长按,以及点击、滑动、捏合和旋转.当然,我只使用了本地内容.

I had to be backwards compatible with 3.0 so I ended up doing all the gesture handling with Javascript inside the UIWebView. Not a good solution, but it worked for my needs. I was able to capture a long press on an element, as well as tap, swipe, pinch and rotate. Of course, I was using only local content.

您可以在 PhoneGap 中查看向 UIWebView 的委托发送消息的示例.简而言之,您使用 document.location = "myscheme:mycommand?myarguments" 然后从这个委托回调中解析出命令和参数:

You can look in PhoneGap for an example of sending messages to the delegate of the UIWebView. In a nutshell, you use document.location = "myscheme:mycommand?myarguments" then parse out the command and arguments from this delegate callback:

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
  if ( [[[inRequest URL] absoluteString] hasPrefix:@"myscheme:"] ) {
    //.. parse arguments
    return NO;
  }
}

您可以在 PhoneGap 代码中看到他们设置了一个队列和计时器来发送消息.根据您的使用情况,您可能需要做同样的事情.关于此主题还有其他关于 SO 的问题.

You can see in the PhoneGap code that they set up a queue and timer to send the messages. Depending on your usage, you may need to do the same thing. There are other questions on SO on this topic.

这里是 事件处理 文档.就我而言,我从 <body onload="myloader();">

Here is the Event Handling documentation. In my case I added event listeners to document from <body onload="myloader();">

function myloader() {
    document.addEventListener( 'touchcancel' , touch_cancel , false );
    document.addEventListener( 'touchstart' , touch_start , false );
    document.addEventListener( 'touchmove' , touch_move , false );
    document.addEventListener( 'touchend' , touch_end , false );
};

实际的事件处理很大程度上取决于您的需求.每个事件处理程序都会收到一个带有 touches 属性的 TouchEvent,其中每个项目都是一个 触摸.您可以在 touchstart 处理程序中记录开始时间和位置.如果触摸移动到很远或经过错误的时间,则不是长时间触摸.

The actual event handling depends a lot on your needs. Each event handler will receive a TouchEvent with a touches property where each item is a Touch. You can record the start time and location in your touchstart handler. If the touches move to far or the wrong amount of time passes it is not a long touch.

WebKit 可能会尝试处理长按以开始选择和复制.在您的事件处理程序中,您可以使用 event.preventDefault(); 来停止默认行为.我还发现 -webkit-user-select:none css 属性对某些事情很方便.

WebKit may try to handle a long touch to start a selection and copy. In your event handler you can use event.preventDefault(); to stop the default behavior. I also found the -webkit-user-select:none css property handy for some things.

var touch = {};
function touch_start( event /*TouchEvent*/ {
  event.preventDefault();
  touch = {};
  touch.startX = event.touches.item(0).pageX;
  touch.startY = event.touches.item(0).pageY;
  touch.startT = ( new Date() ).getTime();
}

这只是我使用 javascript 的第二个项目.你可以在别处找到更好的例子.

This is only the second project I have used javascript with. You can find better examples elsewhere.

如您所见,这不是您问题的快速答案.我对我得到的结果很满意.我使用的 html 除了一两个链接之外没有任何交互元素.

As you can see this is no quick answer to your problem. I am pretty happy with the results I got. The html I was working with had no interactive elements beyond a link or two.

这篇关于UIGestureRecognizer 是否适用于 UIWebView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆