Calling a method after a delay? - cocoa-touch

How can I call a method after a short delay? (For example, after resignFirstResponder is called.)

u can call method after delay using perform select: like
[self performSelector:#selector(keyboardShow) withObject:nil afterDelay:0.5];

You can use either timer like this
timer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:#selector(taskOnTimer) userInfo:nil repeats:NO];
OR use this
[self performSelector:#selector(playPages) withObject:nil afterDelay:10];

Related

Fire IBAction automatically

I have a button and want to fire it automatically by itself without touch.
Is it possible?
-(IBAction)xxx:(id)sender
My answer assumes you have the method:
- (IBAction)someAction:(UIButton *)sender {
}
and that you have a reference to the button in an instance variable named someButton.
If you just need to "fire it" now, simply call it:
[self someAction:someButton];
If you need to "fire it" once, but later, you can do:
// call it 5 seconds from now
[self performSelector:#selector(someAction:) withObject:someButton afterDelay:5.0];
If you want to fire it repeatedly, use a timer:
myTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(buttonTimerFired) userInfo:nil repeats:YES];
- (void)buttonTimerFired {
[self someAction:someButton];
}
Action can be called like every regular function - you can do it by running a timer on something else.
You should use NSTimer for doing your work.
[NSTimer scheduledTimerWithTimeInterval: 0.01f target: self selector: #selector(BtoonMethod) userInfo: nil repeats: NO];
-(void)BtoonMethod
{
// write code for call yor button method
}

How to stop NSTimer started on button click in this timer callback method

I am having problems with stoping NSTimer started on button click. [timer invalidate] and timer = nil; just do nothing neither when I am trying to stop it viewWillDisappear nor in method invoked by method which is being invoked by this timer. However when I start my timer in viewWillAppear and invalidate it in viewWillDisappear everything is fine.
I suppose there might be an issue in thread I am starting the timer from. Can you help with that?
I looked through all answers here regarding NSTimer not stopping, but they didn't help to solve the problem.
The way I initialize my timer:
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(oneSecondPassedSinceRoundStarted) userInfo:nil repeats:YES];
The ways I tried to stop it:
[self.timer invalidate];
[timer invalidate];
It's funny how quick you can answer your own question after you asked for a help. No matter how long you have been struggling to find the answer before by yourself:
[self performSelectorOnMainThread:#selector(stopTimer) withObject:nil waitUntilDone:YES];
And the selector:
- (void) stopTimer
{
[timer invalidate];
timer = nil;
}
I have created and tested this:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(oneSecondPassedSinceRoundStarted:) userInfo:nil repeats:YES];
}
-(void)oneSecondPassedSinceRoundStarted:(NSTimer *)time {
// Do what You want
NSLog(#"CALLING!");
}
-(IBAction)buttonAction:(id)sender {
NSLog(#"Stop timer!");
[myTimer invalidate];
myTimer = nil;
}
And it is working nice.
Note: You forget to add colon after oneSecondPassedSinceRoundStarted.

Staggering NSTimer Fires

I have four NSTimers that work fine. The only problem is that two should fire and then the other two should fire in the middle of the first two. Here is my code:
initTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector: #selector(initText) userInfo:nil repeats:YES];
animateTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector: #selector(textGlow) userInfo:nil repeats:YES];
initTimer1 = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector: #selector(initText1) userInfo:nil repeats:YES];
animateTimer1 = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector: #selector(textGlow1) userInfo:nil repeats:YES];
The initText and initText1 methods create a UILabel with lazy instantiation and place them on the screen on the same spot. The textGlow and textGlow1 methods animate the text to make it grow bigger and fade away. I want initText1 and textGlow1 to fire in the middle of initText and textGlow. For example, let's say each of these methods takes 2 seconds (I set the animation duration to 2 seconds). I want initText and textGlow to fire, and then one second in, initText1 and textGlow1 to fire.
Thanks. If you need any of my other code to help answer the question, just ask.
----------EDIT-------------
#MDT That worked but there is a problem. What I did was I placed the code above of initTimer1 and animateTimer1 in new functions called initTimer1Wrapper and animateTimer1Wrapper. Then I did your code [self performSelector:#selector(initText1Wrapper) withObject:nil afterDelay:1.0]; and duplicated it but changed the selector to animateText1Wrapper. What happens is initText and textGlow will fire as planned, and then also as planned initTimer1 and animateTimer1 will fire one second in, but what also happens unexpectedly is that initText and textGlow do not finish their remaining one second; the animations just stop. I believe this is because they are UIView animations with [UIView beginAnimation:#"name" context:nil] and [UIView commitAnimations] and more then 1 UIView animations can't run at the same time.
To fix this, should I use Core Animation (which I do not know anything about) or is there another solution? Thanks.
----------EDIT2-------------
Also, just if it is important for you to know, I will dismiss this animation with a UIGestureRecognizer.
Something like:
if(screenIsTapped){
[initTimer invalidate];
[animateTimer invalidate];
[initTimer1 invalidate];
[animateTimer1 invalidate];
}
Try placing something like this inside initText or textGlow.
[self performSelector:#selector(initText1) withObject:nil afterDelay:1.0];
I think you should do this with 2 timers instead of 4. Inside the methods, initText and initText1 just call textGlow and textGlow1, respectively, using performSelector:withObject:afterDelay: with whatever delay you want. And, do you really want these timers to be repeating? Do you want to keep creating new UILabels?

Cocoa Touch - Timers

How can I make a timer that counts down from 3 and then runs a method? How would I do that?
Is that different from a timer counting from 0 to 3? It will still wait three seconds, either way.
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(myMethod:) userInfo:nil repeats:NO];
Better way might be to use performSelector:withObject:afterDelay: method:
[self performSelector:#selector(myMethod) withObject:nil afterDelay:3.0f];
Or in case method takes 1 parameter:
[self performSelector:#selector(myMethod:) withObject:parameter afterDelay:3.0f];
If method takes multiple parameters you'll need to use NSInvocation class
- (void) handleTimer: (NSTimer *) timer
{
do some work here...
} // handleTimer
// at some point in your controller
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 3.0
target: self
selector: #selector(handleTimer:)
userInfo: nil
repeats: NO];

Delayed call, with possibility of cancelation?

How do I trigger a delay, let's say I want to call a method (once) in 3 seconds from now, and how do I cancel that call if I need to?
You can also use -[NSObject performSelector:awithObject:afterDelay:], and +[NSObject cancelPreviousPerformRequestsWithTarget:selector:object].
Use NSTimer. Use this to set up a call to method in three seconds time. It will only be called once:
[NSTimer scheduledTimerWithTimeInterval: 3
target: self
selector: #selector(method:)
userInfo: nil
repeats: NO];
method needs to look like this:
- (void) method: (NSTimer*) theTimer;
You can pass parameters into the method using userInfo (set to nil in the above example). It can be accessed in the method as [theTimer userInfo].
Use the invalidate method on NSTimer to cancel it.
in your header..
NSTimer *timer;
when you want to setup..
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(yourMethod:) userInfo:nil repeats:NO];
when you want to cancel..
[timer invalidate];