1

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
4

1 に答える 1

2

これを試して:

あなたの(id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier

if (self) ブロックの最後に次の 2 行を追加します。

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

これは私のコードでは機能しますが、その理由はわかりません。

また、これを試すことができます:

- (void)setAnnotation:(id <MKAnnotation>)annotation
{
    [super setAnnotation:annotation];
    [self setNeedsDisplay];
}
于 2012-03-22T00:36:58.380 に答える