I'm trying to develop a new application for iOS but I'm stuck with NSTimer :-(. Currently I have a void function which hides an UIButton after the user taps twice or more on the screen and a NSTimer. I want to make the button visible again after the NSTimer reaches 5 seconds and stop and reset it right after in order to re-hide it after the user taps twice again :-) but I don't know how to tell the app that 5 seconds have passed :-/.
Can someone help me, please? :-)
Here's the code so far :-)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSUInteger numTaps = [[touches anyObject] tapCount];
if (numTaps >= 2)
{
// Other code//
[self.button setHidden:YES];
buttonHideTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 invocation:nil repeats:NO];
// Other code //
}
}
Thanks!
The timer needs to have a selector, which will tell the timer what to do after the time has passed:
buttonHideTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(showButtonNow) userInfo:nil repeats:NO];
Related
I am working on app in which i have to hide control button after 3 second so i write code using NSTimer and Touch began and it work but problem is that when i touch again with on any another button than my timer is not reset and even if i move my touch example like drag.
If i drag or move touch it should reset the timer but it won't.
I found that this implementation work if i continuously touch on other area (but it is not working on Control button) IF i continuously touch control button still it goes hidden after 3 second.How to solve this problem. i want event to fire on button click also.
EDITED I solve my problem by own..
I have added this code on button click sector and it works..
Thank you for all support
if(screenTimer)
{
[screenTimer invalidate];
screenTimer = nil;
screenTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(turnOffScreen) userInfo:nil repeats:NO];
}
Here is my code
// Touch began for touch event.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(screenTimer)
{
[screenTimer invalidate];
screenTimer = nil;
}
mode1View.hidden=NO;
screenTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(turnOffScreen) userInfo:nil repeats:NO];
}
- (void)turnOffScreen{
NSLog(#"TURN OFF SCREEN");
if(screenTimer!=nil)
{
mode1View.hidden=YES;
}
}
Any help is appreciated. Thank you
have you tried
[self performSelector:#selector(turnOffScreen:) withObject:nil afterDelay:3.0];
The NSObject method performSelector:withObject:afterDelay: it invoke a methods on the object with an objec after a certain delay.
I am creating one photo capture app in which user can take up to 20 photo in one second.
I have already try the AVCam Sample code of apple but it is don't allow to take still Image at 20 FPS.
Is there any other way to take photo at this rate.
see this Demo..Photo Picker
in this demo just check or refer these two classes..
MyViewController.m and
OverlayViewController.m
in OverlayViewController.m class in timedTakePhoto method just set timer with your requirement like bellow..
- (IBAction)timedTakePhoto:(id)sender
{
// these controls can't be used until the photo has been taken
self.cancelButton.enabled = NO;
self.takePictureButton.enabled = NO;
self.timedButton.enabled = NO;
self.startStopButton.enabled = NO;
if (self.cameraTimer != nil)
[self.cameraTimer invalidate];
_cameraTimer = [NSTimer scheduledTimerWithTimeInterval:0.03
target:self
selector:#selector(timedPhotoFire:)
userInfo:[NSNumber numberWithInt:kOneShot]
repeats:YES];
// set time with your requirement above
// start the timer to sound off a tick every 1 second (sound effect before a timed picture is taken)
if (self.tickTimer != nil)
[self.tickTimer invalidate];
_tickTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(tickFire:)
userInfo:nil
repeats:YES];
}
may you can get idea from this and also use this demo for your requirement..
i hope this help you...
I'm trying to develop a game and running into a small issue with NSTimer, once a sprite appear it has a certain amount on time in my scene before fading out.
double CALC_TIME = 5;
[NSTimer scheduledTimerWithTimeInterval:CALC_TIME target:self selector:#selector(hideSprite) userInfo:nil repeats:NO];
I want hideSprite to be called after 5 seconds, but instead it's called instantly(Or near instant).
A possible solution:
I know I could do this by setting the timer to repeat, and having a bool firstCall that is set first time and then the next interval the fading is done, the timer is invalidated but I don't think this is good practice
Like this:
bool firstCall = false;
-(void)hideSprite{
if(!firstCall){
firstCall = true
}else{
//fade out sprite
//Invalidate NSTimer
//firstCall = false;
}
}
Thanks for your help!
I suspect something else is calling hideSprite. The code you have written will cause the timer to wait for five seconds before calling the selector hideSprite.
Provide a different selector (write a new test method which just does an NSLog) to the timer and see what happens. This will tell you a) whether the timer is indeed immediately firing and b) if something else is calling hideSprite.
[NSTimer scheduledTimerWithTimeInterval:CALC_TIME target:self selector:#selector(testTimer) userInfo:nil repeats:NO];
-(void) testTimer { NSLog(#"Timer - and only the timer - called me."); }
-(void) hideSprite {
NSLog(#"I definitely wasn't called by the timer.");
}
Quite often it's easier to just use something like
[UIView animateWithDuration: 0 delay: 5 options:0 animations: nil completion:
^{
// fade sprite
}];
(not sure if animations can be nil, but you get the idea).
Consider initializing your timer using the initWithFireDate:interval:target:selector:userInfo:repeats method. Here is a more detailed look at NSTimer.
I have a variable called touchStatus that tracks the touch status in the program. The variable gets set to B in the touchesBegan method, E in touchesEnded and to M in touchesMoved.
However, my requirements are a little different. I was requested to program in a way so that there is one second of delay between the finger being lift off from the screen and touchStatus getting set to E. If the user touches the screen before the one second elapses, touchStatus should continue to be M or B (whatever it was before the one second).
How can I accomplish this?
You can use
[self performSelector:#selector(setEndedValue:) withObject:self afterDelay:1.0];
Create a BOOL to monitor whether the value should be set such as:
BOOL hasTouchRestarted = NO;
If the screen is touched again before the value is set, change the value to YES and return from the setEndedValue method.
-(void)setEndedValue {
if ( hasTouchRestarted ) { return; }
// set value
self.touchStatus = E;
}
In the touchEnded routine set up an NSTimer task to invoke a selector in one second. If there is another touch before then, cancel the timer task.
Use a NSTimer *timer ivar to initiate a delayed call, and cancel the call if the user lifts the finger before one second.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.myvar = #"B";
[self.timer invalidate];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(handleOneSecondPress) userInfo:nil repeats:NO];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event {
[self.timer invalidate];
self.timer = nil;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
self.myvar = #"M";
}
- (void)handleOneSecondPress {
self.timer = nil;
self.myvar = #"E";
}
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How can I programmatically pause an NSTimer?
I have a question. How can I pause a countdown using a timer? I am developing a game. In the game, I need to go to next view when the timer pauses, and after coming back I want to resume it.
I try this code in the view:
[mytimer pause];
// to resume
[mytimer resume];
I try that code, but I get a warning saying: "NSTimer may not respond to 'pause'"
I build with that warning and when I press the pause button, the app crashes.
NSTimer indeed doesn't have resume and pause methods so you can end up with a crash in runtime after such a warning. Generally you can create 2 kinds of timers (see NSTimer class reference) one that implements only once and the second, that repeats. Example:
This way you create a timer, that will enter callback myMethod each second.
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(myMethod:)
userInfo:nil
repeats:YES];
You probably will choose this one for your purpose where in your class you should maintain some
BOOL pausevariable and in the callback myMethod do the following:
- (void) myMethod:(NSTimer *) aTimer
{
if (!pause) {
// do something
// update your GUI
}
}
where you update pause accordingly somewhere in your code.
To stop the timer (and release it) call
[myTimer invalidate];
good luck
What you want, is what OpenGLES application brings up to you. You should create 2 methods like this:
- (void)startAnimation
{
self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:#selector(selector) userInfo:nil repeats:YES];
}
- (void)stopAnimation
{
[animationTimer invalidate];
animationTimer = nil;
}
It's something like this.
Refer to the NSTimer Class Reference, there is no pause method.