【问题标题】:drawRect not called on MKPinAnnotationView subclassMKPinAnnotationView 子类上未调用 drawRect
【发布时间】:2011-04-12 20:52:30
【问题描述】:

我实现了一个扩展 MKPinAnnotationView 的类。我想在图钉下方画一些东西,我希望通过接管 drawRect:rect 消息来实现。我想通过先自己绘制一些东西然后链接到超类来做到这一点。

问题是这个消息没有被发送。我已经尝试将帧大小设置为非空或 nil(经典原因)而没有任何效果。 MKPinAnnotationView 的实现会以某种方式导致 drawRect:rect 消息不发送到子类吗?

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface QueryAnnotationView : MKPinAnnotationView {
}
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString    *)reuseIdentifier;
@end

实施:

#import "QueryAnnotationView.h"
@implementation QueryAnnotationView
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.frame = CGRectMake(0, 0, 65, 100);
    }
    return self;
}
- (void)drawRect:(CGRect)rect
{
    NSLog(@"drawing my own stuff..");
    [super drawRect:rect];
}
@end

【问题讨论】:

    标签: mapkit drawrect mkpinannotationview


    【解决方案1】:

    试试这个:

    在你的(id)initWithAnnotation:(id &lt;MKAnnotation&gt;)annotation reuseIdentifier:(NSString *)reuseIdentifier

    在 if (self) 块的末尾添加这两行:

    self.backgroundColor = any color but clear color (e.g. redColor)
    self.backgroundColor = [UIColor clearColor];
    

    这适用于我的代码,但我不知道为什么。

    另外,你可以试试这个:

    - (void)setAnnotation:(id <MKAnnotation>)annotation
    {
        [super setAnnotation:annotation];
        [self setNeedsDisplay];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多