Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-04-2012, 03:35 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 30
Anonymous Username is on a distinguished road
Default Help with timers

I want a timer to only work when I hold down the button, and stop counting when you let go. Im using two IBActions one touch up inside and one touch down. The touch up invalidates the timer and the touch down starts one, is there a better way?
Anonymous Username is offline   Reply With Quote
Old 02-04-2012, 03:52 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Anonymous Username View Post
I want a timer to only work when I hold down the button, and stop counting when you let go. Im using two IBActions one touch up inside and one touch down. The touch up invalidates the timer and the touch down starts one, is there a better way?
It depends on your needs. If you need the timer to start exactly at the moment the user presses down, then yes, creating a timer on touch down is the way to go.

If you can tolerate a little slop, you could have a repeating timer running all the time, with the code the timer calls turned on and off by a BOOL instance variable. Let's call it timerActive.

On touch down, you'd set timerActive to true. On touchUp, you'd set timerActive to false.

The code that decrements your counter and displays the result would be inside an if statement: ("If (timerActive) {decrement counter and display}")

That approach would cause the first interval to be short sometimes, since the timer will already be "in flight" when the user presses the button.

I'd say since you've already done the work to create a new timer on touch down, and kill it on touch up, keep that approach. It's more accurate, but a little more work to set up. The work is done, so why do more work to implement a less accurate method? Just make sure your logic will never try to invalidate an already invalidated timer. That will cause a crash. I'd suggest setting your timer's instance variable to nil when you invalidate it.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-04-2012, 06:43 PM   #3 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 30
Anonymous Username is on a distinguished road
Default

Thank You! I feel weird having three IBActions (the third one invalidates the timer if you leave the button) I was just making sure this is the best way to do this.
Anonymous Username is offline   Reply With Quote
Old 02-04-2012, 06:52 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Anonymous Username View Post
Thank You! I feel weird having three IBActions (the third one invalidates the timer if you leave the button) I was just making sure this is the best way to do this.

You could probably tie the touch up outside event to the same action as the touch up inside (stopping the timer.) So you have 3 events, but only 2 actions.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-04-2012, 07:09 PM   #5 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 30
Anonymous Username is on a distinguished road
Default

I never knew you could do that! Thank you!
Anonymous Username is offline   Reply With Quote
Old 02-04-2012, 10:10 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Anonymous Username View Post
I never knew you could do that! Thank you!
Absolutely. It's a very useful technique.

Say you're writing a quiz app, and the user has to pick button a, button b, button c, or button d for each question.

You can use a single IBAction for all the buttons, and put a different tag number on each one. The code IBAction can use the tag property of each button to figure out what button was pressed, and index into an array of questions to see if that button was the correct answer for the current question.

You can also set the target and selector on multiple gesture recognizers to the same method in much the same way, and use the tag of the view the gesture recognizer is linked to to figure out what to do.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 405
18 members and 387 guests
Apptronics RBC, Atatator, chiataytuday, dre, FrankWeller, gwelmarten, ipodphone, jeroenkeij, jleannex55, kukat, LunarMoon, MAMN84, n00b, pbart, reficul, Retouchable, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,124
Posts: 402,909
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:14 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0