如何在Stop Watch iPhone应用程序中实现圈速 [英] How to implment laps in Stop Watch iphone app

查看:47
本文介绍了如何在Stop Watch iPhone应用程序中实现圈速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个具有秒表作为其辅助功能的应用程序.我正在尝试做与iOS秒表非常相似的事情.我停止了,重置功能正常工作,但是我无法启动(一旦停止并且用户单击开始),并且单圈功能正常工作.我试图解决这个问题,但是除了一次启动和重置秒表教程之外,我什么也找不到.有人可以在这里帮我吗?

I am building one application which has stop watch as its accessory function. I am trying to do something very similar to iOS stop watch. I got stop, reset functionality working but I could not get start (once it stop and user click on start) and lap functions working properly. I tried to look around for this problem but I could not find anything besides one time start and reset stop watch tutorials.Here is what I have right now. Can anyone help me out here with this functions ?

// Stop Watch Code Begins

- (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];
stopWatchLable.text = timeString;
[dateFormatter release];

}

 -(IBAction)startTapped:(id)sender
{
if ([self.start.titleLabel.text isEqualToString:@"Start"])
{
    NSLog(@"Start Tapped");
    [self.start setTitle:@"Stop" forState:UIControlStateNormal];
    [self.reset setTitle:@"Lap" forState:UIControlStateNormal];

    self.startDate =[NSDate date];
    stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                      target:self
                                                    selector:@selector(updateTimer)
                                                    userInfo:nil
                                                     repeats:YES];

 }
else if ([self.start.titleLabel.text isEqualToString:@"Stop"])
{
    NSLog(@"Stop Tapped");
    [self.start setTitle:@"Start" forState:UIControlStateNormal];
    [self.reset setTitle:@"Reset" forState:UIControlStateNormal];

    [stopWatchTimer invalidate];
    stopWatchTimer = nil;
    [self updateTimer]; 
}
}
-(IBAction)resetTapped:(id)sender
{   
if ([self.reset.titleLabel.text isEqualToString:@"Reset"])
{
    NSLog(@"Reset Tapped");
    [stopWatchTimer invalidate];
    stopWatchTimer = nil;
    [stopWatchLable setText:@"00:00:00.000"];
}
else if ([self.reset.titleLabel.text isEqualToString:@"Lap"])
{
    NSLog(@"Lap Tapped");        
}
}
-(IBAction)hideTapped:(id)sender
{
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGRect endFrame = self.stopWatchView.frame;
endFrame.origin.y = screenRect.origin.y + screenRect.size.height;

// start the slide down animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];

// we need to perform some post operations after the animation is complete
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(slideDownDidStop)];

self.stopWatchView.frame = endFrame;
[UIView commitAnimations];

// grow the table back again in vertical size to make room for the date picker
CGRect newFrame = self.tableView.frame;
newFrame.size.height += self.stopWatchView.frame.size.height;
self.tableView.frame = newFrame;
//self.tableView.frame = screenRect;

[stopWatchTimer invalidate];
stopWatchTimer = 0;
[stopWatchLable setText:@"00:00:00.000"];
[self updateTimer];
}

推荐答案

不要采取这种错误的方式,但是您的代码中有一个错误的设计选择:您不应该依赖按钮文本来告诉您什么想做的事情–如果将来某个时候对应用程序进行本地化,则必须重做计时器.

Don't take this the wrong way, but you have one bad design choice in your code: You should not rely on a button text to tell you what you want to do – if at some point in the future you localize your application you'll have to redo the timer.

也就是说,这是我如何实现秒表的方法:您需要两个实例变量, NSTimer timer NSDate startDate .对于圈功能,您将需要另一个功能,即 NSMutableArray .您的两个按钮都很好,您可以使用简单的逻辑来确定点击时的操作(未经测试的代码!):

That said, here's how I would implement a stopwatch: You need two instance variables, NSTimer timer and NSDate startDate. For a lap function you'll need another one, an NSMutableArray laps. Your two buttons are fine, using simple logic you can decide what you have to do when tapping (untested code!):

if (!timer) {
    self.startDate = [NSDate date];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 / 10.0
                                                  target:self
                                                selector:@selector(updateTimer)
                                                userInfo:nil
                                                 repeats:YES];
    [self updateTimer];
    // set button text to STOP and LAP
}

// there already is a timer, stopwatch running, so we want to stop
else {
    if ([timer isValid]) {
        [timer invalidate];
    }
    self.timer = nil;
    [self updateTimer];
    // set button text to START and RESET
}

重置/轻按按钮

// if we have a timer, we want to take a lap
if (timer) {
    if (!laps) {
        self.laps = [NSMutableArray array];
    }
    [laps addObject:[NSDate date]];
    [self updateTimer];
}

// there is no timer, so we want to reset
else {
    self.startDate = nil;
    [self updateTimer];

    // delete all laps
    self.laps = nil;
}

现在,您只需修改 updateTimer 即可执行您想要的操作.在updateTimer中,您还应该在计算任何内容之前检查 startDate 是否为nil.对于圈数,您可以循环遍历圈数数组,并计算 startDate 与数组中日期之间的差,甚至可以计算数组中日期之间的差.

Now you only have to modify updateTimer to do what you want it to do. In updateTimer, you should also check whether startDate is nil or not before you calculate anything. For the laps you can just loop over the laps array and calculate the difference between startDate and the date in the array, or even between the dates in the array.

我希望这会有所帮助!

这篇关于如何在Stop Watch iPhone应用程序中实现圈速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