Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

uaecasher

macrumors 65816
Original poster
Jan 29, 2009
1,289
0
Stillwater, OK
hello,

I made an NSTimer to start when a user taps, my problem is that if the user taps again (after the NSTimer started) another timer will be lunched and the time displayed will be moving crazy fast, how to fix that?

Thanks

Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
	UITouch *touch = [touches anyObject]; 
		if(timerBOOL==TRUE)
	{
		[timer invalidate];
		timerBOOL = FALSE;
	} else { 
		timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
				 timerBOOL = TRUE;
				 }


}

-(void)countUp {
	mainint += 1;
	myTextFeild.text = [NSString stringWithFormat:@"%i secs", mainint];
	
}
 
Check whether the timer has already begun before starting another?

sorry, my problem is that when I did the check above:

Code:
	if(timerBOOL==TRUE)
	{
		[timer invalidate];
		timerBOOL = FALSE;
	} else { 
		timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
				 timerBOOL = TRUE;
				 }

the timer would lag for about 4 seconds then continue each time a user taps
 
I'm want to show different images depending on the number of taps (I figured how to do that, the only problem is the timer lags
 
OK, I'm struggling to understand how this fits with running the countUp method at each tap?

I'm also curious regarding where you actually define the timer, and why this is not done inside the touchesBegan method?
 
Try this:
Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
	UITouch *touch = [touches anyObject]; 
		if(timerBOOL==TRUE)
	{
		[timer invalidate];
		timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
	} else { 
		timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
				 timerBOOL = TRUE;
				 }


}
 
Try this:
Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
	UITouch *touch = [touches anyObject]; 
		if(timerBOOL==TRUE)
	{
		[timer invalidate];
		timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
	} else { 
		timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
				 timerBOOL = TRUE;
				 }


}
If you're just going to set timer the same way in both the if and else, in this case, it can be moved outside that logic.

Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
        if(timerBOOL==TRUE)
        {
            [timer invalidate];
        } else { 
             timerBOOL = TRUE;
        }
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
}

Also, uaecasher, realize that normally Objective-C BOOL values are expressed as YES/NO, not TRUE/FALSE (although that still seems to work).

I'm want to show different images depending on the number of taps...
And how does the NSTimer solve that problem? Wouldn't you rather use
Code:
[touches count]
instead?
 
If you're going to post efficiency suggestions you might as well go all the way

Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    [timer invalidate];
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
}

If you invalidate the timer anywhere else set the timer ivar to nil. The timer ivar is its own BOOL.

The usual rule for managing timers is that a timer ivar always has one of two values: a valid timer, or nil. If the timer is invalidated then set the timer ivar to nil.

OP, the reason for the lag is that you didn't restart the timer in the case where one already existed.
 
If you're going to post efficiency suggestions you might as well go all the way

Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    [timer invalidate];
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
}

If you invalidate the timer anywhere else set the timer ivar to nil. The timer ivar is its own BOOL.

The usual rule for managing timers is that a timer ivar always has one of two values: a valid timer, or nil. If the timer is invalidated then set the timer ivar to nil.

OP, the reason for the lag is that you didn't restart the timer in the case where one already existed.

I tired your code and same problems occurs.
Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
	UITouch *touch = [touches anyObject];
	[timer invalidate];
	NSUInteger tapCount = [touch tapCount];
	taps = tapCount;
	timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
	
	
}

@Dejo: I'm using tapcount already, the reason I'm using NSTimer as well is that I also what to know how much time did it take the user to get to the correct answer (depending on number of taps), in other words I want to control how long does it take the user to answer.

Thanks
 
I also what to know how much time did it take the user to get to the correct answer (depending on number of taps), in other words I want to control how long does it take the user to answer.
Have a look at NSDate and NSTimeInterval.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.