使用 UIWebView 进行手势识别

发布于 2024-11-15 23:05:41 字数 915 浏览 5 评论 0原文

我在我正在构建的应用程序中设置了一些手势识别功能。其中一种手势是单指单击,它会隐藏屏幕顶部的工具栏。效果很好,除了一件事:点击链接也会导致工具栏消失。

是否可以检测到不是链接点击的点击?我可以通过查看点击发生的位置来做到这一点,并且仅在 html 链接上没有发生点击时才采取操作吗?这是否可能,指向如何确定链接是否被点击的指针会有所帮助。

根据 Octys 的建议,我确实尝试将 UIWebView 包装在 UIView 中。我正在使用界面生成器,因此我在层次结构中插入了一个视图,并使 UIWebView 成为新 UIView 的“子级”。层次结构现在看起来像这样:

Hierarchy screen shot

我在视图控制器中为视图添加了一个 IBOutlet,并将其链接起来在界面生成器中。

我将手势识别器设置更改为如下所示:

UITapGestureRecognizer* singleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTap:)];
singleTap.numberOfTouchesRequired=1;
singleTap.delegate=self;
[self.UIWebViewContainer addGestureRecognizer:singleTap];
[singleTap release];

此代码位于 viewDidLoad 中。

和以前一样,代码正确地看到单指单击,但点击 UIWebView 中的链接也会导致工具栏消失。我只希望如果未点击链接,工具栏就会消失。

有人有什么建议吗?

谢谢。

克里斯·

谢斯 克里斯

I have set up some gesture recognition in an app that I'm building. One of the gestures is a single finger single tap, which hides the toolbar at the top of the screen. Works great except for one thing: a tap on a link causes the toolbar to go away, too.

Is it possible to detect a tap that was not a tap on a link? Could I do this by seeing where the tap occurred, and only take action if it didn't occur on an html link? Is this is possible, a pointer to how to determine if a link was tapped would be helpful.

Per Octys suggestion, I did attempt to wrap the UIWebView inside a UIView. I'm using interface builder, so I inserted a view in the hierarchy, and made the UIWebView a "child" of the new UIView. The hierarchy looks like this now:

Hierarchy screen shot

I added an IBOutlet for the view in my view controller, and linked it up in interface builder.

I changed the gesture recognizer setup to look like this:

UITapGestureRecognizer* singleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTap:)];
singleTap.numberOfTouchesRequired=1;
singleTap.delegate=self;
[self.UIWebViewContainer addGestureRecognizer:singleTap];
[singleTap release];

This code resides in viewDidLoad.

As before, the code correctly sees a single finger single tap, but a tap on a link in the UIWebView also causes the toolbar to go away. I only want the toolbar to go away if a link was NOT tapped.

Anyone have any suggestions?

Thank you.

Chris

Thanks
Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

ㄟ。诗瑗 2024-11-22 23:05:42

尝试将您的 UIWebView 包装在 UIView 容器中,并在容器视图上设置手势识别器。 UIWebView 未处理的触摸事件将沿着视图层次结构向上传递,并被容器视图拦截,假设它实现了适当的处理程序(并且正是这些处理程序应该实现隐藏代码)工具栏...)。

Try wrapping your UIWebView in a UIView container and set your gesture recognizers on the container view. Touch events that are not handled by the UIWebView will be passed up the view hierarchy and be intercepted by your container view, assuming it implements the appropriate handlers (and it is these handlers that should implement the code for hiding the toolbars...).

空气里的味道 2024-11-22 23:05:42

好的,所以一种“hack-ish”解决方法是使包含 UIWebView 的视图控制器成为您实例化的手势识别器的委托(UIGestureRecognizerDelegate) 我通常不喜欢这样的解决方案,但是

...手势识别器到包装您的 Web 视图的视图,并将其委托设置为 self。

然后在委托方法gestureRecognizer:shouldRecieveTouch方法中,设置状态以指示存在点击事件,

