你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> 一個iOS上的秒表小運用的完成辦法分享

一個iOS上的秒表小運用的完成辦法分享

編輯:IOS開發綜合

模擬完成一下IOS體系運用時鐘裡的秒表法式,就是這個運用:

2015102994851336.png (324×486)

重要完成的功效:
1.由start/stop鍵完成計時
2.有reset/lap鍵完成復位和計次

須要思慮的點:
1.時光的表現辦法(有許多種思緒)
2.計次數據的倒序分列,即計次1的數據在最底端,順次向上為計次2,計次3的時光數據

我的完成:
ARC省去了我們自行治理內存的年夜部門工作,寫慣了c++因而舒暢了許多


- (IBAction) startOrstop:(UIButton *)sender 

    //點擊切換按鈕配景圖 
    UIImage *newImage = (checked) ? [UIImage imageNamed:@"red.png"] : [UIImage imageNamed:@"green.png"]; 
    [leftBtn setBackgroundImage:newImage forState:UIControlStateNormal]; 
     
    NSString *titlel = (checked) ? (@"Stop") : (@"Start"); 
    [leftBtn setTitle:titlel forState:UIControlStateNormal]; 
    NSString *titler = (checked) ? (@"Lap") : (@"Reset"); 
    [rightBtn setTitle:titler forState:UIControlStateNormal]; 
   
       
    if (checked)   //start 
    { 
        timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; 
    }else {        //stop 
        [timer invalidate]; 
    } 
     
    checked = !checked; 

 
- (IBAction) resetOrLap:(UIButton *)sender 

    static NSInteger count = 1; 
     
    if (checked) //reset 
    { 
        time = time_lap = 0.0; 
        timestr = [NSString stringWithFormat:@"00:00.0"]; 
        [label setText:timestr]; 
        list_time = list_lap = nil; 
        count = 1; 
        [tableview reloadData]; 
         
    }else {      //lap 
        if (list_time == nil) { 
            list_time = [[NSArray alloc]initWithObjects:timestr_lap, nil]; 
            list_lap = [[NSArray alloc]initWithObjects:[NSString stringWithFormat:@"%d",count++], nil]; 
        }else { 
#if 0 
            [list arrayByAddingObject:timestr]; 
#else 
            NSArray *array = [[NSArray alloc]initWithObjects:timestr_lap, nil]; 
            list_time = [array arrayByAddingObjectsFromArray:list_time]; 
            array = [[NSArray alloc]initWithObjects:[NSString stringWithFormat:@"%d",count++], nil]; 
            list_lap = [array arrayByAddingObjectsFromArray:list_lap]; 
#endif 
        } 
        time_lap = 0; 
        [tableview reloadData]; 
     } 

 
- (float) updateTime 

    time+=0.1; 
    time_lap +=0.1; 
    timestr = [NSString stringWithFormat:@"%02d:%04.1f",(int)(time / 60) ,time - ( 60 * (int)( time / 60 ) )]; 
    timestr_lap = [NSString stringWithFormat:@"%02d:%04.1f",(int)(time_lap / 60) ,time_lap - ( 60 * (int)( time_lap / 60 ) )]; 
    [label setText:timestr]; 
    return time; 

 
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

    return [list_time count]; 

 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

    static NSString *tableViewIdentifier = @"tableViewIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewIdentifier]; 
    if (cell == nil) { 
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:tableViewIdentifier];  
    } 
     
    NSUInteger row = [indexPath row]; 
     
    cell.detailTextLabel.text = [list_time objectAtIndex:row]; 
    cell.detailTextLabel.textColor = [UIColor blackColor]; 
    cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:25.0]; 
    cell.detailTextLabel.textAlignment = UITextAlignmentCenter; 
     
    NSString *text = [[NSString alloc]initWithFormat:@"lap %@", [list_lap objectAtIndex:row]]; 
    cell.textLabel.text = text; 
    return cell; 

 

2015102994921137.png (320×480)

2015102994939154.png (320×480)

待改良的處所:
1.關於時光的計時操作和UI事宜應當分分歧線程完成,這裡我偷懶了
2.關於時光的表現辦法其實也是很偷懶的,沒有依照尺度的秒分進位表現

【一個iOS上的秒表小運用的完成辦法分享】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved