How to respond only to a one-touch gesture, allowing objects to respond to double clicks?

I am showing a document in a UIWebView. I want to place an access point above the document to trigger an action when it is being listened, but I also want to support the default UIWebView behavior to automatically scale the document when it is double-clicked. I cannot figure out how to respond to single clicks, allowing the UIWebView to respond to double clicks.

First, I set the access point as a transparent UIButton with the action, but double-clicking on the hotspot causes the hotspot action to be called twice. So I deleted the action with the button and added a one-touch gesture, and instead:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)];
singleTap.numberOfTapsRequired = 1;
singleTap.delegate = self;
[self.hotspot addGestureRecognizer:singleTap];
[singleTap release];

This works the same as the action of a regular button. But then I created a double-touch gesture and set it up to block a one-touch gesture using requireGestureRecognizerToFail:

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zoomWebView:)];
doubleTap.numberOfTapsRequired = 2;
doubleTap.delegate = self;
[self.hotspot addGestureRecognizer:doubleTap];
[doubleTap release];

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)];
singleTap.numberOfTapsRequired = 1;
[singleTap requireGestureRecognizerToFail:doubleTap];
singleTap.delegate = self;
[self.hotspot addGestureRecognizer:singleTap];
[singleTap release];

- (void)zoomWebView:(UITapGestureRecognizer *)gesture {
    NSLog(@"double tap");
}

With this setting, a single click on a hot spot calls singleTapAction and double click on a hot spot calls zoomWebView (native method). This is good because singleTapAction is no longer called twice, but bad because UIWebView no longer responds to double-clicking.

UIWebView, UITapGestureRecognizer, touchhesBegan touchesEnded UIWebView. , , , UIWebView . , iOS , UIView, UIKit.

, , ? , . , .

, UIWebView - "" - . " ", - "grandparent", .

+5
2

, :

1) UIWebView. , . . , "" UIWebView, -. , , Interface Builder, " ". , , , .

2) gestureRecognizer: shouldRecognizeSimultaneousWithGestureRecognizer: ( ) YES. , HTML, UIWebView, . .

:

  • -: ; , , .

  • -: - ( ); , , ; - , (, - , )

, :

- (void)viewDidLoad {
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapWebView:)];
    doubleTap.numberOfTapsRequired = 2;
    doubleTap.delegate = self;
    [self.webViewParent addGestureRecognizer:doubleTap];
    [doubleTap release];

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapWebView:)];
    singleTap.numberOfTapsRequired = 1;
    [singleTap requireGestureRecognizerToFail:doubleTap];
    singleTap.delegate = self;
    [self.webViewParent addGestureRecognizer:singleTap];
    [singleTap release];
}

- (void)doubleTapWebView:(UITapGestureRecognizer *)gesture {
    NSLog(@"double-tap");
    // nothing to do here
}

- (void)singleTapWebView:(UITapGestureRecognizer *)gesture {
    NSLog(@"single-tap");
    CGPoint touchLocation = [gesture locationInView:self.webViewParent];
    float x = touchLocation.x;
    float y = touchLocation.y;
    CGRect frame = self.webViewParent.frame;
    if (y < frame.size.height * .33) {
        NSLog(@"top");
    } else if (y > frame.size.height * .67) {
        NSLog(@"bottom");
    } else if (x < frame.size.width * .33) {
        NSLog(@"left");
    } else if (x > frame.size.width * .67) {
        NSLog(@"right");
    } else {
        NSLog(@"center");
    }
}

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

, .

, , .

, , .

.

, .

-2

All Articles