iOS程序入门开发练习:秒表stopWatch

看了念茜的博客,按她的思路自己也动手写了一个。感觉她的代码里有些东西是没有必要的,并对她的逻辑进行了小的修改:start为YES时时间转,为NO时停止。这样比较符合我的思维习惯,

另外在创建NSTimer那里,使用scheduledTimerWithTimeInterval函数是创建一个NSTimer并把它放进了默认的runloop里,此runloop的模式是默认的NSDefaultRunLoopMode,应该将默认模式改成NSRunLoopCommonModes,不然在程序跑起来的时候拖拽tableView会卡住。其中的细节与原理还有待深究

下面是.m的代码:


#import "firstfbAppViewController.h"


@interface firstfbAppViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSString *timeStr;
    BOOL start; //记录当前状态
    NSTimer *timer;
    float secCount;
    NSInteger lapCount;
    NSArray *timeList;
}
@property (strong, nonatomic) IBOutlet UILabel *timeShower;
@property (strong, nonatomic) IBOutlet UIButton *startOrStop;
@property (strong, nonatomic) IBOutlet UIButton *resetOrLap;
@property (strong, nonatomic) UITableView *lapTable;
@end


@implementation firstfbAppViewController
@synthesize timeShower = _timeShower;
@synthesize startOrStop = _startOrStop;
@synthesize resetOrLap = _resetOrLap;
@synthesize lapTable = _lapTable;


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.timeShower setText: @"00:00.0"];
    [self.startOrStop setTitle:@"Start" forState:UIControlStateNormal];
    [self.resetOrLap setTitle:@"Reset" forState:UIControlStateNormal];
    
    CGRect screenRect = [[UIScreen mainScreen]bounds];
    CGRect lapTableRect = CGRectMake(screenRect.origin.x, screenRect.size.height/2 - 30, screenRect.size.width, screenRect.size.height/2 + 30);
    self.lapTable = [[UITableView alloc]initWithFrame:lapTableRect style:UITableViewStyleGrouped];
    
    [self.lapTable setDelegate:self];
    [self.lapTable setDataSource:self];
    [self.view addSubview:self.lapTable];
    
    
    start = NO;
    secCount = lapCount = 0;
    timer = [[NSTimer alloc]init];
    
}
- (IBAction)startOrStopClick
{
    start = !start;
    if (start)
    {
        [self.startOrStop setTitle:@"Stop" forState:UIControlStateNormal];
        [self.resetOrLap setTitle:@"Lap" forState:UIControlStateNormal];
        //timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; 就是这里需要注意,也可以使用这一行代码并用后面两句设置currentRunLoop的模式。
        timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
        NSRunLoop *t = [NSRunLoop currentRunLoop];
        [t addTimer:timer forMode:NSRunLoopCommonModes];//这里
    }
    else
    {
        [self.startOrStop setTitle:@"Start" forState:UIControlStateNormal];
        [self.resetOrLap setTitle:@"Reset" forState:UIControlStateNormal];
        [timer invalidate];
    }
}


- (IBAction)resetOrLap:(id)sender
{
    if (start)  //lap
    {
        if (timeList == nil)
        {
            timeList = [[NSMutableArray alloc]initWithObjects:timeStr, nil];
        }
        else
        {
            timeList = [timeList arrayByAddingObject:timeStr];
            //NSLog(@"timeListCount: %d",[timeList count]);
        }
        [self.lapTable reloadData];
    }
    else    //reset
    {
        timeStr = @"00:00.0";
        self.timeShower.text = timeStr;
        timeList = nil;
        secCount = 0;
        [self.lapTable reloadData];
    }
}


- (void)updateTime
{
    secCount += 0.1;
    timeStr = [NSString stringWithFormat:@"%02d:%04.1f",(int)(secCount/60),secCount - 60 * (int)(secCount/60)];
    self.timeShower.text = timeStr;
    NSLog(@"timeStr: %@",timeStr);
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return [timeList count];
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *tableViewIdentifier = @"tableViewIdentifier";
    UITableViewCell *timeCell = [tableView dequeueReusableCellWithIdentifier:tableViewIdentifier];
    if (timeCell == nil)
    {
        timeCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:tableViewIdentifier];
    }
    NSUInteger timeRow = [indexPath row];
    //NSLog(@"timeRow: %d",timeRow);
    
    timeCell.detailTextLabel.text = [timeList objectAtIndex:timeRow];
    timeCell.detailTextLabel.textColor = [UIColor blackColor];
    timeCell.detailTextLabel.font = [UIFont boldSystemFontOfSize:25.0];
    timeCell.detailTextLabel.textAlignment = UITextAlignmentCenter;
    
    NSString *text = [[NSString alloc]initWithFormat:@"lap %d",timeRow + 1];
    timeCell.textLabel.text = text;
    return timeCell;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值