Dav*_*ter 11 iphone drawrect mkannotationview
我已经实现了一个从MKAnnotation派生的自定义注释,称为ContainerAnnotation,以及一个从MKAnnotationView派生的自定义注释视图和一个名为ContainerAnnotationView的drawRect:方法.由于某种原因,drawRect:方法没有被调用,我无法弄清楚为什么.
这是我的注释视图的源代码.
ContainerAnnotationView.h:
@interface ContainerAnnotationView : MKAnnotationView
{
}
@end
Run Code Online (Sandbox Code Playgroud)
ContainerAnnotationView.m:
@implementation ContainerAnnotationView
- (void) drawRect: (CGRect) rect
{
// Draw the background image.
UIImage * backgroundImage = [UIImage imageNamed: @"container_flag_large.png"];
CGRect annotationRectangle = CGRectMake(0.0f, 0.0f, backgroundImage.size.width, backgroundImage.size.height);
[backgroundImage drawInRect: annotationRectangle];
// Draw the number of annotations.
[[UIColor whiteColor] set];
UIFont * font = [UIFont systemFontOfSize: [UIFont smallSystemFontSize]];
CGPoint point = CGPointMake(2, 1);
ContainerAnnotation * containerAnnotation = (ContainerAnnotation *) [self annotation];
NSString * text = [NSString stringWithFormat: @"%d", containerAnnotation.annotations.count];
[text drawAtPoint: point withFont: font];
}
@end
Run Code Online (Sandbox Code Playgroud)
从我的视图控制器:
- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id <MKAnnotation>) annotation
{
if ([annotation isKindOfClass: [ContainerAnnotation class]])
{
ContainerAnnotationView * annotationView = (ContainerAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier: _containerMapAnnotationId];
if (annotationView == nil)
{
annotationView = [[[ContainerAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: _containerMapAnnotationId] autorelease];
annotationView.centerOffset = CGPointMake(0, -17.5);
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];
annotationView.canShowCallout = YES;
}
annotationView.annotation = annotation;
return annotationView;
}
// etc...
}
Run Code Online (Sandbox Code Playgroud)
我有其他注释使用香草MKAnnotation与图像工作正常.我还有另一个不实现drawRect的自定义注释视图:工作正常.知道我在这里做错了吗?
Dav*_*ter 23
结果问题是我的drawRect:方法从未被调用过,因为框架没有设置为非零大小.添加initWithAnnotation:方法来解决这个问题.
- (id) initWithAnnotation: (id <MKAnnotation>) annotation reuseIdentifier: (NSString *) reuseIdentifier
{
self = [super initWithAnnotation: annotation reuseIdentifier: reuseIdentifier];
if (self != nil)
{
self.frame = CGRectMake(0, 0, 30, 30);
self.opaque = NO;
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4204 次 |
| 最近记录: |