【问题标题】:iOS MKAnnotationView LongPressGestureRecognizeriOS MKAnnotationView LongPressGestureRecognizer
【发布时间】:2015-03-19 23:06:03
【问题描述】:

大家好,提前感谢=)

我对 MKMapView 和 MKAnnotationView 有疑问。我需要在 MKMapView 上显示带有自定义图像的注释。为此,按照几个教程和其他 stackoverflow 答案,我创建了自己的课程。 EDAnnotation.h:

@interface EDAnnotation : MKAnnotationView
    //@property (nonatomic, strong) UIImageView *imageView;
    - (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;
@end

EDAnnotation.m:

#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。
  • 在 viewForAnnotation 回调中使用 (EDAnnotation *) [mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 检索到的“EDAnnotationView”实例。我什至尝试让这个实例可拖动并监听didChangeDragState 调用,以便在触发MKAnnotationViewDragStateStarting 时立即取消它们,但这也没有按预期工作。

基本上我需要的是:

  • 如果用户按下在EDAnnotationdrawRect 方法中指定的图像,则会显示气泡。
  • 如果用户长按EDAnnotationdrawRect 方法中指定的图像,则会收到一个回调,让我向地图添加一个新的 MKPointAnnotation。

提前感谢您的帮助 =)

【问题讨论】:

    标签: ios objective-c mkmapview mkannotationview


    【解决方案1】:

    问题也可能是您的gestureRecognizer 与mapView 中的gestureRecognizer 冲突。这可能会发生,因为 annotationViews 是 mapView 的子视图。要解决此问题,请使用 UIGestureRecognizerDelegate。当你初始化你的gestureRecognizer时,将delegate属性设置为你实现该协议的类,更准确地说是这两个方法:

    #pragma mark GestureRecognizerDelegate
    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
        return YES;
    }
    
    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
        return YES;
    }
    

    通过在这两种方法中轻松返回 YES,gestureRecognizer 应该做出反应。也许mapView中的其他一些gestureRecognizers现在也会触发它们的动作,但不幸的是,它不可能对mapView的gestureRecognizers进行委托。

    当我将 longPressureRecognizer 添加到 mapView 时,此解决方法对我有帮助。我认为它也可以帮助您解决问题。

    【讨论】:

      【解决方案2】:

      您是否尝试过调用注解的委托方式?

      在注解类中创建委托

      @protocol AnnotationDelegate <NSObject>
      
      @optional
      - (void)shouldContinueAnimate;
      @end
      

      在实现文件中

      - (void)shouldContinueAnimate {
      //add code for animating
      }
      

      在需要的地方导入委托&lt; AnnotationDelegate &gt;

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

      _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];
      

      处理方法:

      - (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];
          }
      }
      

      谢谢。

      【讨论】:

      • 嗨@user2998200。感谢您的回答。我明天试试,然后告诉你。
      猜你喜欢
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多