【发布时间】: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时立即取消它们,但这也没有按预期工作。
基本上我需要的是:
- 如果用户按下在
EDAnnotation的drawRect方法中指定的图像,则会显示气泡。 - 如果用户长按
EDAnnotation的drawRect方法中指定的图像,则会收到一个回调,让我向地图添加一个新的 MKPointAnnotation。
提前感谢您的帮助 =)
【问题讨论】:
标签: ios objective-c mkmapview mkannotationview