不要將子視圖添加到註釋中,而要將子類MKAnnotationView添加並實現所需的方法。如果您想要完全控制圖紙,只需覆蓋-drawRect:
。請記住,MKAnnotationView是一個特殊的類,它有點像UITableViewCell,因爲它有一些特殊的處理,可以直接使用它。這是一個讓你開始的例子。
- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self) {
[self commonInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInit];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
- (id)init
{
self = [super init];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit
{
self.frame = CGRectMake(0, 0, 40, 40);
self.backgroundColor = [UIColor clearColor];
self.canShowCallout = YES;
}
- (void)drawRect:(CGRect)rect
{
//// Oval Drawing
CGRect imageBox = CGRectMake(5, 5, 25, 25);
UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect:imageBox];
[[UIColor clouds] setFill];
[ovalPath fill];
[[UIColor clouds] setStroke];
ovalPath.lineWidth = 6;
[ovalPath stroke];
CGFloat radius = imageBox.size.width/2;
UIImage *img = [UIView rasterizedStatusImageForCheckpoint:self.checkpoint withSize:imageBox.size];
img = [img makeRoundedImage:img radius:radius];
[img drawInRect:imageBox];
}