Ios 如何使UIPickerView组件环绕?
我想在UIPickerView组件中显示一组连续的数字,但要像Clock->Timer应用程序的seconds组件一样进行包装。我可以启用的唯一行为类似于计时器应用程序的小时组件,在该组件中,您只能向一个方向滚动。我在这里找到了答案: 当它要求一行的标题时,请给出: 代码: 当它表示用户didSelectRow:incomonent:,请使用以下内容: 代码:Ios 如何使UIPickerView组件环绕?,ios,cocoa-touch,uipickerview,Ios,Cocoa Touch,Uipickerview,我想在UIPickerView组件中显示一组连续的数字,但要像Clock->Timer应用程序的seconds组件一样进行包装。我可以启用的唯一行为类似于计时器应用程序的小时组件,在该组件中,您只能向一个方向滚动。我在这里找到了答案: 当它要求一行的标题时,请给出: 代码: 当它表示用户didSelectRow:incomonent:,请使用以下内容: 代码: //我们希望选择总是在第二个集合中(这样它看起来好像在前后都有东西) 如果(行=(2*[行数]){ 行=行%[行计数]; 行+=[行计
//我们希望选择总是在第二个集合中(这样它看起来好像在前后都有东西)
如果(行<[行数]| |行>=(2*[行数]){
行=行%[行计数];
行+=[行计数];
[pickerView selectRow:行不完整:组件动画:否];
}
UIPickerView似乎不支持本机环绕,但您可以通过插入更多要显示的数据集来愚弄它,当选择器停止时,将组件置于数据集的中间。我在这里找到了答案: 当它要求一行的标题时,请给出: 代码: 当它表示用户didSelectRow:incomonent:,请使用以下内容: 代码:
//我们希望选择总是在第二个集合中(这样它看起来好像在前后都有东西)
如果(行<[行数]| |行>=(2*[行数]){
行=行%[行计数];
行+=[行计数];
[pickerView selectRow:行不完整:组件动画:否];
}
UIPickerView似乎不支持本机换行,但您可以通过插入更多要显示的数据集来愚弄它,当选择器停止时,将组件居中放置在数据集的中间。将行数设置为一个较大的数字,并使其从一个较高的值开始,用户滚动滚轮很长一段时间的可能性很小,即使如此,更糟糕的情况是他们会触底
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
// Near-infinite number of rows.
return NSIntegerMax;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
// Row n is same as row (n modulo numberItems).
return [NSString stringWithFormat:@"%d", row % numberItems];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.pickerView = [[[UIPickerView alloc] initWithFrame:CGRectZero] autorelease];
// ...set pickerView properties... Look at Apple's UICatalog sample code for a good example.
// Set current row to a large value (adjusted to current value if needed).
[pickerView selectRow:currentValue+100000 inComponent:0 animated:NO];
[self.view addSubview:pickerView];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSInteger actualRow = row % numberItems;
// ...
}
将行数设置为一个大的数字并使其以高值开始同样容易,用户几乎不可能在很长时间内滚动滚轮——即使如此,更糟糕的情况是他们会触底
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
// Near-infinite number of rows.
return NSIntegerMax;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
// Row n is same as row (n modulo numberItems).
return [NSString stringWithFormat:@"%d", row % numberItems];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.pickerView = [[[UIPickerView alloc] initWithFrame:CGRectZero] autorelease];
// ...set pickerView properties... Look at Apple's UICatalog sample code for a good example.
// Set current row to a large value (adjusted to current value if needed).
[pickerView selectRow:currentValue+100000 inComponent:0 animated:NO];
[self.view addSubview:pickerView];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSInteger actualRow = row % numberItems;
// ...
}
只需多次创建一个数组,就可以多次获得数字。让我们说一下,什么时候需要从0到23的数字,并将其放入一个数组中。我们会像这样做10次
NSString *stdStepper;
for (int j = 0; j<10; j++) {
for(int i=0; i<24; i++)
{
stdStepper = [NSString stringWithFormat:@"%d", i];
[_hoursArray addObject:stdStepper];
}
}
只需多次创建一个数组,就可以多次获得数字。让我们说一下,什么时候需要从0到23的数字,并将其放入一个数组中。我们会像这样做10次
NSString *stdStepper;
for (int j = 0; j<10; j++) {
for(int i=0; i<24; i++)
{
stdStepper = [NSString stringWithFormat:@"%d", i];
[_hoursArray addObject:stdStepper];
}
}
这就是时钟应用程序实现“循环”的方式。NSIntegerMax
NSIntegerMax
可能不是明智之举NSIntegerMax
取决于平台。它会使Lion上的模拟器崩溃(其中NSInteger
被定义为long
)。NSInteger
的iOS定义可能会更改,从而导致相同的崩溃。最好使用已测试的显式数字。正如@BenedictCohen在评论中提到的,这在64位iOS设备上不起作用,因为NSInteger
被定义为long
。我的解决方案是添加一个固定数量的数据集(基本上是:realCount*201
),这样,我每边都有100个重复的数据集(加上实际的数据源),然后当选择器停止滚动时,我移回中央数据源。这种方法还破坏了画外音功能,它将读取[NSIntegerMax]的“[item]”在iOS 9模拟器上失败,原因是:未能为第0节中的9223372036854775807行分配数据存储。考虑使用更少的行这是时钟应用程序如何实现“循环”。NSIntegerMax <代码> NStimeGrimax < /Cord>可能不是明智之举。code>NSIntegerMax取决于平台。它会使Lion上的模拟器崩溃(其中NSInteger
被定义为long
)。NSInteger
的iOS定义可能会更改,从而导致相同的崩溃。最好使用已测试的显式数字。正如@BenedictCohen在评论中提到的,这在64位iOS设备上不起作用,因为NSInteger
被定义为long
。我的解决方案是添加一个固定数量的数据集(基本上是:realCount*201
),这样,我每边都有100个重复的数据集(加上实际的数据源),然后当选择器停止滚动时,我移回中央数据源。这种方法还破坏了画外音功能,它将读取[NSIntegerMax]的“[item]”在iOS 9模拟器上失败,原因是:未能为第0节中的9223372036854775807行分配数据存储。考虑使用更少的行
[_hoursPickerView selectRow:120 inComponent:0 animated:NO];