來選擇一個UIPickerView行我試圖使用一個UIPicker視圖,其行爲與iPhone代碼示例中通常看到的不同。允許用戶通過點擊
我想要做的是允許用戶滾動選擇器的內容,而不是自動選擇選擇器的行(使用picker代表的「didSelectRow」方法)。相反,我想允許用戶觸摸選擇器的中心行,突出顯示,併成爲選擇。
有什麼辦法可以達到這個目的嗎?
在此先感謝。
來選擇一個UIPickerView行我試圖使用一個UIPicker視圖,其行爲與iPhone代碼示例中通常看到的不同。允許用戶通過點擊
我想要做的是允許用戶滾動選擇器的內容,而不是自動選擇選擇器的行(使用picker代表的「didSelectRow」方法)。相反,我想允許用戶觸摸選擇器的中心行,突出顯示,併成爲選擇。
有什麼辦法可以達到這個目的嗎?
在此先感謝。
UIPickerView只有2個代表。
所以,我們只能使用7方法,通過委託來控制UIPickerView。
- pickerView:rowHeightForComponent:
- pickerView:widthForComponent:
- pickerView:titleForRow:forComponent:
- pickerView:viewForRow:forComponent:reusingView:
- pickerView:didSelectRow:inComponent:
- numberOfComponentsInPickerView :
- pickerView:numberOfRowsInComponent:
that'all。
在UITableViewDelegate case中,有更多的UITableView用於管理選擇的方法。 如, - 的tableView:willSelectRowAtIndexPath:
- 的tableView:didSelectRowAtIndexPath方法:
- 的tableView:willDeselectRowAtIndexPath:
- 的tableView:didDeselectRowAtIndexPath:
但是......
在UIPickerViewDelegate情況下,只有一種響應行選擇的方法。
- pickerView:didSelectRow:inComponent:
這就是我的想法。我看過應用程序正在做我想做的事,但我想我正在以其他方式做。不管怎麼說,還是要謝謝你。 – camilo 2010-03-09 10:12:18
作出新的UIControl
[yourcontrol setBackGroundColor: [UIColor clearColor]];
做的方法
- (IBAction) pickerControlTapped { [yourpicker selectRow: rand()% yourpickersize inComponent: 0 animated: YES]; }
0.3。建立連接1和2
[yourcontrol addTarget: self action: @selector(pickerControlTapped) forControlEvents: UIControlEventTouchUpInsied];
之間的手勢識別器添加到觸發目標方法在對象的UIPickerView:
myGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pickerTapped:)];
[myPicker addGestureRecognizer:myGR];
// target method
-(void)pickerTapped:(id)sender
{
// your code
}
Martin,我遇到了你的解決方案,發現它非常有幫助。這麼簡單,它的工作原理。唯一的問題是,在我對水龍頭的迴應中,我怎樣才能將水龍頭傳遞給選取器,以便我能夠在被挖掘的線上獲取對象,而不是中心的對象? – 2012-01-08 13:47:32
@DeanDavids看起來你指出的頁面不再存在。你可以提供另一個,或解釋解決方案? – 2013-03-01 11:46:59
對於任何找到這個答案的谷歌搜索,還有另一個stackoverflow答案,處理允許點擊通過並選擇一個項目 - https://stackoverflow.com/a/25719326/655822 – jp36 2017-11-08 17:31:44
大廈馬丁·林克萊特的回答,支持輕敲採摘其他行: 有一些神奇的數字,但爲我工作。
- (void) pickerTapped:(UITapGestureRecognizer*)gestureRecognizer
{
CGPoint location = [gestureRecognizer locationInView:self.pickerView];
CGFloat halfViewHeight = self.pickerView.frame.size.height/2;
NSInteger row = -1;
if (location.y < halfViewHeight - 22
&& location.y > halfViewHeight - 66)
{
row = [self.pickerView selectedRowInComponent:0] - 1;
}
else if (location.y < halfViewHeight + 22
&& location.y > halfViewHeight - 22)
{
row = [self.pickerView selectedRowInComponent:0];
}
else if (location.y < halfViewHeight + 66
&& location.y > halfViewHeight + 22)
{
row = [self.pickerView selectedRowInComponent:0] + 1;
}
if (row >= 0 && row < [self.content count])
{
id element = [self.content objectAtIndex:row];
if (element)
{
[self.pickerView selectRow:row inComponent:0 animated:YES];
// do more stuff
}
}
}
任何找到這個答案的谷歌搜索,有另一個stackoverflow的答案,處理允許點擊通過和選擇一個項目,而不必依賴於神奇的數字 - https://stackoverflow.com/a/25719326/655822 – jp36 2017-11-08 17:32:12
我有一個相對簡單的解決方案,這個問題對我來說效果很好。使用隱藏的自定義按鈕,您可以在沒有手勢識別器的情況下實現輕擊功能。該解決方案適用於帶有一個組件的拾取器,但我相信它可以適應更多的工作。
首先在界面生成器中或以編程方式添加一個按鈕。將其隱藏起來並與拾取器一樣寬,然後放置它,使其完全位於拾取器的中心,並且位於視圖層次結構的前方。
我正在使用這樣的IBAction來顯示我的選擇器。然而,真正取決於你如何顯示和隱藏選擇器。
- (IBAction)showPicker:(id)sender
{
_picker.hidden = NO;
_buttonPicker.hidden = NO;
}
所有選擇拾取值的動作發生在爲UIControlEventTouchUpInside事件,像這樣的IBAction爲。
- (IBAction)selectPicker:(id)sender
{
//Hide the button so that it doesn't get in the way
_buttonPicker.hidden = YES;
//Make sure we're within range
NSInteger max = _values.count;
NSInteger row = [_picker selectedRowInComponent:0];
if(row >= 0 && row < max) {
NSString *value = [_values objectAtIndex:row];
//Set the label value and hide the picker
_label.text = value;
_picker.hidden = YES;
}
}
我已經稍微修改了來自工作代碼的這個答案的代碼,所以如果它完全被打破了,我們就會道歉。
你能解釋一下爲什麼didSelectRow方法不能幫助你嗎?你有沒有嘗試過使用它,如果有的話,你有什麼問題? – DyingCactus 2010-03-08 18:06:47
問題在於它的工作原理與它所假設的一樣。 : - /基本上我看起來是一個pickerView,pickerView自動選擇它的一個行值,但它只在用戶觸摸時選擇一個值。我試圖做同樣的事情,但我做不到。 – camilo 2010-03-09 14:31:46