如何在UIWebView上检测触摸
使用UIGestureRecognizerDelegate方法:
添加UIGestureRecognizerDelegate在声明文件(即你的.h文件中)
第1步:只需设置gestureRecognizer的代表:(在.m文件viewDidLoad中)
UITapGestureRecognizer *webViewTapped = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
webViewTapped.numberOfTapsRequired = 1;
webViewTapped.delegate = self;
[offScreenWebView addGestureRecognizer:webViewTapped];
[webViewTapped release];
第2步:覆盖此函数:(以.m文件)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
3步:现在实现tapAction功能:
- (void)tapAction:(UITapGestureRecognizer *)sender
{
NSLog(@"touched");
// Get the specific point that was touched
CGPoint point = [sender locationInView:self.view];
}
从UIResponder继承的所有东西都可以处理触摸(UIWebView也是如此)。阅读文档: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIResponder_Class/Reference/Reference.html
你将不得不使用:
touchesBegan:withEvent:
编辑:添加评论这里clarity-
我相信再有就是做的不干净的方式,你可以用this之类的事件方法覆盖命中测试或者做一个像这样的破解:overriding UIView
你的意思是你想重写当他们按住链接时弹出的选项?我设法得到一个与本教程/指导工作,但一张贴在这里仍有小幅越野车,需要你做一些微调: http://www.icab.de/blog/2010/07/11/customize-the-contextual-menu-of-uiwebview/
注意,接受的答案上面只捕获自来水手势(着陆和润色,两者之间无需拖动) ,并且滑动手势将被忽略。
为了我的目的,我需要被告知两者,因此我适当地添加了滑动手势识别器。 (请注意,尽管是位字段,但您不能共同滑动手势识别器的属性direction
属性,因此需要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];
}
如果您只需要检测水龙头,接受的答案是很好的。如果您需要检测所有触摸,最好的方法是创建一个新的UIView子类并将其放置在Web视图上。在子类中,你可以使用则hitTest检测触摸:
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
@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
我意识到这一点。但是当我分类uiwebview并实现了touchesBegan和touchesEnded时,它们并没有被调用..... – Satyam 2011-01-19 14:41:00
在这种情况下,你可以在webview上创建一个透明的UIView并使用它的touches方法。 – Ankit 2011-01-20 05:42:26