简体   繁体   English

如何在iPhone中检测UIPickerView的选择指示器上的触摸?

[英]How can i detect touch on selection indicator of UIPickerView in iphone?

I am working on UIPickerview control in iPhone app.I want to detect touch on selection indicator of UIPickerView.Please help me.Now i am using the following code 我正在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)) {

  }
}

From here: Responding to touchesBegan in UIPickerView instead of UIView 从这里开始: 在UIPickerView而不是UIView中响应touchesBegan

Subclassing UIpickerView is the right way to do it. 子类化UIpickerView是正确的方法。 But you've to override the - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event method. 但是您必须使用Event:(UIEvent *)event方法重写-(UIView *)hitTest:(CGPoint)点。 This is the method called whenever you touch the screen and it returns the view that will react to the touch. 每当您触摸屏幕时都会调用此方法,它会返回对触摸有反应的视图。 In other words the view whose touchesBegan:withEvent: method will be called. 换句话说,将调用其touchesBegan:withEvent:方法的视图。

The UIPickerView has 9 subviews! UIPickerView有9个子视图! In the UIPickerView class implementation - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event won't return self (this means the touchesBegan:withEvent: you write in the subclass won't be called) but will return a subview, exactly the view at index 4 (an undocumented subclass called UIPickerTable). 在UIPickerView类的实现中-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event不会返回self(这意味着touchesBegan:withEvent:您在子类中编写的不会被调用)但会返回一个子视图,恰好是索引4处的视图(一个未记录的子类,称为UIPickerTable)。

The trick is to make the - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event method to return self so you have control over the touchesBegan:withEvent:, touchesMoved:withEvent: and touchesEnded:withEvent: methods. 诀窍是使-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event方法返回self,以便您可以控制touchesBegan:withEvent:,touchesMoved:withEvent:和touchesEnded:withEvent:方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM