【发布时间】:2011-06-11 16:54:46
【问题描述】:
在 UIWebview 上,如何检测触摸?
但不是当用户单击某个 URL 或触摸控件时。
可以处理吗?
【问题讨论】:
在 UIWebview 上,如何检测触摸?
但不是当用户单击某个 URL 或触摸控件时。
可以处理吗?
【问题讨论】:
从 UIResponder 继承的所有东西都可以处理触摸(UIWebView 也是如此)。阅读文档: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIResponder_Class/Reference/Reference.html
你必须使用:
touchesBegan:withEvent:
编辑:为了清楚起见,在此处添加评论-
我相信没有干净的方法可以做到这一点,您可以像 this 这样覆盖 hittest withEvent 方法,或者像这样进行破解:overriding UIView
【讨论】:
您的意思是要覆盖按住链接时弹出的选项吗?我设法让一个与本教程/指南一起工作,但这里发布的那个仍然有点错误,需要你做一些微调: http://www.icab.de/blog/2010/07/11/customize-the-contextual-menu-of-uiwebview/
【讨论】:
使用 UIGestureRecognizerDelegate 方法:
在声明文件(即您的 .h 文件)中添加 UIGestureRecognizerDelegate
第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];
}
【讨论】:
请注意,上面接受的答案只会捕获 tap 手势(touchDown 和 touchUp 之间没有拖动),并且滑动手势将被忽略。
出于我的目的,我需要了解这两者,因此我适当地添加了滑动手势识别器。 (请注意,尽管是位字段,但您不能 OR 一起滑动手势识别器的 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 子类并将其放在 webview 上。在子类中,您可以使用 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
【讨论】: