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.

Use of NSTimer

bleasdellbleasdell Posts: 17Registered Users
I have a situation (perhaps unique) that is causing me grief and I am hoping that someone can help me to get this straightened out.

Let me just jump right in with the code:

-(void)loadView {
[self StartFadeItems];
}

-(void)StartFadeItems {
timerFade = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(FadeItems) userInfo:nil repeats:NO];
}

-(void)ResetFadeItems {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[infoButton setAlpha:dimmerAlpha];
[mySwitch setAlpha:dimmerAlpha];
[myLabel setAlpha:dimmerAlpha];
[UIView commitAnimations];
[self StartFadeItems];
}

-(void)FadeItems {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[infoButton setAlpha:0];
[mySwitch setAlpha:0];
[myLabel setAlpha:0];
[UIView commitAnimations];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self ResetFadeItems];
}


So the jist is that when the user touches the screen these three items will fade in, and after 5 seconds, the items will fade out. The problem is that I want the behavior to be such that the timer is invalidated if the user touches the screen more than once in the 5 second period and have the timer reset. In other words:

0:00 - app launched
0:05 - items fade out
0:10 - user touches screen, items fade in
0:15 - items fade out
0:20 - user touches screen, items fade in
0:22 - user touches screen again, items remain visible
0:27 - items fade out

I have tried putting in a [timerFade invalidate]; in the ResetFadeItems method, but that causes a "BAD_EXC_EXCEPTION" exception. I have tried wrapping the invalidate message inside an "if (timerFade != nil) {" statement, but still get the same exception. I have also done a "timerFade = nil;" after the invalidate, to no avail.

Thanks in advance for your help.
Post edited by bleasdell on

Replies

  • detzdetz Posts: 394Registered Users
    How about just resetting the timer when there is a click...

    			 [fadeTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:5]];
  • bleasdellbleasdell Posts: 17Registered Users
    First, thanks for the quick reply.

    Second, I am having trouble fitting what you suggested into my code. I am sort of new to this so please bear with me. I can use setFireDate but I still have to init the timer object so that it knows what selector to trigger, correct? If I do that I get a crash when I call setFireDate. Here is the code:


    -(void)loadView {
    [self StartFadeItems];
    }

    -(void)StartFadeItems {
    if ([timerFade isValid]){
    [timerFade setFireDate:[NSDate dateWithTimeIntervalSinceNow:5]];
    }else{
    [timerFade release];
    timerFade = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(FadeItems) userInfo:nil repeats:NO];
    }
    }

    -(void)ResetFadeItems {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];
    [infoButton setAlpha:dimmerAlpha];
    [mySwitch setAlpha:dimmerAlpha];
    [myLabel setAlpha:dimmerAlpha];
    [UIView commitAnimations];
    [timerFade setFireDate:[NSDate dateWithTimeIntervalSinceNow:5]];
    [self StartFadeItems];
    }

    -(void)FadeItems {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];
    [mySwitch setAlpha:0];
    [infoButton setAlpha:0];
    [myLabel setAlpha:0];
    [UIView commitAnimations];
    }

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self ResetFadeItems];
    }


    What am I missing? I am guessing it is something simple, so be gentle.
  • detzdetz Posts: 394Registered Users
    Change StartFadeItems to

    if (timerFade){
    [timerFade setFireDate:[NSDate dateWithTimeIntervalSinceNow:5]];
    }else{
    timerFade = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(FadeItems) userInfo:nil repeats:NO];
    }



    In the function FadeItems ad

    timerFade = nil;
    [timerFade invalidate];
    [timerFade release];



    If you get crashes add some break points and step through your program to find the line that is crashing.
  • bleasdellbleasdell Posts: 17Registered Users
    detz,

    Thank you VERY much for your help. The code you offered wasn't 100% perfect, but it led me to the solution. Here is the solution as I found it:


    -(void)loadView {
    [self StartFadeItems];
    }

    -(void)StartFadeItems {
    timerFade = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(FadeItems) userInfo:nil repeats:NO];
    }

    -(void)ResetFadeItems {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];
    [infoButton setAlpha:dimmerAlpha];
    [mySwitch setAlpha:dimmerAlpha];
    [myLabel setAlpha:dimmerAlpha];
    [UIView commitAnimations];
    if ([timerFade isValid]) {
    [timerFade setFireDate:[NSDate dateWithTimeIntervalSinceNow:5]];
    }else{
    [self StartFadeItems];
    }
    }

    -(void)FadeItems {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];
    [mySwitch setAlpha:0];
    [infoButton setAlpha:0];
    [myLabel setAlpha:0];
    [UIView commitAnimations];
    timerFade = nil;
    [timerFade invalidate];
    [timerFade release];
    }

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self ResetFadeItems];
    }


    Again, thank you very, very much for helping me out.
Sign In or Register to comment.