0

如何使用纯 JavaScript 在 iPad 上的 Safari 上禁用所有默认手势。我已经尝试使用event.preventDefault();每个事件,但它不起作用。我在 Safari 中运行应用程序,我想禁用所有默认触摸事件(手势)并用我自己的覆盖。我也尝试过使用一个流行的库hammer.js,它有一个选项 prevent_default 但这不起作用。

4

2 回答 2

3

防止滚动:

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-05T12:54:24.290 回答
0

我认为您的问题不够清楚(请更改它)。你的意思是你有一个带有 UIWebView 的 iOS 应用程序,并且你想禁用该 UIWebView 上的所有鼠标交互吗?如果是这样,那么您确实必须使用以下内容禁用滚动:

<script type="text/javascript">
touchMove = function(event) {
    event.preventDefault();
}
</script>

UIWebViews 不会将触摸和手势传递给下面的视图。如果您想在本机代码中处理触摸,只需将通配符 GestureRecognizer 添加到 UIWebView。您可以在下面看到一个可以使用的辅助类。您可以通过调用在 UIWebView 上使用它:

[[WildcardGestureRecognizerForwarder alloc] initWithController:self andView:self.webview];

在您的项目中,使用以下代码添加 WildcardGestureRecognizerForwarder.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

还使用以下代码添加文件 WildcardGestureRecognizerForwarder.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
于 2013-02-05T09:09:42.750 回答