#import "ViewController.h"
@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property(retain)UIPickerView * pick;
@property(retain)NSMutableArray * dateSource;
@property(retain)NSMutableArray * dataSource1;
@property(retain)NSTimer * timer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_pick=[[UIPickerView alloc]init];
_pick.delegate=self;
_pick.dataSource=self;
_pick.showsSelectionIndicator=YES;
[self.view addSubview:_pick];
_dateSource=[NSMutableArray new];
_dataSource1=[NSMutableArray new];
for (int i=0; i<35; i++) {
NSString * str=[NSString stringWithFormat:@"%d",i+1];
NSLog(@"%@",str);
[_dateSource addObject:str];
}
for (int i=0; i<12; i++) {
NSString * str1=[NSString stringWithFormat:@"%d",i+1];
[_dataSource1 addObject:str1];
}
for (int i=0; i<2; i++) {
UIButton * btn=[UIButton buttonWithType:UIButtonTypeCustom];
if (i==0) {
[btn setTitle:@"开始" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside];
}else{
[btn setTitle:@"结束" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(stop:) forControlEvents:UIControlEventTouchUpInside];
}
[btn setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];
btn.frame=CGRectMake(50+i*100, 400, 100, 30);
[self.view addSubview:btn];
}
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 7;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component<=4) {
return _dateSource.count;
}else{
return _dataSource1.count;
}
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 50;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
return 40;
}
//设置每一列的内容 返回值是一个字符串
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component<=4) {
return _dateSource[row];
}else{
return _dataSource1[row];
}
}
-(void)start:(UIButton *)sender
{
//_timer=[NSTimer timerWithTimeInterval:0.3 target:self selector:@selector(timer:) userInfo:nil repeats:YES];
//取消定时器
[_timer invalidate];
_timer=[NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(timer:) userInfo:nil repeats:YES];
NSLog(@"1111");
}
-(void)timer:(NSTimer *)sender
{
[_pick selectRow:0 inComponent:0 animated:NO];
[_pick selectRow:0 inComponent:1 animated:NO];
[_pick selectRow:0 inComponent:2 animated:NO];
[_pick selectRow:0 inComponent:3 animated:NO];
[_pick selectRow:0 inComponent:4 animated:NO];
[_pick selectRow:0 inComponent:5 animated:NO];
[_pick selectRow:0 inComponent:6 animated:NO];
//开始滚动的时候第八行第0列
[_pick selectRow:34 inComponent:0 animated:YES];
[_pick selectRow:34 inComponent:1 animated:YES];
[_pick selectRow:34 inComponent:2 animated:YES];
[_pick selectRow:34 inComponent:3 animated:YES];
[_pick selectRow:34 inComponent:4 animated:YES];
[_pick selectRow:11 inComponent:5 animated:YES];
[_pick selectRow:11 inComponent:6 animated:YES];
}
-(void)stop:(UIButton *)sender
{
[_timer invalidate];
int i1=arc4random()%35;
int i2=arc4random()%35;
int i3=arc4random()%35;
int i4=arc4random()%35;
int i5=arc4random()%35;
int j1=arc4random()%12;
int j2=arc4random()%12;
[_pick selectRow:i1 inComponent:0 animated:YES];
[_pick selectRow:i2 inComponent:1 animated:YES];
[_pick selectRow:i3 inComponent:2 animated:YES];
[_pick selectRow:i4 inComponent:3 animated:YES];
[_pick selectRow:i5 inComponent:4 animated:YES];
[_pick selectRow:j1 inComponent:5 animated:YES];
[_pick selectRow:j2 inComponent:6 animated:YES];
NSLog(@"1122");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end