2014-05-05 34 views
0

我想實現一個iOS自定義標註視圖,它顯示在被點擊的針腳的右側。我最終做的方式有點像this教程。
基本上我在mapView:didSelectAnnotationView:delegate回調(第二個參數是註解視圖)中向註釋視圖添加了一個子視圖。它顯示了我想如何,但它不能接收觸摸事件。我發現它是(可能是),因爲我添加的UIView不在註釋視圖的框架中,如果我點擊邊框中的內容,它仍然會被識別,但是沒有其他地方。我還沒有找到任何其他方式來做這種事情,我不知道如何使觸摸工作在UIView的任何地方。
我試圖使註解視圖的框架更大,但它也使得pin(圖像)更大,它不是一種方法。

任何想法?iOS MapKit針腳側面的自定義標註

回答

0

不要將子視圖添加到註釋中,而要將子類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]; 
}