简体   繁体   中英

Trouble using UILongPressGestureRecognizer in custom MKAnnotationView

Having trouble with a UILongPressGestureRecognizer in a custom subclass of MKAnnotationView. The callback is fired only intermittently. If I make the minimumPressDuration shorter, it fires more frequently. With a minimumPressDuration value of 0.1, it fires every time. With a value of 0.4, it never fires at all, no matter how long I leave my finger on it. At 0.2 it is hit or miss.

If I use a Tap gesture (as below), it works fine. I'm using LongPress gestures on other views and they work fine. It's just on the MKAnnotationView that I have this problem, so I'm wondering if some of the other internal event callbacks on AnnotationViews are interfering (callout, etc).

I see this problem on iOS4 (sim and phone) and 3.2 (sim, don't have a device).

Here is how I'm creating the gesture recognizer:

#define USE_LONG_PRESS 1
#define USE_TAP 0
#if USE_LONG_PRESS
    UILongPressGestureRecognizer *longPressGR = 
    [[UILongPressGestureRecognizer alloc] initWithTarget:self 
                                                action:@selector(handleLongPress:)];
    longPressGR.minimumPressDuration = 0.2;
    [self addGestureRecognizer:longPressGR];
    [longPressGR release];
#endif
#if USE_TAP
    UITapGestureRecognizer *tapGR = 
    [[UITapGestureRecognizer alloc] initWithTarget:self 
                                                action:@selector(handleTap:)];
    [self addGestureRecognizer:tapGR];
    [tapGR release];
#endif

And the callback methods are defined in this class as follows:

- (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer {
    NSLog(@"LONG PRESS");
}

- (void)handleTap:(UIGestureRecognizer *)gestureRecognizer {
    NSLog(@"TAP");
}

Any iPhone gurus have any idea why this might be happening?

As far as I know the markers in 3.2 and iOS 4 already have a long press gesture attached to them to handle marker dragging. Could it be that that's interfering with your long press gesture recognizer? Maybe that's why a shorter duration works; it catches the gesture before the built in long press recognizer can.

  • this is just a guess *

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM