35

We are using a UIPickerView to allow a user to select from a list of options. We are adding UIPickerView as a subview of a container UIView. We are then adding a UITapGestureRecognizer to the container UIView. The UITapGestureRecognizer is being used to dismiss the picker via removing it's super view.

In iOS 7.0 and previous versions, this was working as expected. However, in iOS 7.1 this set up is no longer working in the sense that UITapGestureRecognizer is not recognizing the tap and calling the selector specified in the action (dismissing the picker view and container view). code is below

   - (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");
}

If any other info is needed or if there is a more preferred method for doing this, please let me know.

Anupam
  • 555
  • 1
  • 5
  • 14

5 Answers5

88

I had the same problem, and I finally had a revelation :P

It was like simultaneous gesture rencognizers on uiPickerView don't work.

so I use the gesture delegate

<
UIGestureRecognizerDelegate>

with

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

with

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

and then its working!

See You

ZDidier
  • 995
  • 6
  • 5
2

I've managed to almost restore the original functionality by subclassing the UIPickerView and override the hitTest implementation. The new implementation first allows all of the rows of the picker to claim the touch event before I finally allow the picker itself to claim it.

I say almost because there is another change to the UIPickerView where views that are visible in the picker may no longer exist. So the user my tap a visible image of a row that is not centered in the picker and it may cause the picker to scroll instead of selecting that row because it doesn't really exist anymore.

- (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];
    }
}
Doug Gerecht
  • 404
  • 4
  • 9
  • I tried your solution, but the delegate action still doesn't get called on single tap. I don't see how this implementation changes the reported behaviour. – user623396 Mar 14 '14 at 15:17
  • I should have mentioned that I also stopped using a gesture attached to the UIPickerView. I now allow the content objects (which in my case happen to be UIButton's) to manage their own touches. – Doug Gerecht Mar 14 '14 at 15:24
2

Try This Sol:

Set gestureRecognizer as true to detect in all ios versions

Step 1: Add UIGestureRecognizerDelegate

Step 2: Add folloeing code into to your class file

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    // return
    return true;
}
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
1

The accepted answer is very helpful, thank you! I was already subclassing UIPickerView. So, following ZDidier, I made the subclass a UIGestureRecognizerDelegate and then overrode addGestureRecognizer like this:

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

That fixed it for me.

John
  • 91
  • 1
  • 9
0

Swift method for the lazy :

conform to protocol UIGestureRecognizerDelegate

and

override func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}
Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134