MKAnnotationView的子类 [英] Subclasses of MKAnnotationView
问题描述
我想知道是否有人知道任何子类的MKAnnotationView类。在苹果文档,他们说一个例子是MKPinAnnotationView所以我想知道是否有其他预先创建的子类,如用于跟踪设备当前位置。
感谢,
bubster
我不知道任何其他模板,但这并不意味着它们不存在。 :)
无论如何,这里是如何创建自定义的:
创建一个符合 MKAnnotation
协议。您将需要有两个类型为 NSString *
的名为 title
和 subtitle
以及类型
CLLocationCoordinate2D
命名坐标和适当的setter方法(例如属性)之一。那些字符串将显示在标注中。在您的mapView的委托中,以类似于为UITableView实现数据源的方式实现方法 -mapView:viewForAnnotation:
。也就是说,通过标识符使注释视图出队,设置新的属性(例如,用于右附件视图的类型为UIButtonTypeDetailDisclosure的按钮)。您将要添加要在偏移下显示的图像。只需使用您的MKAnnotationView的 image
属性。您的自定义图像的中心将放置在指定的坐标,因此您可能想要给一个偏移: aView.centerOffset = CGPointMake(0,-20)
这里是一些示例代码:
- (MKAnnotationView *)mapView: MKMapView *)mapView viewForAnnotation:(id //重新使用视图(如果存在)
MKAnnotationView * aView = [mapView dequeueReusableAnnotationViewWithIdentifier:@pinView];
//创建新视图else
if(!aView){
aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@pinView];
}
//现在配置视图
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[(UIButton *)aView.rightCalloutAccessoryView addTarget:self action:@selector(showDetails :) forControlEvents:UIControlEventTouchUpInside];
aView.canShowCallout = YES;
aView.enabled =是;
aView.image = [UIImage imageNamed:@green_pin.png];
aView.centerOffset = CGPointMake(0,-20);
return aView;
}
I was wondering if anyone knows of any subclasses for the MKAnnotationView class. On apples documentation they say one example is the MKPinAnnotationView so I was wondering if there were other pre-created subclasses like the one used to track the devices current location. If anyone has tips for creating my own subclass of the MKAnnotationView class that be great as well.
Thanks, bubster
I don't know of any other templates, but that does not mean that they don't exist. :)
Anyway, here's how to create custom ones:
Create a new class conforming to the MKAnnotation
protocol. You will need to have two instance variables of type NSString*
named title
and subtitle
and one of type CLLocationCoordinate2D
named coordinate and an appropriate setter method (e.g. property). Those strings are going to be displayed in the callout. In the delegate of your mapView, implement the method -mapView:viewForAnnotation:
in a similar way as you would implement the datasource for a UITableView. That is, dequeueing an annotationView by an identifier, setting the new properties (e.g. a button of type UIButtonTypeDetailDisclosure for the right accessory view). You will want to add an image to display underneath the offset. Just use the image
property of your MKAnnotationView. The center of your custom image will be placed at the coordinate specified, so you may want to give an offset: aView.centerOffset = CGPointMake(0, -20)
Here is some sample code:
- (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;
}
这篇关于MKAnnotationView的子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!