检测UIWebView中的双击

时间:2011-09-19 12:16:27

标签: ios uiwebview

我试图在uiwebview中检测双击。以下是代码。当我调试时,调试器来到initwithcoder但从来没有触及到选择器功能。为什么呢?

我在我的xib文件中放置一个UIWebView,并将身份检查器类设置为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

2 个答案:

答案 0 :(得分:3)

您只需要将代理添加到识别器

singleFingerDoubleTap.delegate = self;

然后执行以下方法返回YES。

#pragma mark Gesture recognizer delegate

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

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

喜欢这个

@interface ORWebViewController : UIViewController<UIGestureRecognizerDelegate>

答案 1 :(得分:-2)

如果您希望在双击时覆盖默认缩放操作,this将指导您。

<强>更新

  

请不要考虑上面提到的参考文献,因为作者已移动它。

您可以找到Disabling default zoom effect.

的参考资料