首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在UILongPressGestureRecognizer中结合使用MKPinAnnotationView和可拖放MKPinAnnotationView

在UILongPressGestureRecognizer中结合使用MKPinAnnotationView和可拖放MKPinAnnotationView
EN

Stack Overflow用户
提问于 2012-01-11 15:27:50
回答 1查看 2.7K关注 0票数 3

我在使用UILongPressGestureRecognizer (可拖式MKPinAnnotationView )时遇到了问题。

我试图制作的行为类似于Maps应用程序。

  1. 引脚可以被拖动。
  2. 当有一个长按压/抽头时,一个别针就掉了。

然而,在MKPinAnnotationView的框架之外,长时间的媒体被认可是有问题的。长的按压手势,放下销,如果针是不可拉的,工作良好。然而,当引脚是可拉的,我不能让长按压手势识别器被识别,这样我就可以放下引脚。

有什么想法吗?

顺便说一下,我试图为长新闻识别器设置代表,以便

代码语言:javascript
复制
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

在这种情况下,长按压手势被识别,引脚被丢弃,但是引脚的拖动不再有效。

MapView的片段(MKMapView的子类)

代码语言:javascript
复制
- (id)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {

    // init the gesture recognizer
        UILongPressGestureRecognizer* lpgr = [[UILongPressGestureRecognizer alloc] 
            initWithTarget:self action:@selector(handleLongPress:)];
        lpgr.minimumPressDuration = 0.5f; //user needs to press for 2 seconds
        lpgr.delegate = self;
        [self addGestureRecognizer:lpgr];
        [lpgr release];

        //add some initial annotation
        Marker *_annotation = [[Marker alloc] initWithCoordinate:_location];
        [_annotation titleWithString:@"some title"];
        [self addAnnotation:_annotation];

    }

    return self;

}

- (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
    {
        return;
    }

    CGPoint touchPoint = [gestureRecognizer locationInView:self];   
    CLLocationCoordinate2D touchMapCoordinate = [self convertPoint:touchPoint toCoordinateFromView:self];

    // add marker to self-map
    // Marker is subclass of MKAnnotation
    Marker *_annotation = [[Marker alloc] initWithCoordinate:_location];
    [_annotation titleWithString:@"some title"];
    [self addAnnotation:_annotation];

}


- (MKAnnotationView *)mapView:(MKMapView *)mView viewForAnnotation:(id<MKAnnotation>) annotation {

    if([annotation isMemberOfClass:[Marker class]] ) {

        // use MKPinAnnotationView for the view
        MKPinAnnotationView *_pin = (MKPinAnnotationView *) [mView dequeueReusableAnnotationViewWithIdentifier:@"spot_pin"];

        if (_pin == nil)
        {
            _pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"spot_pin"] autorelease];
        }
        else
        {
            _pin.annotation = annotation;
        }

        [_pin setDraggable:YES];
        [_pin setSelected:YES animated:YES];
        [_pin setCanShowCallout:YES];

        return _pin;

    } else {

        return nil;

    }

}
EN

回答 1

Stack Overflow用户

发布于 2012-01-12 04:29:41

好了伙计们我解决了。

显然,在子类MKMapView之后,我还添加了一个方法handleLongPress。这种方法显然干扰了MKMapView的MKMapView方法。

只要将我的handleLongPress选择器更改为像handleLongPress2这样的不同名称,它就会像地图应用程序一样工作。

代码语言:javascript
复制
    UILongPressGestureRecognizer* lpgr = [[UILongPressGestureRecognizer alloc] 
        initWithTarget:self action:@selector(handleLongPress2:)];
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8821872

复制
相关文章

相似问题

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