iOS 7.1 UITapGesture不能与UIPickerView一起使用

我们正在使用UIPickerView来允许用户从选项列表中进行select。 我们添加UIPickerView作为容器UIView的子视图。 然后,我们将UITapGestureRecognizer添加到容器UIViewUITapGestureRecognizer被用来通过删除超级视图来closuresselect器。

在iOS 7.0和以前的版本,这是按预期工作。 但是,在iOS 7.1中,这个设置不再起作用,因为UITapGestureRecognizer不能识别tap并调用操作中指定的select器( UITapGestureRecognizerselect器视图和容器视图)。 代码如下

  - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.nameList=[[NSMutableArray alloc] initWithObjects:@"A",@"B",@"C", nil]; UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)]; myPickerView.delegate = self; myPickerView.showsSelectionIndicator = YES; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapInListPickerView:)]; [singleTap setNumberOfTapsRequired:1]; [singleTap setNumberOfTouchesRequired:1]; [myPickerView addGestureRecognizer:singleTap]; [self.view addSubview:myPickerView]; } -(void)tapInListPickerView:(UIGestureRecognizer *)sender { NSLog(@"Taped in pikcer view"); } 

如果需要其他信息,或者如果有更好的方法来做到这一点,请让我知道。

我有同样的问题,我终于有了一个启示:P

这就像uiPickerView同时手势rencognizers不起作用。

所以我使用手势代表

 < UIGestureRecognizerDelegate> 

  // add tap gesture UITapGestureRecognizer* gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pickerViewTapGestureRecognized:)]; [picker addGestureRecognizer:gestureRecognizer]; gestureRecognizer.delegate = self; 

 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ // return return true; } 

然后工作!

再见

我设法几乎恢复了原来的function,通过inheritanceUIPickerView并重写hitTest实现。 新的实现首先允许拾取器的所有行来声明触摸事件,然后才允许拾取器自己声明它。

我说几乎是因为对UIPickerView的另一个更改,在select器中可见的视图可能不再存在。 因此,用户点击一个不在选取器中心的行的可见图像,这可能会导致select器滚动而不是select该行,因为它不再存在。

 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { if (self.hidden) { return nil; } else { if (event.type == UIEventTypeTouches) { for (int component = 0; component < self.numberOfComponents; component++) { for (int row = 0; row < [self numberOfRowsInComponent:component]; row++) { UIView *view = [self viewForRow:row forComponent:0]; if (view) { view = [view hitTest:[self convertPoint:point toView:view] withEvent:event]; if (view) { return view; } } } } } return [super hitTest:point withEvent:event]; } } 

接受的答案是非常有帮助的,谢谢! 我已经是UIPickerView的子类。 所以,遵循ZDidier,我做了一个UIGestureRecognizerDelegate的子类,然后像这样覆盖addGestureRecognizer

 - (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer { gestureRecognizer.delegate = self; [super addGestureRecognizer:gestureRecognizer]; } 

这为我修好了。

试试这个Sol:

gestureRecognizer设置为true,以检测所有ios版本

第1步:添加UIGestureRecognizerDelegate

第2步:添加以下代码到您的类文件

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { // return return true; } 

Swift方法为懒:

符合协议UIGestureRecognizerDelegate

 override func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true }