这是可能的。这是你如何做到的。首先设置一个定时器。让我们假设int maxNumber
一个实例变量设置为某个任意值。
- ( void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.timer invalidate];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
}
- (void) viewWillDisappear:(BOOL)animated{
[self.timer invalidate];
[super viewWillDisappear:animated];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.infinitePickerView selectRow:(int)maxNumber*5 inComponent:0 animated:YES];
}
在计时器触发方法中,检查是否显示了 uipickerview 的任何“边缘”视图。
- (void)timerFireMethod:(NSTimer*)theTimer{
int rowSelected = [self.infinitePickerView selectedRowInComponent:0];
for (int i=0; i<= 20; i++) {
UIView * viewBelow = [self.infinitePickerView viewForRow:i forComponent:0];
UIView * viewAbove = [self.infinitePickerView viewForRow:maxNumber*10-20+i forComponent:0];
if(viewBelow!=nil || viewAbove!=nil){
int middlePosition = maxNumber * 5 + (rowSelected % maxNumber);
[self.infinitePickerView selectRow:middlePosition inComponent:0 animated:NO];
break;
}
}
}
请注意,这是有效的,因为[self.infinitePickerView viewForRow:i forComponent:0];
仅当 UIView 可见时才返回它。
当然你UIPickerViewDelegate
必须使用类似
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return maxNumber * 10; //returning a large number
}
//You cannot use this method
/*
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{}
You have to use the method below in lieu of it
*/
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel * label;
if (!view) {
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.infinitePickerView.bounds.size.width, 30)];
}else {
label = (UILabel *) view;
}
[label setText:[NSString stringWithFormat:@" %d", row % maxNumber]];
return label;
}
希望这有效!:) 祝你好运!