gpt4 book ai didi

ios - 单击注释两次并在第二次单击时执行操作

转载 作者:行者123 更新时间:2023-11-28 20:01:59 24 4
gpt4 key购买 nike

我有一个 map View ,并且绘制了来自 JSON 响应的坐标。当他们第一次点击它时,它会显示引脚/注释名称。现在我需要以某种方式允许用户再次单击相同的图钉/注释并执行序列和一些操作/代码。我该怎么做?

最佳答案

以下是我在许多应用程序中实现此功能的方法...首先,我创建了自己的自定义注释,其中包含一些我可能想要存储的值,这些值对于与 pin 关联的每个对象都是唯一的。在本例中,它是一个 URL。这是一个通用的“CustomAnnotation”对象的 .h 和 .m:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface CustomAnnotation : NSObject <MKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, retain) NSString *URL;


@end

// the .m file doesn't need anything else in it

#import "CustomAnnotation.h"
#import <MapKit/MapKit.h>

@implementation CustomAnnotation

@end

然后在 View Controller 中我添加了 map View ,你需要添加注释,自定义点击它时显示的 View ,并处理操作:

- (void)createMapAndAddAnnotation {
// create the mapview and add it to the view
MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
mapView.delegate = self;
[self.view addSubview:mapView];

// create the annotation however you want, this is just for demo purposes
CustomAnnotation *annotation = [[CustomAnnotation alloc] init];
annotation.coordinate = CLLocationCoordinate2DMake(50, 50);
annotation.title = @"Test Title";
annotation.subtitle = @"Subtitle";
annotation.URL = @"www.google.com";
[mapView addAnnotation:annotation];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
// if the annotation is a custom annotation
if ([annotation isKindOfClass:[CustomAnnotation class]]) {
// create the reuse identifier so we can recycle annotatinons
NSString *pinAnnotationViewReuseID = @"PinAnnotationView";

// attempt to reuse one
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinAnnotationViewReuseID];

// if we were unable to dequeue one for reuse
if (!pinView) {
// create and format it accordingly
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinAnnotationViewReuseID];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}

// otherwise all we want to do is set the annotation on the pin
else {
pinView.annotation = annotation;
}

// and return it
return pinView;
}

// otherwise we don't want to return one at all
else {
return nil;
}
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
// get the annotation object from the view that was tapped
CustomAnnotation *tappedAnnotation = view.annotation;

// get the info from the annotation itself
NSString *urlFromAnnotation = tappedAnnotation.URL;

// do something with the info - in this case I'm just popping up an alert for demo purposes
[[[UIAlertView alloc] initWithTitle:@"The URL was:" message:urlFromAnnotation delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}

这是显示结果的视频:

https://www.dropbox.com/s/rbcpvo3cuspgly9/annotation.mov

编辑:根据您的编辑/问题

你说你添加了自定义注释文件,但是在你展示的代码中你没有使用自定义注释对象,你只是使用了一个常规的 MKPointAnnotation ......下面我稍微编辑了你的代码以使其更具可读性和清楚 - 你可能想尝试让你的变量名和字典键更有意义。这一切都假设您有一个名为 employees 的数组,其中充满了字典,并且每个字典都有一个名为“GymName”的键,并且该键的值是一个包含 lat 和 lon 值的 % 分隔字符串?

for (int index = 0; index < employees.count; index++) {
NSLog(@"Inside Loop!");

// get the employee dictionary based on the array index
NSDictionary *employee = employees[index];

// get the coordinate string - I'm guessing based on your code it's something like "45.254%72.156"
NSString *coordinateString = employee[@"GymName"];

// break up the lat and lon into components in an array
NSArray *coordinateComponents = [coordinateString componentsSeparatedByString:@"%"];

// get the latitude and longitude values from the components array and conver them to doubles
double latitude = [coordinateComponents[0] doubleValue];
double longitude = [coordinateComponents[1] doubleValue];

// create the coordinate object from the lat and long
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);

// create the custom annotation and add it to the map
CustomAnnotation *annotation = [[CustomAnnotation alloc] init];
annotation.coordinate = coordinate;
[_mapView addAnnotation:annotation];
}

关于ios - 单击注释两次并在第二次单击时执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23621386/

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