首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS MKAnnotationView LongPressGestureRecognizer

iOS MKAnnotationView LongPressGestureRecognizer
EN

Stack Overflow用户
提问于 2015-03-19 23:06:03
回答 2查看 261关注 0票数 1

大家好,谢谢。)

我对MKMapView和MKAnnotationView有一个疑问。我需要在MKMapView上显示带有自定义图像的注释。为了做到这一点,并遵循几个教程和其他堆栈溢出答案,我创建了自己的类。EDAnnotation.h:

代码语言:javascript
复制
@interface EDAnnotation : MKAnnotationView
    //@property (nonatomic, strong) UIImageView *imageView;
    - (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;
@end

艾德诺:

代码语言:javascript
复制
#import "EDAnnotation.h"

@implementation EDAnnotation
    - (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier{

        self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];

        if (self != nil) {

            CGRect frame = self.frame;
            frame.size = CGSizeMake(15.0, 15.0);
            self.frame = frame;
            self.backgroundColor = [UIColor clearColor];
            self.centerOffset = CGPointMake(-5, -5);
        }
        return self;
    }

    -(void) drawRect:(CGRect)rect {

        NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
        [style setAlignment:NSTextAlignmentCenter];
        [[UIImage imageNamed:@"train4_transparent.png"] drawInRect:CGRectMake(0, 0, 15, 15)];
    }
@end

我在地图上添加了几个注释,一切都如预期的那样工作。每当我点击图片时,都会显示出一个显示一些信息的气泡。问题是,我需要能够在其中一个注释上检测到长的按压手势(除了显示气泡的点击手势之外)。为了实现这一点,我尝试将UILongGestureRecognizer添加到几乎所有可能的内容中:

  • UIImageView在上面的类中进行了注释。
  • 在EDAnnotationView回调中使用(EDAnnotation *) [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];检索“viewForAnnotation”实例。我甚至尝试让这个实例具有可拖动性,并侦听didChangeDragState调用,以便在触发MKAnnotationViewDragStateStarting时立即取消这些调用,但这也不像预期的那样有效。

基本上我需要的是:

  • 如果用户按下在drawRect方法EDAnnotation中指定的图像,则气泡显示。
  • 如果用户长期按下在drawRect方法EDAnnotation中指定的图像,则接收一个回调,该回调允许我向映射中添加一个新的MKPointAnnotation。

谢谢你的帮助

EN

回答 2

Stack Overflow用户

发布于 2015-03-28 23:43:05

问题还可能是您的gestureRecognizer与mapView中的gestureRecognizers冲突。这可能会发生,因为annotationViews是mapView.To的子视图,解决了这个问题,使用UIGestureRecognizerDelegate。初始化gestureRecognizer时,将委托属性设置为实现该协议的类,更确切地说,这两种方法:

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

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    return YES;
}

在这两种方法中很容易地返回YES,gestureRecognizer应该做出反应。也许来自mapView的其他一些mapView现在也会启动它们的操作,但不幸的是,无法执行mapView的gestureRecognizers的委托。

当我在mapView中添加一个mapView时,这个解决方案帮助了我。我觉得这也能帮你解决问题。

票数 1
EN

Stack Overflow用户

发布于 2015-03-19 23:51:47

您尝试过调用注释的委托方式吗?

在注释类中创建委托

代码语言:javascript
复制
@protocol AnnotationDelegate <NSObject>

@optional
- (void)shouldContinueAnimate;
@end

在实现文件中

代码语言:javascript
复制
- (void)shouldContinueAnimate {
//add code for animating
}

在任何需要的地方导入委托< AnnotationDelegate >

在图像视图类中,可以为图像添加LongPressGestureRecognizer和TapGestureRecognizer。

代码语言:javascript
复制
_longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self
                                                          action:@selector(handleLongPressGestureRecognizer:)];
        _tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                    action:@selector(handleTapGestureRecognizer:)];

        [self.imageView addGestureRecognizer:self.longPressGestureRecognizer];
        [self.imageView addGestureRecognizer:self.tapGestureRecognizer];

处理方法:

代码语言:javascript
复制
- (void)handleTapGestureRecognizer:(UIGestureRecognizer *)sender {


        if ([self.delegate respondsToSelector:@selector(shouldContinueAnimate)]) {
            [self.delegate shouldContinueAnimate];
        }
    }


- (void)handleLongPressGestureRecognizer:(UIGestureRecognizer *)sender {

    if ([self.delegate respondsToSelector:@selector(shouldContinueAnimate)]) {
        [self.delegate shouldContinueAnimate];
    }
}

谢谢。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29156586

复制
相关文章

相似问题

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