防止iPad Safari上的所有默认手势

时间:2013-02-05 08:26:59

标签: javascript ios safari gestures

如何使用普通的javascript在iPad上禁用Safari上的所有默认手势。 我已尝试对每个事件使用event.preventDefault();,但它无效。 我在Safari中运行应用程序,我想禁用所有默认触摸事件(手势)并使用我自己的覆盖。我也尝试过使用一个受欢迎的库hammer.js,它有一个选项prevent_default但是没有用。

2 个答案:

答案 0 :(得分:2)

阻止滚动:

document.ontouchmove = function(event){
event.preventDefault();}

防止点击:

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>

答案 1 :(得分: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