On UIWebview, how can I detect a touch?
在UIWebview上,如何检测触摸?
But not when user clicks some URL or touching a control.
但不是在用户点击某个URL或触摸控件时。
Is it possible to handle it?
有可能处理它吗?
5 个解决方案
#1
32
Use UIGestureRecognizerDelegate method:
使用UIGestureRecognizerDelegate方法:
Add UIGestureRecognizerDelegate in declaration file (i.e. your .h file)
在声明文件中添加UIGestureRecognizerDelegate(即您的.h文件)
Step 1: Just set the delegate of gestureRecognizer: (in .m file viewDidLoad)
第1步:只需设置gestureRecognizer的委托:(在.m文件viewDidLoad中)
UITapGestureRecognizer *webViewTapped = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
webViewTapped.numberOfTapsRequired = 1;
webViewTapped.delegate = self;
[offScreenWebView addGestureRecognizer:webViewTapped];
[webViewTapped release];
Step 2: Override this function: (in .m file)
第2步:覆盖此功能:(在.m文件中)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Step 3: Now implement the tapAction function:
第3步:现在实现tapAction功能:
- (void)tapAction:(UITapGestureRecognizer *)sender
{
NSLog(@"touched");
// Get the specific point that was touched
CGPoint point = [sender locationInView:self.view];
}
#2
1
The accepted answer is great if you only need to detect taps. If you need to detect all touches, the best way is to create a new UIView subclass and place it over the webview. In the subclass you can detect touches using hitTest:
如果您只需要检测水龙头,那么接受的答案就很棒。如果需要检测所有触摸,最好的方法是创建一个新的UIView子类并将其放在webview上。在子类中,您可以使用hitTest检测触摸:
TouchOverlay.h
TouchOverlay.h
@class TouchOverlay;
@protocol TouchOverlayDelegate <NSObject>
@optional
- (void)touchOverlayTouched:(TV4TouchOverlay *)touchOverlay;
@end
@interface TouchOverlay : UIView
@property (nonatomic, unsafe_unretained) id <TouchOverlayDelegate> delegate;
@end
Touchoverlay.m
Touchoverlay.m
@implementation TouchOverlay
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
return self;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *hitView = [super hitTest:point withEvent:event];
if (hitView == self) {
if (self.delegate && [self.delegate respondsToSelector:@selector(touchOverlayTouched:)]) {
[self.delegate touchOverlayTouched:self];
}
return nil; // Tell the OS to keep looking for a responder
}
return hitView;
}
@end
#3
0
Everything that inherits from UIResponder can handle touches (so does UIWebView). Read the doc: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIResponder_Class/Reference/Reference.html
从UIResponder继承的所有内容都可以处理触摸(UIWebView也是如此)。阅读文档:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIResponder_Class/Reference/Reference.html
You'll have to use:
你必须使用:
touchesBegan:withEvent:
Edit: Adding the comment here for clarity-
编辑:为了清楚起见,在此处添加评论
I believe then there's no clean way of doing it, you can either override the hittest withEvent method like this or do a hack like this: overriding UIView
我相信那时没有干净的方法,你可以像这样覆盖hittest withEvent方法,或者像这样做一个hack:覆盖UIView
#4
0
Note that the accepted answer above will only capture tap gestures (touchDown and touchUp without a drag in between), and that swipe gestures will be ignored.
请注意,上面接受的答案将仅捕获轻击手势(touchDown和touchUp之间没有拖动),并且将忽略滑动手势。
For my purposes I needed to be informed of both, and so I added swipe gesture recognizers appropriately. (Note that despite being a bit field, you can't OR
together swipe gesture recognizers' direction
property, so 4 gesture recognizers are required to detect any swipe).
为了我的目的,我需要被告知两者,所以我适当地添加了滑动手势识别器。 (请注意,尽管是一个位字段,但您无法一起滑动手势识别器的方向属性,因此需要4个手势识别器来检测任何滑动)。
// Note that despite being a bit field, you can't `OR` together swipe gesture
// recognizers' `direction` property, so 4 gesture recognizers are required
// to detect any swipe
for (NSNumber * swipeDirection in @[@(UISwipeGestureRecognizerDirectionUp), @(UISwipeGestureRecognizerDirectionDown), @(UISwipeGestureRecognizerDirectionLeft), @(UISwipeGestureRecognizerDirectionRight)]) {
UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(timerReset:)];
swipe.direction = [swipeDirection integerValue];
swipe.delegate = self;
[rootWebView addGestureRecognizer:swipe];
}
#5
-1
Do you mean you want to override the options that popup when they hold down on a link? I managed to get one to work with this tutorial/guide but the one posted here is still slightly buggy and needs you to do some fine tuning: http://www.icab.de/blog/2010/07/11/customize-the-contextual-menu-of-uiwebview/
你的意思是你想要覆盖他们按住链接时弹出的选项吗?我设法让一个人使用本教程/指南,但是这里发布的那个仍然有点错误,需要你做一些微调:http://www.icab.de/blog/2010/07/11/customize-在上下文菜单-的-的UIWebView /
#1
32
Use UIGestureRecognizerDelegate method:
使用UIGestureRecognizerDelegate方法:
Add UIGestureRecognizerDelegate in declaration file (i.e. your .h file)
在声明文件中添加UIGestureRecognizerDelegate(即您的.h文件)
Step 1: Just set the delegate of gestureRecognizer: (in .m file viewDidLoad)
第1步:只需设置gestureRecognizer的委托:(在.m文件viewDidLoad中)
UITapGestureRecognizer *webViewTapped = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
webViewTapped.numberOfTapsRequired = 1;
webViewTapped.delegate = self;
[offScreenWebView addGestureRecognizer:webViewTapped];
[webViewTapped release];
Step 2: Override this function: (in .m file)
第2步:覆盖此功能:(在.m文件中)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Step 3: Now implement the tapAction function:
第3步:现在实现tapAction功能:
- (void)tapAction:(UITapGestureRecognizer *)sender
{
NSLog(@"touched");
// Get the specific point that was touched
CGPoint point = [sender locationInView:self.view];
}
#2
1
The accepted answer is great if you only need to detect taps. If you need to detect all touches, the best way is to create a new UIView subclass and place it over the webview. In the subclass you can detect touches using hitTest:
如果您只需要检测水龙头,那么接受的答案就很棒。如果需要检测所有触摸,最好的方法是创建一个新的UIView子类并将其放在webview上。在子类中,您可以使用hitTest检测触摸:
TouchOverlay.h
TouchOverlay.h
@class TouchOverlay;
@protocol TouchOverlayDelegate <NSObject>
@optional
- (void)touchOverlayTouched:(TV4TouchOverlay *)touchOverlay;
@end
@interface TouchOverlay : UIView
@property (nonatomic, unsafe_unretained) id <TouchOverlayDelegate> delegate;
@end
Touchoverlay.m
Touchoverlay.m
@implementation TouchOverlay
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
return self;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *hitView = [super hitTest:point withEvent:event];
if (hitView == self) {
if (self.delegate && [self.delegate respondsToSelector:@selector(touchOverlayTouched:)]) {
[self.delegate touchOverlayTouched:self];
}
return nil; // Tell the OS to keep looking for a responder
}
return hitView;
}
@end
#3
0
Everything that inherits from UIResponder can handle touches (so does UIWebView). Read the doc: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIResponder_Class/Reference/Reference.html
从UIResponder继承的所有内容都可以处理触摸(UIWebView也是如此)。阅读文档:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIResponder_Class/Reference/Reference.html
You'll have to use:
你必须使用:
touchesBegan:withEvent:
Edit: Adding the comment here for clarity-
编辑:为了清楚起见,在此处添加评论
I believe then there's no clean way of doing it, you can either override the hittest withEvent method like this or do a hack like this: overriding UIView
我相信那时没有干净的方法,你可以像这样覆盖hittest withEvent方法,或者像这样做一个hack:覆盖UIView
#4
0
Note that the accepted answer above will only capture tap gestures (touchDown and touchUp without a drag in between), and that swipe gestures will be ignored.
请注意,上面接受的答案将仅捕获轻击手势(touchDown和touchUp之间没有拖动),并且将忽略滑动手势。
For my purposes I needed to be informed of both, and so I added swipe gesture recognizers appropriately. (Note that despite being a bit field, you can't OR
together swipe gesture recognizers' direction
property, so 4 gesture recognizers are required to detect any swipe).
为了我的目的,我需要被告知两者,所以我适当地添加了滑动手势识别器。 (请注意,尽管是一个位字段,但您无法一起滑动手势识别器的方向属性,因此需要4个手势识别器来检测任何滑动)。
// Note that despite being a bit field, you can't `OR` together swipe gesture
// recognizers' `direction` property, so 4 gesture recognizers are required
// to detect any swipe
for (NSNumber * swipeDirection in @[@(UISwipeGestureRecognizerDirectionUp), @(UISwipeGestureRecognizerDirectionDown), @(UISwipeGestureRecognizerDirectionLeft), @(UISwipeGestureRecognizerDirectionRight)]) {
UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(timerReset:)];
swipe.direction = [swipeDirection integerValue];
swipe.delegate = self;
[rootWebView addGestureRecognizer:swipe];
}
#5
-1
Do you mean you want to override the options that popup when they hold down on a link? I managed to get one to work with this tutorial/guide but the one posted here is still slightly buggy and needs you to do some fine tuning: http://www.icab.de/blog/2010/07/11/customize-the-contextual-menu-of-uiwebview/
你的意思是你想要覆盖他们按住链接时弹出的选项吗?我设法让一个人使用本教程/指南,但是这里发布的那个仍然有点错误,需要你做一些微调:http://www.icab.de/blog/2010/07/11/customize-在上下文菜单-的-的UIWebView /