2013-08-30 84 views
0

我在iPhone上app.I控制UIPickerview工作要me.Now我用下面的代碼我如何檢測觸摸在iphone的選擇指標的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

你想要什麼?爲什麼你想這樣做?你面臨的問題是什麼? – Bhavin

+0

您是否需要在選取器視圖中返回選定的項目或返回座標? –

+0

@ Vin,我需要觸摸發生在selectionIndicator或UIPickerview的其他部分。如何檢測觸摸? – IKKA

回答

0

從這裏來檢測的UIPickerView.Please幫助選擇顯示器的觸摸:Responding to touchesBegan in UIPickerView instead of UIView

子類UIpickerView是正確的做法。但是你必須用事件:(UIEvent *)事件方法覆蓋 - (UIView *)hitTest:(CGPoint)點。這是在您觸摸屏幕時調用的方法,它返回將對觸摸做出反應的視圖。換句話說,其touchesBegan:withEvent:方法的視圖將被調用。

UIPickerView有9個子視圖!在UIPickerView類的實現中 - (UIView *)hitTest:(CGPoint)指向withEvent:(UIEvent *)事件不會返回self(這意味着touchesBegan:withEvent:你在子類中寫入的內容不會被調用),而是會返回一個子視圖,就是索引4處的視圖(一個名爲UIPickerTable的未記錄子類)。竅門是讓 - (UIView *)hitTest:(CGPoint)指向withEvent:(UIEvent *)事件方法返回自我,讓您控制touchesBegan:withEvent:,touchesMoved:withEvent:和touchesEnded: withEvent:方法。