我们使用UIPickerView
来允许用户从选项列表中进行选择。我们正在添加UIPickerView
作为容器UIView的子视图。然后,我们将UITapGestureRecognizer添加到容器UIView
。 UITapGestureRecognizer
用于通过删除它的超级视图来关闭选择器。
在iOS 7.0和之前的版本中,这是按预期工作的。但是,在iOS 7.1中,此设置不再起作用,UITapGestureRecognizer
无法识别tap并调用操作中指定的选择器(忽略选择器视图和容器视图)。代码在下面
- (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");
}
如果需要任何其他信息或者有更优选的方法,请告知我们。
答案 0 :(得分:87)
我遇到了同样的问题,我终于有了一个启示:P
就像uiPickerView上的同步手势重新识别不起作用。
所以我使用手势委托
<
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;
}
然后它的工作!
见到你
答案 1 :(得分:2)
我通过继承UIPickerView并覆盖hitTest实现,设法几乎恢复原始功能。新的实现首先允许选择器的所有行在我最终允许选择器本身声明它之前声明触摸事件。
我说几乎因为UIPickerView还有另一个更改,其中选择器中可见的视图可能不再存在。因此,用户点击一个不在选择器中心的行的可见图像,这可能导致选择器滚动而不是选择该行,因为它不再存在。
- (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];
}
}
答案 2 :(得分:2)
gestureRecognizer
设置为true以在所有ios版本中检测
第1步:添加UIGestureRecognizerDelegate
第2步:将以下代码添加到您的类文件中
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
// return
return true;
}
答案 3 :(得分:1)
接受的答案非常有帮助,谢谢!我已经是UIPickerView的子类。所以,在ZDidier之后,我将子类设为UIGestureRecognizerDelegate
,然后像这样覆盖addGestureRecognizer
:
- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
gestureRecognizer.delegate = self;
[super addGestureRecognizer:gestureRecognizer];
}
为我修好了。
答案 4 :(得分:0)
懒惰的Swift方法:
符合协议UIGestureRecognizerDelegate
和
override func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}