我有一个UIPickerView,在不使用时会逐渐消失到20%的alpha.我希望用户能够触摸选择器并将其淡入.
如果我在主视图上放置touchesBegan方法,我可以让它工作,但这仅在用户触摸View时才有效.我尝试了UIPickerView的子类化,并在那里有一个touchesBegan,但它没有用.
我猜它与Responder链有关,但似乎无法解决.
一个多星期以来,我一直在寻找这个问题的解决方案.即使你的问题超过一年,我也在回答你,希望这会对别人有所帮助.
对不起,如果我的语言不是很技术,但我对Objective-C和iPhone开发很新.
子类化UIpickerView是正确的方法.但是你要覆盖这个- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
方法.无论何时触摸屏幕,都会调用此方法,并返回将对触摸作出反应的视图.换句话说,touchesBegan:withEvent:
将调用其方法的视图.
UIPickerView有9个子视图!在UIPickerView类中,实现- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
将不会返回self
(这意味着touchesBegan:withEvent:
您在子类中的写入将不会被调用),但将返回一个子视图,完全是索引4处的视图(一个名为UIPickerTable的未记录的子类).
诀窍是使- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
方法返回self
所以你必须在控制touchesBegan:withEvent:
,touchesMoved:withEvent:
和touchesEnded:withEvent:
方法.
在这些方法中,为了保持UIPickerView的标准功能,你必须记得再次调用它们,但是在UIPickerTable子视图上.
我希望这是有道理的.我现在无法编写代码,只要我在家,我就会编辑这个答案并添加一些代码.
以下是一些可以满足您需求的代码:
@interface TouchDetectionView : UIPickerView { } - (UIView *)getNextResponderView:(NSSet *)touches withEvent:(UIEvent *)event; @end @implementation TouchDetectionView - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UIView * hitTestView = [self getNextResponderView:touches withEvent:event]; [hitTestView touchesBegan:touches withEvent:event]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UIView * hitTestView = [self getNextResponderView:touches withEvent:event]; [hitTestView touchesMoved:touches withEvent:event]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UIView * hitTestView = [self getNextResponderView:touches withEvent:event]; [hitTestView touchesEnded:touches withEvent:event]; } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { UIView * hitTestView = [self getNextResponderView:touches withEvent:event]; [hitTestView touchesCancelled:touches withEvent:event]; } - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { return self; } - (UIView *)getNextResponderView:(NSSet *)touches withEvent:(UIEvent *)event { UITouch * touch = [touches anyObject]; CGPoint point = [touch locationInView:self]; UIView * hitTestView = [super hitTest:point withEvent:event]; return ( hitTestView == self ) ? nil : hitTestView; }