简单定时器Demo

#import <UIKit/UIKit.h>

@interface LYViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
    IBOutlet UILabel *timeLabel;
    NSTimer *timer;
    IBOutlet UITableView * tableView;
    IBOutlet UIButton *startAndEnd;
    
    int minute;
    int second;
    int millisecond;
    NSMutableArray * record;
    int isEnd;
}
-(IBAction)startAndEnd:(id)sender;
-(void)updateTimer;
-(IBAction)recordOnce:(id)sender;
-(IBAction)reset:(id)sender;
@end
#import "LYViewController.h"

@implementation LYViewController


-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        ///
    } 
    return self;
    
}

- (void)viewDidLoad
{
    //数组一定要先初始化
    record=[[NSMutableArray alloc]init];
    isEnd=0;
    [super viewDidLoad];
//   /后台运行
//    [self performSelectorInBackground:@selector(updateTimer) withObject:nil];
}

-(IBAction)startAndEnd:(id)sender
{
    if (isEnd==0) {
        [startAndEnd setTitle:@"暂停" forState:UIControlStateNormal];
          timer=[NSTimer  scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES] ;
        //增加进程
        [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
        isEnd=1;
    }
    else
    {
        [startAndEnd setTitle:@"开始计时" forState:UIControlStateNormal];
        isEnd=0;
        [timer invalidate];
    }
   
}
-(void)updateTimer
{
    [timeLabel setText:[NSString stringWithFormat:@"%02d:%02d:%02d",minute,second,millisecond]];
    millisecond++;
    if (millisecond==100) {
        millisecond=0;
        second++;
        if (second==60) {
            second=0;
            minute++;
        }
    }

}
-(IBAction)reset:(id)sender
{
    [timer invalidate];
    minute=0;
    second=0;
    millisecond=0;
    [timeLabel setText:[NSString stringWithFormat:@"%02d:%02d:%02d",minute,second,millisecond]];
    record =nil;
    [tableView reloadData];
    [startAndEnd setTitle:@"开始计时" forState:UIControlStateNormal];
    isEnd=0;
    record=[[NSMutableArray alloc]init];
 }
-(IBAction)recordOnce:(id)sender;
{
    NSString *temp=[NSString stringWithFormat:@"第%d次 %02d:%02d:%02d",[record count]+1,minute,second,millisecond];
    [record addObject:temp];
    [tableView reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [record count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
  [[cell textLabel] setText:[record objectAtIndex:[indexPath row]]];
    return cell;
}
@end










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值