我在iPhone应用程序中处理UIPickerview控件。我想检测UIPickerView的选择指示器上的触摸。请帮助我。现在我使用以下代码
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pickerViewTapGestureRecognized:)];
[self.picker addGestureRecognizer:gestureRecognizer];
-(void)pickerViewTapGestureRecognized:(UITapGestureRecognizer *)gestureRecognizer
{
CGPoint touchpoint = [gestureRecognizer locationInView:gestureRecognizer.view.superview];
CGRect frame = self.picker.frame;
CGRect selectorFrame = CGRectInset(frame, 0.0, self.picker.bounds.size.height * 0.85/2.0);
if (CGRectContainsPoint(selectorFrame, touchpoint)) {
}
}
答案 0 :(得分:0)
从这里开始:Responding to touchesBegan in UIPickerView instead of UIView
子类化UIpickerView是正确的方法。但是你要使用Event:(UIEvent *)事件方法覆盖 - (UIView *)hitTest:(CGPoint)点。无论何时触摸屏幕,都会调用此方法,并返回将对触摸作出反应的视图。换句话说,将调用touchesBegan:withEvent:方法的视图。
UIPickerView有9个子视图!在UIPickerView类实现 - (UIView *)hitTest:(CGPoint)指向withEvent:(UIEvent *)事件不会返回self(这意味着touchesBegan:withEvent:你在子类中写的将不会被调用)但会返回一个子视图,正好是索引4处的视图(一个名为UIPickerTable的未记录的子类)。
诀窍是使 - (UIView *)hitTest:(CGPoint)指向withEvent:(UIEvent *)事件方法返回self,这样你就可以控制touchesBegan:withEvent:,touchesMoved:withEvent:和touchesEnded:withEvent :方法。