首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >防止iPad Safari上的所有默认手势

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

Stack Overflow用户
提问于 2013-02-05 08:26:58
回答 2查看 2.5K关注 0票数 0

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

EN

回答 2

Stack Overflow用户

发布于 2013-02-05 12:54:24

防止滚动:

代码语言:javascript
运行
复制
document.ontouchmove = function(event){
event.preventDefault();}

防止抽查:

代码语言:javascript
运行
复制
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
票数 3
EN

Stack Overflow用户

发布于 2013-02-05 09:09:42

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

代码语言:javascript
运行
复制
<script type="text/javascript">
touchMove = function(event) {
    event.preventDefault();
}
</script>

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

代码语言:javascript
运行
复制
[[WildcardGestureRecognizerForwarder alloc] initWithController:self andView:self.webview];

在您的项目中,使用以下代码添加WildcharGestureRecognizerForwarder.h:

代码语言:javascript
运行
复制
    #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文件:

代码语言:javascript
运行
复制
#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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14703022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档