查找了网上资料,循环效果绝大部分都是增加行数来制造循环的错觉,延时滚动就是利用NSTimer间隔出发滚动事件来制造滚动效果。
代码:
#import <UIKit/UIKit.h>
#import "UserEntity.h"
@interface PickerViewController : UIViewController
{
NSTimer *times;
int j;//设置延时滚动的行数
}
@property (retain,nonatomic) UserEntity *userEntity;
@property (strong,nonatomic) NSMutableArray *pickerData;//设置可变数组存放数据
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
- (IBAction)backButton:(id)sender;
@end
-------------------------------------------------------------------------------------------------------------------
@implementation PickerViewController
@synthesize pickerView;
@synthesize pickerData;
@synthesize userEntity;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(@"min:%d,max:%d",self.userEntity.minNum,self.userEntity.maxNum);
//设置滚动初始位置 等于 扩大行数后的位置(1000/2)
j=500;
int startNum= self.userEntity.minNum;
int endNum =self.userEntity.maxNum;
int value=endNum-startNum;
NSMutableArray *numArray =[NSMutableArray arrayWithCapacity:value+1];
for(int i=startNum;i<=endNum;i++)
{
NSString* str = [NSString stringWithFormat:@"%d",i];
[numArray addObject:str];
}
self.pickerData = numArray;
[pickerView selectRow:1000/2 inComponent:0 animated:NO];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(int)getRandomNumber:(int)from to:(int)to
{
return (int)(from + (arc4random() % (to - from + 1)));
}
- (IBAction)backButton:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)selectButton:(id)sender {
times=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(scollPicker) userInfo:nil repeats:YES];
}
-(void)scollPicker
{
j++;
if(j%100==0)
{
[times invalidate];
times=nil;
int startNum= self.userEntity.minNum;
int endNum =self.userEntity.maxNum;
int value=endNum-startNum;
int getNum = [self getRandomNumber:0 to:1000];
[pickerView selectRow:getNum%value inComponent:0 animated:YES];
[pickerView reloadComponent:0];
NSInteger row = [self.pickerView selectedRowInComponent:0];
NSString *selected = [self.pickerData objectAtIndex:row];
NSString *title=[[NSString alloc] initWithFormat:@"You selected %@!", selected];
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:title message:@"Thanks for choosing." delegate:self cancelButtonTitle:@"You're Welcome" otherButtonTitles:nil,nil];
[alert show];
j=0;
// button.enabled=YES;
}
@implementation PickerViewController
@synthesize pickerView;
@synthesize pickerData;
@synthesize userEntity;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(@"min:%d,max:%d",self.userEntity.minNum,self.userEntity.maxNum);
//设置滚动初始位置 等于 扩大行数后的位置(1000/2)
j=500;
获取随机数范围
int startNum= self.userEntity.minNum;
int endNum =self.userEntity.maxNum;
int value=endNum-startNum;
//初始话数组并赋值
NSMutableArray *numArray =[NSMutableArray arrayWithCapacity:value+1];
for(int i=startNum;i<=endNum;i++)
{
NSString* str = [NSString stringWithFormat:@"%d",i];
[numArray addObject:str];
}
//绑定数据
self.pickerData = numArray;
//初始Picker位置
[pickerView selectRow:1000/2 inComponent:0 animated:NO];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//获取随机数
-(int)getRandomNumber:(int)from to:(int)to
{
return (int)(from + (arc4random() % (to - from + 1)));
}
@implementation PickerViewController
@synthesize pickerView;
@synthesize pickerData;
@synthesize userEntity;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(@"min:%d,max:%d",self.userEntity.minNum,self.userEntity.maxNum);
//设置滚动初始位置 等于 扩大行数后的位置(1000/2)
j=500;
获取随机数范围
int startNum= self.userEntity.minNum;
int endNum =self.userEntity.maxNum;
int value=endNum-startNum;
//初始话数组并赋值
NSMutableArray *numArray =[NSMutableArray arrayWithCapacity:value+1];
for(int i=startNum;i<=endNum;i++)
{
NSString* str = [NSString stringWithFormat:@"%d",i];
[numArray addObject:str];
}
//绑定数据
self.pickerData = numArray;
//初始Picker位置
[pickerView selectRow:1000/2 inComponent:0 animated:NO];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//获取随机数
-(int)getRandomNumber:(int)from to:(int)to
{
return (int)(from + (arc4random() % (to - from + 1)));
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
//扩大行数制造循环
return 1000;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
//求余赋值使扩大后的数与数组值 相同 ,制造循环效果
return [pickerData objectAtIndex:(row%(self.userEntity.maxNum-self.userEntity.minNum+1))];
}
}
else
{for (int i=0; i<3; i++) {
[pickerView selectRow:j inComponent:0 animated:YES];
[pickerView reloadComponent:0];
// button.enabled=NO;
}
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 1000;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [pickerData objectAtIndex:(row%(self.userEntity.maxNum-self.userEntity.minNum+1))];
}
可能有些理解有错,希望求指出指点。