1

I have a simple MapKit app working fine in iOS. It has annotation and when the user clicks on them, the little gray default popup is displayed with the title / subtitle. I even added a UIButton view into it.

So the problem is, I have a search bar above my map. I wanted to resignFirstResponder from the search box whenever the user clicks on the MapView, so I added a simple tap gesture responder. Worked great except now the little gray detail popups no longer show up (only the annotation pins)! I can still tap, zoom, move around etc. Just no popups.

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
tap.cancelsTouchesInView = NO;
tap.delaysTouchesBegan = NO;
tap.delaysTouchesEnded = NO;
[mapView addGestureRecognizer:tap];


-(IBAction)tapped:(UITapGestureRecognizer *)geture {
    [searchBar resignFirstResponder];
}

Is it possible to have the best of both worlds?

4

1 に答える 1

2

次のようなデリゲート メソッドを使用して、カスタム ビューのパン ジェスチャ レコグナイザーに移動するタッチと、カスタム ビューを含むスクロール ビューに移動するタッチを調整しました。それのようなものはあなたのために働くかもしれません。

// the following UIGestureRecognizerDelegate method returns YES by default.
// we modify it so that the tap gesture recognizer only returns YES if
// the search bar is first responder; otherwise it returns NO.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
  if ((gestureRecognizer == self.tapGestureRecognizer) &&
      (gestureRecognizer.view == self.mapView) &&
      [searchBar isFirstResponder])
  {
    return YES;  // return YES so that the tapGestureRecognizer can deal with the tap and resign first responder
  }
  else
  {
    return NO;  // return NO so that the touch is sent up the responder chain for the map view to deal with it
  }
}
于 2012-06-19T03:01:36.527 に答える