Time between 2 events

I haven't been programming in iOS for long but I was just wondering if anyone could help. I have a IBAction function and every time it is pressed increases a counter so I can see how many times it's been pressed. But I want to add functionality so when it's pressed I can display the time between each press. So if they press the button a one pops up. Then they press it again and 2 button presses pops up but also the amount of time since they pressed it. I'm not sure how to implement this because I'm not sure how I would find the time of the event. There is UIEvent's timestamp, but I'm not entirely sure how to use it.

Answers


Unless you need extreme accuracy it's probably enough to get the current time when your IBAction method is called in which case you could do something like this:

- (IBAction) buttonAction: (id) inButton
{
    NSDate              *now = [NSDate date];

    if (self.lastEventTime != nil) {
        NSTimeInterval      timeSinceLast = [now timeIntervalSinceDate: self.lastEventTime];

        NSLog(@"time since last press: %f seconds", timeSinceLast);
    }

    self.lastEventTime = now;
}

Here's how that might look in Swift:

class SomeController: UIViewController {
    var lastEventTime                   : NSDate?

    @IBAction func buttonAction(inButton: AnyObject) {
        let now = NSDate()

        if let lastEventTime = self.lastEventTime {
            let timeSinceLast = now.timeIntervalSinceDate(lastEventTime)

            println("time since last press: \(timeSinceLast) seconds")
        }

        self.lastEventTime = now
    }
}

Need Your Help

Rotating CSS cube

css css3 css-transforms

I want to create a CSS cube that has 4 sides (front, back, top, bottom) and it rotates up (or down) constantly only on X axis. But for some reason I can't align all the 4 sides so it looks like a c...

Read from stdin write to stdout in C

c io stdout stdin stdio

I am trying to write a cat clone to exercise C, I have this code:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.