int i=0;
int min=0;
-(void)showTime{
if(i<10){
NSMutableString *time=[[NSMutableString alloc] initWithFormat:@"%d:0%d",min,i];
[shower setText:time];
}
if(i>=10){
NSMutableString *time=[[NSMutableString alloc] initWithFormat:@"%d:%d",min,i];
[shower setText:time];
}
if(i>=60){
i=0;
min++;
NSMutableString *time=[[NSMutableString alloc] initWithFormat:@"%d:0%d",min,i];
[shower setText:time];
}
i++;
}
- (IBAction)start:(id)sender {
//区别:enabled设置为no时同时文字颜色置灰
//startButton.userInteractionEnabled=NO;
startButton.enabled=NO;
[control setTitle:@"停止" forState:UIControlStateNormal];
//这里只输出一次,所以timer每秒循环只是循环showTime方法
NSLog(@"循环一次");
timer =[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(showTime)userInfo:nil
repeats:YES];
}
- (IBAction)stop:(id)sender {
NSString *controlText=[[NSString alloc] initWithFormat:@"%@",control.titleLabel.text] ;
if([controlText isEqualToString:@"停止"]){
[timer invalidate];
startButton.enabled=YES;
[control setTitle:@"重置" forState:UIControlStateNormal];
}else{
[shower setText:@"0:00"];
i=0;
min=0;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。