TapgestureRecogniser将触摸检测为Scrollview

时间:2013-04-16 10:29:49

标签: iphone ipad uiscrollview xcode4.5 uitapgesturerecognizer

我正在向一个scrollview添加一个UIPickerView。当我从pickerview中选择一行时,它被隐藏了,我无法选择它,因为当用户点击屏幕上的任何地方(scrollView)而不是pickerView时,我编写了删除pickerview的代码。但现在即使我点击(选择)pickerview我隐藏了浏览器视图,因为触摸被视为ScrollView.I无法理解我出错的地方。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{

    if(touch.view==testScroll)
    {

        if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
        {
            ViewForValuePicker.hidden=true;//view to which pickerview is added and this s view is added to scrollview
            pickerView.hidden=YES;

        }
        return YES; 
    } 
   else

        return NO; 
}

点击屏幕上的任意位置时,如果点击选择器视图,我该怎么做才能删除选择器视图

2 个答案:

答案 0 :(得分:0)

尝试这样可能对你有帮助,

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  {
    NSLog(@"touches began");
    UITouch *touch = [touches anyObject];   
    if([touch view] isKindOfClass:[UIScrollView class]){

    //do whatever you want to do  here

   }
}

答案 1 :(得分:0)

你可以做的是为pickerview创建第二个手势识别器,它会吞下不应该发送到scrollview的那些水龙头。

请参阅图示一个完整的测试项目来实现这一目标。您可以看到,点击pickerview被路由到 gestureswallover 方法,并且pickerview的外部所有点击都被路由到 gesturehelper 方法,因此您可以在这些方法中实现您的逻辑。

在ViewController.h中,这些是手势识别器的动作方法:

- (IBAction) gesturehelper: (UITapGestureRecognizer*) sender;
- (IBAction) gestureswallover: (UITapGestureRecognizer*) sender;

enter image description here

在我的ViewController.m中我有这个(特定逻辑应该包含在动作方法中):

- (IBAction) gesturehelper: (UITapGestureRecognizer*) sender {
    NSLog(@"gesturehelper tap, %@", sender.view);
}
- (IBAction) gestureswallover: (UITapGestureRecognizer*) sender {
    NSLog(@"gestureswallover tap, %@", sender.view);    
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return 5;
}