• <small id='uTsEk'></small><noframes id='uTsEk'>

      <bdo id='uTsEk'></bdo><ul id='uTsEk'></ul>

    <tfoot id='uTsEk'></tfoot>
    1. <legend id='uTsEk'><style id='uTsEk'><dir id='uTsEk'><q id='uTsEk'></q></dir></style></legend>

        <i id='uTsEk'><tr id='uTsEk'><dt id='uTsEk'><q id='uTsEk'><span id='uTsEk'><b id='uTsEk'><form id='uTsEk'><ins id='uTsEk'></ins><ul id='uTsEk'></ul><sub id='uTsEk'></sub></form><legend id='uTsEk'></legend><bdo id='uTsEk'><pre id='uTsEk'><center id='uTsEk'></center></pre></bdo></b><th id='uTsEk'></th></span></q></dt></tr></i><div id='uTsEk'><tfoot id='uTsEk'></tfoot><dl id='uTsEk'><fieldset id='uTsEk'></fieldset></dl></div>

        如何从通过 MKAnnotationView 添加的按钮获取点击事件

        时间:2023-07-09
        <legend id='DjxF9'><style id='DjxF9'><dir id='DjxF9'><q id='DjxF9'></q></dir></style></legend>

          <small id='DjxF9'></small><noframes id='DjxF9'>

            <tfoot id='DjxF9'></tfoot>
              <tbody id='DjxF9'></tbody>

              <bdo id='DjxF9'></bdo><ul id='DjxF9'></ul>
            • <i id='DjxF9'><tr id='DjxF9'><dt id='DjxF9'><q id='DjxF9'><span id='DjxF9'><b id='DjxF9'><form id='DjxF9'><ins id='DjxF9'></ins><ul id='DjxF9'></ul><sub id='DjxF9'></sub></form><legend id='DjxF9'></legend><bdo id='DjxF9'><pre id='DjxF9'><center id='DjxF9'></center></pre></bdo></b><th id='DjxF9'></th></span></q></dt></tr></i><div id='DjxF9'><tfoot id='DjxF9'></tfoot><dl id='DjxF9'><fieldset id='DjxF9'></fieldset></dl></div>
                1. 本文介绍了如何从通过 MKAnnotationView 添加的按钮获取点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  任何人都知道是否有办法从添加到 MKAnnotationViewbutton 获取点击事件,这个 button 仅用作标签在 map 上显示每个引脚的名称,现在我成功地在 pin 被点击,所以当按钮(标签)被点击时我需要做同样的事情.

                  Anyone know if there's a way to get click event from a button that is added to MKAnnotationView, this button is used as label just to display the name of each pin on the map , now I successd to show a custom view (which contains image, text ....) when the pin is clicked so i need to do the same thing when the button (label) is clicked.

                  感谢您提供的任何建议.

                  Thanks for any advice you can provide.

                  MKAnnotationViewbutton的代码:

                  UIButton * pinButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 140, 28)];
                  [pinButton.titleLabel setTextColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1]]; 
                  [pinButton setCenter:CGPointMake(pinAnnotationView.center.x + 70, pinAnnotationView.center.y + 10)]; 
                  [pinButton addTarget:self action:@selector(pinLabelClicked) forControlEvents:UIControlEventTouchUpInside]; 
                  [pinAnnotationView addSubView:pinButton]; 
                  [pinButton setUserInteractionEnabled:YES];
                  

                  推荐答案

                  标准的 UI 方法是使用 callout view 并添加一个附件按钮作为 progrmr 显示.

                  The standard UI approach is to use the callout view and add an accessory button as progrmr shows.

                  但是,如果您必须将按钮直接添加到 MKAnnotationView,那么您的方法的问题是 MKPinAnnotationView 的默认框架(这不容易更改)比您添加的按钮小,因此大多数按钮不会响应触摸,即使您切换到使用 MKAnnotationView 并增加帧大小,MKMapView 将阻止按钮获得任何触摸.

                  However, if you must add a button directly to the MKAnnotationView, the problems with your approach are that the MKPinAnnotationView's default frame (which can't easily be changed) is smaller than the button you're adding so most of the button will not respond to touches and even if you switch to using an MKAnnotationView and increase the frame size, the MKMapView will prevent the button from getting any touches.

                  您需要做的是将 UITapGestureRecognizer 添加到按钮(使用手势处理程序的操作方法而不是按钮上的 addTarget)并将按钮添加到普通 MKAnnotationView 具有适当的帧大小,而不是 MKPinAnnotationView.

                  What you'll need to do is add a UITapGestureRecognizer to the button (use the gesture handler's action method instead of an addTarget on the button) and add the button to a plain MKAnnotationView with an appropriate frame size instead of an MKPinAnnotationView.

                  例子:

                  - (MKAnnotationView *)mapView:(MKMapView *)mapView 
                          viewForAnnotation:(id<MKAnnotation>)annotation
                  {
                      MKAnnotationView *annView = (MKAnnotationView *)[mapView 
                              dequeueReusableAnnotationViewWithIdentifier: @"pin"];
                      if (annView == nil)
                      {
                          annView = [[[MKAnnotationView alloc] initWithAnnotation:annotation 
                                        reuseIdentifier:@"pin"] autorelease];
                  
                          annView.frame = CGRectMake(0, 0, 200, 50);
                  
                          UIButton *pinButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                          pinButton.frame = CGRectMake(0, 0, 140, 28);
                          pinButton.tag = 10;
                  
                          UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] 
                              initWithTarget:self action:@selector(handlePinButtonTap:)];
                          tap.numberOfTapsRequired = 1;
                          [pinButton addGestureRecognizer:tap];
                          [tap release];
                  
                          [annView addSubview:pinButton]; 
                      }
                  
                      annView.annotation = annotation;
                  
                      UIButton *pb = (UIButton *)[annView viewWithTag:10];
                      [pb setTitle:annotation.title forState:UIControlStateNormal];
                  
                      return annView;
                  }
                  
                  - (void) handlePinButtonTap:(UITapGestureRecognizer *)gestureRecognizer 
                  {
                      UIButton *btn = (UIButton *) gestureRecognizer.view;
                      MKAnnotationView *av = (MKAnnotationView *)[btn superview];
                      id<MKAnnotation> ann = av.annotation;
                      NSLog(@"handlePinButtonTap: ann.title=%@", ann.title);
                  }
                  


                  请注意,这将阻止地图视图的 didSelectAnnotationView 委托方法触发.如果您需要触发该方法(在 addition 到按钮的手势处理程序方法中),则添加以下内容:


                  Note that this will prevent the map view's didSelectAnnotationView delegate method from firing. If you need that method to fire (in addition to the button's gesture handler method), then add the following:

                  //in the view controller's interface:
                  @interface YourVC : UIViewController <UIGestureRecognizerDelegate>
                  
                  //where the UITapGestureRecognizer is created:
                  tap.delegate = self;
                  
                  - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
                          shouldRecognizeSimultaneouslyWithGestureRecognizer
                              :(UIGestureRecognizer *)otherGestureRecognizer
                  {
                      return YES;
                  }
                  

                  这篇关于如何从通过 MKAnnotationView 添加的按钮获取点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将情节提要中的多个按钮连接到单个动作? 下一篇:多行 UIButton 和自动布局

                  相关文章

                  <tfoot id='yhQlJ'></tfoot>
                  <i id='yhQlJ'><tr id='yhQlJ'><dt id='yhQlJ'><q id='yhQlJ'><span id='yhQlJ'><b id='yhQlJ'><form id='yhQlJ'><ins id='yhQlJ'></ins><ul id='yhQlJ'></ul><sub id='yhQlJ'></sub></form><legend id='yhQlJ'></legend><bdo id='yhQlJ'><pre id='yhQlJ'><center id='yhQlJ'></center></pre></bdo></b><th id='yhQlJ'></th></span></q></dt></tr></i><div id='yhQlJ'><tfoot id='yhQlJ'></tfoot><dl id='yhQlJ'><fieldset id='yhQlJ'></fieldset></dl></div>
                  <legend id='yhQlJ'><style id='yhQlJ'><dir id='yhQlJ'><q id='yhQlJ'></q></dir></style></legend>
                2. <small id='yhQlJ'></small><noframes id='yhQlJ'>

                    <bdo id='yhQlJ'></bdo><ul id='yhQlJ'></ul>