Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Touch event and countdown timer

freshkingfreshking Posts: 124Registered Users
Hi all,

in my app I want to have a uilabel in my mainview which displays a countdown from a chosen number of seconds. This shouldnt be too hard. But now comes the clue. I only want the timer to start the countown when there are no touch events at all. So every time the user clicks a button, changes to a different view or does anything else the timer should start counting down from the begining again.

How would you do this?
How do I termine if there is a touch event anywhere in the app?
Do I use NSTimer?


Thanks for taking your time and making thoughts!
Post edited by freshking on

Replies

  • exorcyzeexorcyze Posts: 496Registered Users
    Just learning things myself, but hopefully this should point you in the right direction ( and someone can correct me if I'm way off base ).


    - (void)createTimer {
    // start timer
    myTimer = [[NSTimer timerWithTimeInterval:.04 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES] retain];
    [[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode];
    }

    #pragma mark Touches
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // touch started. [allTouches count] will tell how many touches if it matters
    NSSet *allTouches = [event allTouches];
    }
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSSet *allTouches = [event allTouches];
    }
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    // they released the touch ( stop timer? )
    [myTimer invalidate];
    }

    #pragma mark Timer
    - (void)timerFired:(NSTimer *)timer{
    // update label
    }


    We have a few different sections here. The first is where we create and start our timer object. The second section is to detect touches (start, move and end), and the last is for the callback on the timer firing.

    Hope that guides you in the right direction. =)
    My Apps on AppStore : gScale (guitar scales reference), <a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=302865690" target=
  • freshkingfreshking Posts: 124Registered Users
    Thank you! That really got me quite far.
    But how do I update the label constantly? I want the user to see the countdown.
    I guess I have to update the label every second. Just exactly how do you do that. Haven't found anything on that here.
    Thanks again!
  • mpramodjainmpramodjain Posts: 362Registered Users
    freshking;34242 said:
    Thank you! That really got me quite far.
    But how do I update the label constantly? I want the user to see the countdown.
    I guess I have to update the label every second. Just exactly how do you do that. Haven't found anything on that here.
    Thanks again!
    Hi,

    Though I am not an expert at IPHONE SDK,this may help..

    a) start running a timer to update the lable for every second(as u required).
    b) As and when any touch is detected, invalidate the timer and set the countdown buffer to required value.
    c) If u find touch is ended, again start the timer for displaying countdown and its update.

    May this help.

    Please reply ,if u find any helpful solution.

    Thank u.
Sign In or Register to comment.