iOS 7.1 UITapGesture not working with UIPickerView

廉价感情. 提交于 2019-11-27 13:27:53

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

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

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

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.

Swift method for the lazy :

conform to protocol UIGestureRecognizerDelegate

and

override func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!