允许用户通过点击
来选择一个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
- 同样的位置UIPickerView
[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