Creating a StopWatch in Iphone

后端 未结 2 933
孤街浪徒
孤街浪徒 2020-12-16 06:31

I am trying to create a simple stopwatch. I referred to this website ( http://www.apptite.be/tutorial_ios_stopwatch.php) to do this app. When I click the start button, I get

相关标签:
2条回答
  • 2020-12-16 06:57

    I created a simple stopwatch with pause/resume capability. It uses two NSDate variables to keep track of time between pausing:

    NSTimeInterval secondsBetween = [pause_date timeIntervalSinceDate:start_date];
    start_date = [NSDate dateWithTimeIntervalSinceNow:(-1)*secondsBetween];
    

    Check out the example project here: https://github.com/kevinpk/NSStopwatch

    0 讨论(0)
  • 2020-12-16 07:00

    Solved the errors:

    ViewController.h

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController{
        UILabel *lbl;
        NSTimer *stopTimer;
        NSDate *startDate;
        BOOL running;
    }
    
    @property (strong,nonatomic) IBOutlet UILabel *lbl;
    
    -(IBAction)startPressed:(id)sender;
    -(IBAction)resetPressed:(id)sender;
    
    -(void)updateTimer;
    
    @end
    

    ViewController.m

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    @synthesize lbl;
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        lbl.text = @"00.00.00.000";
        running = FALSE;
        startDate = [NSDate date];
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    -(IBAction)startPressed:(id)sender{
        if(!running){
            running = TRUE;
            [sender setTitle:@"Stop" forState:UIControlStateNormal];
            if (stopTimer == nil) {
                stopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                             target:self
                                                           selector:@selector(updateTimer)
                                                           userInfo:nil
                                                            repeats:YES];
            }
        }else{
            running = FALSE;
            [sender setTitle:@"Start" forState:UIControlStateNormal];
            [stopTimer invalidate];
            stopTimer = nil;
        }
    
    }
    -(void)updateTimer{
        NSDate *currentDate = [NSDate date];
        NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
        NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"HH:mm:ss.SSS"];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
        NSString *timeString=[dateFormatter stringFromDate:timerDate];
        lbl.text = timeString;
    }
    
    -(IBAction)resetPressed:(id)sender{
        [stopTimer invalidate];
        stopTimer = nil;
        startDate = [NSDate date];
        lbl.text = @"00.00.00.000";
        running = FALSE;
    }
    
    @end
    

    Slight alterations to the code have been made.. But it is working correctly now...

    0 讨论(0)
提交回复
热议问题