检测 UIWebView 中的双击

发布于 2024-12-05 02:46:46 字数 1332 浏览 0 评论 0原文

我正在尝试检测 uiwebview 中的双击。以下是代码。当我调试时,调试器进入 initwithcoder 但永远不会进入 Touchsbegan 或选择器函数。为什么会这样?

我将 UIWebView 放入我的 xib 文件中并将身份检查器类设置为 MyWebView

#import "MyWebView.h"


@implementation MyWebView

- (void) initTap {

    UITapGestureRecognizer *singleFingerDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerDoubleTap:)];
    singleFingerDoubleTap.numberOfTouchesRequired = 1;
    singleFingerDoubleTap.numberOfTapsRequired = 2;
    [self addGestureRecognizer:singleFingerDoubleTap];
    [singleFingerDoubleTap release];
}

- (id)initWithCoder:(NSCoder*)coder
{
    if (self = [super initWithCoder:coder]) {
        // Initialization code.
        [self initTap];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
        [self initTap];
    }
    return self;
}


-(void)handleSingleFingerDoubleTap:(id)sender {
    NSLog(@"Doulble click detected !");
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    if (touch.tapCount == 2) {
            //put you zooming action here
        NSLog(@"Doulble click detected !");
    }
}


- (void)dealloc {
    [super dealloc];
}


@end

I am trying to detect double tap in a uiwebview. following is the code. when i debug, debugger comes to initwithcoder but never to touchesbegan nor to selector function. why so ?

I am putting a UIWebView in my xib file and setting identity inspector class to MyWebView

#import "MyWebView.h"


@implementation MyWebView

- (void) initTap {

    UITapGestureRecognizer *singleFingerDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerDoubleTap:)];
    singleFingerDoubleTap.numberOfTouchesRequired = 1;
    singleFingerDoubleTap.numberOfTapsRequired = 2;
    [self addGestureRecognizer:singleFingerDoubleTap];
    [singleFingerDoubleTap release];
}

- (id)initWithCoder:(NSCoder*)coder
{
    if (self = [super initWithCoder:coder]) {
        // Initialization code.
        [self initTap];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
        [self initTap];
    }
    return self;
}


-(void)handleSingleFingerDoubleTap:(id)sender {
    NSLog(@"Doulble click detected !");
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    if (touch.tapCount == 2) {
            //put you zooming action here
        NSLog(@"Doulble click detected !");
    }
}


- (void)dealloc {
    [super dealloc];
}


@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七月上 2024-12-12 02:46:46

您只需将委托添加到识别器

singleFingerDoubleTap.delegate = self;

然后实现以下方法即可返回 YES。

#pragma mark Gesture recognizer delegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
{
    return YES;
}

不要忘记将符合声明添加到您的类文件中。

像这样

@interface ORWebViewController : UIViewController<UIGestureRecognizerDelegate>

You just need to add delegate to the recognizer

singleFingerDoubleTap.delegate = self;

Then implement the following method to return YES.

#pragma mark Gesture recognizer delegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
{
    return YES;
}

Don't forget to add conform declaration to your class file.

Like this

@interface ORWebViewController : UIViewController<UIGestureRecognizerDelegate>
神回复 2024-12-12 02:46:46

如果您想覆盖双击时的默认缩放操作,将为您提供指导。

更新:

请不要考虑上述引用,因为作者已经移动了它。

参考禁用默认缩放效果

In case, you want to override the default zooming action on double tap, this will guide you.

UPDATE:

Please do not consider above mentioned reference as the author has moved it.

You can find the references from Disabling default zoom effect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文