1

我有一个简单的 MapKit 应用程序在 iOS 中运行良好。它有注释,当用户单击它们时,会显示带有标题/副标题的小灰色默认弹出窗口。我什至在其中添加了一个 UIButton 视图。

所以问题是,我的地图上方有一个搜索栏。每当用户单击 MapView 时,我想从搜索框中退出 FirstResponder,因此我添加了一个简单的点击手势响应器。效果很好,除了现在不再出现灰色的小细节弹出窗口(只有注释图钉)!我仍然可以点击、缩放、移动等。只是没有弹出窗口。

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];
}

有没有可能两全其美?

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 回答