大家好,并提前致谢=)
我对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
添加到几乎所有可能的内容中:
(EDAnnotation *) [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
检索'EDAnnotationView'实例。我甚至试图让这个实例可以拖动并听取didChangeDragState
次调用,以便在MKAnnotationViewDragStateStarting
被触发后立即取消它们,但这也没有按预期工作。基本上我需要的是:
drawRect
EDAnnotation
方法中指定的图片,则会显示气泡。drawRect
EDAnnotation
{{1}}方法中指定的图片,则会收到允许我向地图添加新MKPointAnnotation的回调。提前感谢您的帮助=)
答案 0 :(得分:1)
问题还可能是你的gestureRecognizer与mapView中的gestureRecognizers冲突。这可能发生,因为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中的其他一些gestureRecognizer现在也会触发它们的动作,但不幸的是,不可能对mapView的gestureRecognizers进行委托。
当我向mapView添加longPressureRecognizer时,此解决方法帮助了我。我认为它也可以帮助你解决问题。
答案 1 :(得分:0)
您是否尝试过委托调用注释的方式?
在注释类
中创建委托@protocol AnnotationDelegate <NSObject>
@optional
- (void)shouldContinueAnimate;
@end
在实施文件
中- (void)shouldContinueAnimate {
//add code for animating
}
导入需要的委托< AnnotationDelegate >
在图像视图类中,您可以为图像添加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];
}
}
感谢。