bub*_*ter 5 cocoa-touch objective-c mapkit mkannotationview
我想知道是否有人知道MKAnnotationView类的任何子类.在苹果文档中,他们说一个例子是MKPinAnnotationView,所以我想知道是否有其他预先创建的子类,如用于跟踪设备当前位置的子类.如果有人有关于创建我自己的MKAnnotationView类的子类的提示,那也很棒.
谢谢,bubster
小智 9
如果有人仍然对此感兴趣:
您可以使用Objective-C运行时函数获取类的所有子类,如下所述:http://cocoawithlove.com/2010/01/getting-subclasses-of-objective-c-class.html
从MKAnnotationView继承的其他类是:
MKTransitCalloutView,MKAdAnnotationView,MKUserLocationView,MKUserLocationBreadCrumbView和MKPinAnnotationView
其中MKPinAnnotationView是唯一记录的.所有其他人都是Apple内部使用的私人课程.
我不知道任何其他模板,但这并不意味着它们不存在.:)
无论如何,这里是如何创建自定义的:创建符合MKAnnotation协议的新类.你需要有一个类型的两个实例变量NSString*命名title,并subtitle和类型之一CLLocationCoordinate2D,并命名为协调适当的setter方法(如属性).这些字符串将显示在标注中.在mapView的委托中-mapView:viewForAnnotation:,以与为UITableView实现数据源类似的方式实现该方法.也就是说,通过标识符使annotationView出列,设置新属性(例如,用于右侧附件视图的UIButtonTypeDetailDisclosure类型的按钮).您需要添加要在偏移下方显示的图像.只需使用imageMKAnnotationView的属性.自定义图像的中心将放置在指定的坐标处,因此您可能需要提供偏移量:aView.centerOffset = CGPointMake(0, -20)
以下是一些示例代码:
- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id<MKAnnotation>) annotation {
// reuse a view, if one exists
MKAnnotationView *aView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"pinView"];
// create a new view else
if (!aView) {
aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"];
}
// now configure the view
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[(UIButton*)aView.rightCalloutAccessoryView addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
aView.canShowCallout = YES;
aView.enabled = YES;
aView.image = [UIImage imageNamed:@"green_pin.png"];
aView.centerOffset = CGPointMake(0, -20);
return aView;
}
Run Code Online (Sandbox Code Playgroud)