gpt4 book ai didi

iphone - 我的自定义 MKAnnotationView 不可点击

转载 作者:可可西里 更新时间:2023-11-01 04:35:38 25 4
gpt4 key购买 nike

我正在处理显示背景图像的自定义注释 View ,其中包含自定义图像。我的代码基于此:Custom MKAnnotationView with frame,icon and image而我的实际代码是:

    else if ([annotation isKindOfClass:[ImagePin class]]){
static NSString *identifierImage = @"ImagePinLocation";
MKAnnotationView *annotationImageView = (MKAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifierImage];
if(annotationImageView){
return annotationImageView;
}
else {
annotationImageView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifierImage];
annotationImageView.canShowCallout = YES;

annotationImageView.image = [UIImage imageNamed:@"image_bg_mappa"];

UIImage *frame = [UIImage imageNamed:@"image_bg_mappa"];
ImagePin *imagePinImage = annotation;
NSLog(@"%@", [NSString stringWithFormat:@"%@", imagePinImage.image]);


NSString *urlStringForImage = [NSString stringWithFormat:@"%@", imagePinImage.image];
NSURL *urlForImage = [NSURL URLWithString:urlStringForImage];
UIImageView *imageView = [[UIImageView alloc] init];

[imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", urlForImage]] placeholderImage:[UIImage imageNamed:@"avatar-placeholder.png"]];

UIGraphicsBeginImageContext(CGSizeMake(annotationImageView.image.size.width, annotationImageView.image.size.height));

[frame drawInRect:CGRectMake(0, 0, frame.size.width, frame.size.height)];
[imageView.image drawInRect:CGRectMake(2, 2, 48, 38)]; // the frame your inner image

annotationImageView.image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:@selector(writeSomething:) forControlEvents:UIControlEventTouchUpInside];
[rightButton setTitle:@"" forState:UIControlStateNormal];
annotationImageView.canShowCallout = YES;
annotationImageView.rightCalloutAccessoryView = rightButton;
annotationImageView.enabled = YES;
return annotationImageView;
}
}

然后我有两个方法:

(void)writeSomething {        
}

(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
}

但是:

  1. 不显示我的右按钮标注(不显示按钮)
  2. 我更喜欢在单击完整图像/注释时调用 mapView annotationView 方法。

我该怎么做?为什么当我点击注释时,它不调用我的 calloutAccessoryControlTapped 方法?在这张 map 中,我还有更多不同类型的引脚(mkpinannotation),并且工作正常(方法 calloutAccessoryControlTapped 也可以很好地处理这些引脚)。问题出在哪里 ?谢谢大家,对不起我的英语不好(我正在学习)。

编辑:我的 viewForAnnotation 方法的完整代码:https://gist.github.com/anonymous/6224519e308d6bf56c4cMKPinAnnotation 工作正常。

编辑:这是我的 ImagePin.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface ImagePin : NSObject <MKAnnotation>

- (id)initWithImage:(NSString*)image imagebeachId:(NSString*)imagebeachId coordinate:(CLLocationCoordinate2D)coordinate;
//- (MKMapItem*)mapItem;
@property (nonatomic, copy) NSString *imagebeachId;
@property (nonatomic, copy) NSString *image;
@property (nonatomic, assign) CLLocationCoordinate2D theCoordinate;
@end

还有我的ImagePin.m

#import "ImagePin.h"
#import <AddressBook/AddressBook.h>

@interface ImagePin ()

@end

@implementation ImagePin
@synthesize image = _image;
@synthesize imagebeachId = _imagebeachId;
@synthesize theCoordinate = _theCoordinate;

- (id)initWithImage:(NSString*)image imagebeachId:(NSString*)imagebeachId coordinate:(CLLocationCoordinate2D)coordinate {
if ((self = [super init])) {
//self.address = address;
self.image = image;
self.imagebeachId = imagebeachId;
self.theCoordinate = coordinate;
}
return self;
}
- (NSString *)title {
return @"";
}
- (NSString *)subtitle {
return _image;
}

- (CLLocationCoordinate2D)coordinate {
return _theCoordinate;
}
@end

编辑:这是我添加注释的地方:

- (void)imagePositions:(NSDictionary *)responseData {
for (id<MKAnnotation> annotation in self.mapView.annotations) {
if ([annotation isKindOfClass:[ImagePin class]]){
//[self.mapView removeAnnotation:annotation];
}
}

for (NSArray *row in responseData) {
NSString * imageBeachId = [row valueForKey:@"id"];

NSNumber * latitude = [row valueForKey:@"lat"];


NSNumber * longitude = [row valueForKey:@"lng"];
NSString * imageBeach = [row valueForKey:@"fb_photo_url"];


CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude.doubleValue;
coordinate.longitude = longitude.doubleValue;
ImagePin *annotation = [[ImagePin alloc] initWithImage:imageBeach imagebeachId:imageBeachId coordinate:coordinate];

[self.mapView addAnnotation:annotation];
}
}

最佳答案

我都修好了!问题是标题不能为空(我使用 @""... 和 @"AAA 工作正常)然后不显示我已经完成的标注:

annotationView.canShowCallout = NO;

并实现:

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
//here you action
}

非常感谢 Anna(解决方案是您的)!

关于iphone - 我的自定义 MKAnnotationView 不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17698158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com