【问题标题】:How Do I Add Custom Button/MapPin to MKAnnotation?如何将自定义按钮/映射添加到 MKAnnotation?
【发布时间】:2013-02-28 13:16:20
【问题描述】:

我是 iphone 开发的菜鸟,我正在尝试将自定义按钮添加到注释标注。我向 rightCalloutAccessoryView 添加常规按钮没有问题,但它不适用于自定义样式。我的图像大小是 32 x 32。我还想在这个 stackoverflow 问题here 之后添加一个自定义地图图钉,非常感谢任何帮助。

我的代码

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
// Define your reuse identifier.
static NSString *identifier = @"MapPoint";   

if ([annotation isKindOfClass:[MapPoint class]]) {
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    } else {
        annotationView.annotation = annotation;
    }
    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
    annotationView.animatesDrop = YES;
    annotationView.image = [UIImage imageNamed:@"myimage.png"];

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setImage:[UIImage imageNamed:@"phony2.png"] forState:UIControlStateNormal];
    [rightButton addTarget:self
                    action:@selector(showDetails:)
          forControlEvents:UIControlEventTouchUpInside];
    annotationView.rightCalloutAccessoryView = rightButton;

    return annotationView;
}
return nil;    

}

【问题讨论】:

    标签: iphone uibutton mapkit customization mkannotation


    【解决方案1】:
     [rightButton addTarget:self
                    action:@selector(showDetails:)
    

    删除此行 并设置按钮的框架

    [rightButton setFrame:CGRectMake(0,0,32,32)];
    

    并从 degate 方法中获取点击动作

    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
    

    获得注释视图的位置。

    希望这会对你有所帮助。

    【讨论】:

    • 感谢您的回复。我尝试了你的建议,但我仍然没有任何运气。
    • @B.金钱:你必须更具体的说“没有运气”。
    猜你喜欢
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多