大家好,谢谢。)
我对MKMapView和MKAnnotationView有一个疑问。我需要在MKMapView上显示带有自定义图像的注释。为了做到这一点,并遵循几个教程和其他堆栈溢出答案,我创建了自己的类。EDAnnotation.h:
@interface EDAnnotation : MKAnnotationView
//@property (nonatomic, strong) UIImageView *imageView;
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;
@end艾德诺:
#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];检索“viewForAnnotation”实例。我甚至尝试让这个实例具有可拖动性,并侦听didChangeDragState调用,以便在触发MKAnnotationViewDragStateStarting时立即取消这些调用,但这也不像预期的那样有效。基本上我需要的是:
drawRect方法EDAnnotation中指定的图像,则气泡显示。drawRect方法EDAnnotation中指定的图像,则接收一个回调,该回调允许我向映射中添加一个新的MKPointAnnotation。谢谢你的帮助
发布于 2015-03-28 23:43:05
问题还可能是您的gestureRecognizer与mapView中的gestureRecognizers冲突。这可能会发生,因为annotationViews是mapView.To的子视图,解决了这个问题,使用UIGestureRecognizerDelegate。初始化gestureRecognizer时,将委托属性设置为实现该协议的类,更确切地说,这两种方法:
#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时,这个解决方案帮助了我。我觉得这也能帮你解决问题。
发布于 2015-03-19 23:51:47
您尝试过调用注释的委托方式吗?
在注释类中创建委托
@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];
}
}谢谢。
https://stackoverflow.com/questions/29156586
复制相似问题