从委托方法返回NO,以便事件沿着响应者链传播到UIWebView。

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{

    _userTapped = YES;
    [self performSelectorOnMainThread:@selector(hideMenuIfNotLink) afterDelay:??.?];
//??.?? amount of time to lapse to see if shouldStartLoadWithRequest gets called.
   return NO;

}

-(void)hideMenuIfNotLink{
    if(_userTapped)
         //hideMenu
}

现在,在您的 UIWebViewDelegate 中 shouldStartLoadWithRequest (当用户实际上单击了链接时会调用它)

if(_userTapped){
    _userTapped = NO;
     //possibly code to reshow or verify keep showing menu
}

OK, so one "hack-ish" workaround is to make the view controller which houses the UIWebView a delegate of the gesture recognizer that you instantiate(UIGestureRecognizerDelegate) I am usually not a fan of solutions like this one, but...

Assign the gesture recognizer to the view that wraps you web view, and set it's delegate to self.

then in the delegate method gestureRecognizer:shouldRecieveTouch method, set the state to indicate the there was a tap event,

Return NO from the delegate method so the event propagates down the responder chain to the UIWebView.

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{

    _userTapped = YES;
    [self performSelectorOnMainThread:@selector(hideMenuIfNotLink) afterDelay:??.?];
//??.?? amount of time to lapse to see if shouldStartLoadWithRequest gets called.
   return NO;

}

-(void)hideMenuIfNotLink{
    if(_userTapped)
         //hideMenu
}

Now, in your UIWebViewDelegate shouldStartLoadWithRequest (which gets called when a user has in fact clicked a link)

if(_userTapped){
    _userTapped = NO;
     //possibly code to reshow or verify keep showing menu
}
时光与爱终年不遇 2024-11-22 23:05:42

您可以使用 UIWebViewDelegate 协议的 -webView:shouldStartLoadWithRequest:navigationType: 方法。

如果navigationTypeUIWebViewNavigationTypeLinkClicked,您可以通过检查[request URL]来获取点击的URL。

You can use the UIWebViewDelegate protocol's -webView:​shouldStartLoadWithRequest:​navigationType: method.

If the navigationType is UIWebViewNavigationTypeLinkClicked, you can get the URL for the click by checking [request URL].

花辞树 2024-11-22 23:05:42

我一直在寻找同样的东西,并发现了这个:有一个 iOS 特定属性,当您将手指放在链接上时,该属性会禁用标注。您可以将其添加到网址的样式 (CSS) 中。

-webkit-touch-callout: none;

I've been looking for the same thing and found this: There is an iOS specific property that disables the callout when you hold your finger on a link. You add this into the styling (CSS) of your urls.

-webkit-touch-callout: none;
心是晴朗的。 2024-11-22 23:05:42

这对我有用。尝试添加以下代码行

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}

This worked for me.try adding the below line of code

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
吃素的狼 2024-11-22 23:05:42

我也遇到了同样的问题。这个解决方案对我有用:

1)确保将协议添加到您的界面:UIGestureRecognizerDelegate
例如:

@interface ViewController : UIViewController _SlideViewProtocol, UIGestureRecognizerDelegate_

2) 添加这行代码

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
}

3)

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(nextSlide)];
        swipeGesture.numberOfTouchesRequired = 1;
        swipeGesture.direction = (UISwipeGestureRecognizerDirectionLeft);
        swipeGesture.cancelsTouchesInView = YES;
        [swipeGesture setDelegate:self];
        [self.view addGestureRecognizer:swipeGesture];

I was having the same problem. This solution worked for me:

1) make sure to add the protocol to your interface: UIGestureRecognizerDelegate
for example:

@interface ViewController : UIViewController _SlideViewProtocol, UIGestureRecognizerDelegate_

2) add this line of code

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
}

3)

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(nextSlide)];
        swipeGesture.numberOfTouchesRequired = 1;
        swipeGesture.direction = (UISwipeGestureRecognizerDirectionLeft);
        swipeGesture.cancelsTouchesInView = YES;
        [swipeGesture setDelegate:self];
        [self.view addGestureRecognizer:swipeGesture];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文