如何使用纯javascript禁用iPad上Safari上的所有默认手势。我试过在每个事件中都使用event.preventDefault();
,但是它不起作用。我在Safari中运行这个应用程序,我希望禁用所有默认的触摸事件(手势),并使用自己的操作覆盖。我也尝试过使用一个流行的库hammer.js,它有一个选项prevent_default,但这不起作用。
发布于 2013-02-05 12:54:24
防止滚动:
document.ontouchmove = function(event){
event.preventDefault();}
防止抽查:
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
发布于 2013-02-05 09:09:42
我认为你的问题不够清楚(请改变它)。您的意思是您有一个带有iOS的UIWebView应用程序,并且希望禁用该UIWebView上的所有鼠标交互?如果是这样的话,那么实际上您必须禁用滚动,如下所示:
<script type="text/javascript">
touchMove = function(event) {
event.preventDefault();
}
</script>
UIWebViews不会将触摸和客人传递到下面的视图。如果您想处理本机代码中的触摸,那么只需将通配符GestureRecognizer添加到UIWebView。在下面您可以看到一个可以使用的助手类。您可以在UIWebView上使用它,方法是:
[[WildcardGestureRecognizerForwarder alloc] initWithController:self andView:self.webview];
在您的项目中,使用以下代码添加WildcharGestureRecognizerForwarder.h:
#import <Foundation/Foundation.h>
typedef void (^TouchesEventBlock)(NSSet * touches, UIEvent * event);
@interface WildcardGestureRecognizerForwarder : UIGestureRecognizer
@property(nonatomic,assign) UIView *forwardFrom;
@property(nonatomic,assign) UIViewController *forwardTo;
-(void) initWithController:(UIViewController*)theViewController andView:(UIView*)theView;
@end
还可以使用折叠代码添加WildcarGestureRecognizerForwarder.m文件:
#import "WildcardGestureRecognizerForwarder.h"
@implementation WildcardGestureRecognizerForwarder
-(id) init {
if (self = [super init])
{
self.cancelsTouchesInView = NO;
}
return self;
}
-(void) initWithController:(id)theViewController andView:(UIView*)theView {
if ([self init]) {
self.forwardFrom = theView;
self.forwardTo = theViewController;
[theView addGestureRecognizer:self];
self.delegate = theViewController;
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(self.forwardTo)
[self.forwardTo touchesBegan:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
if(self.forwardTo)
[self.forwardTo touchesCancelled:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(self.forwardTo)
[self.forwardTo touchesEnded:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(self.forwardTo)
[self.forwardTo touchesMoved:touches withEvent:event];
}
- (void)reset
{
}
- (void)ignoreTouch:(UITouch *)touch forEvent:(UIEvent *)event
{
}
- (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)preventingGestureRecognizer
{
return NO;
}
- (BOOL)canPreventGestureRecognizer:(UIGestureRecognizer *)preventedGestureRecognizer
{
return NO;
}
@end
https://stackoverflow.com/questions/14703022
复制相似问题