Stopping an UIView animation that was started but being delayed [duplicate] - ios

This question already has answers here:
How to cancel UIView block-based animation?
(4 answers)
Closed 8 years ago.
[UIView animateWithDuration:1 delay:4 options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionCurveEaseIn)
animations:^
{
// do something here (irrelevant)
}
completion:nil
];
How do I stop the animation within the time of the delay? For example, I want to stop this animation after I execute it but before the delay timer ends. I've tried [self.view.layer removeAllAnimations]; but to no avail.
EDIT: As I've stated. I've tried [self.view.layer removeAllAnimations]; but to no avail.

I didn't try it but you can stop all animations with [view.layer removeAllAnimations]; it should work.

You could use NSTimer and then just call [timer invalidate] if you no longer want the animation to run.
E.g.:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:#selector(animateView:) userInfo:nil repeats:NO];
// Then stop...
[timer invalidate];

Related

CATransaction Completionblock triggers immediately [duplicate]

This question already has answers here:
CATransaction completion being called immediately
(4 answers)
Closed 8 years ago.
I'm trying to wait for an animation to finish before starting another task. I looked at different methods but using CATransactions seems to be the most used method to do this.
Somehow, my CATransaction Completionblock triggers immediately after the animation starts, not after it finishes.
Here's my code:
[CATransaction begin];
[CATransaction setCompletionBlock: ^{
NSLog(#"Animation ends");
}];
NSLog(#"Animation begins");
[tableView setEditing:NO animated:YES];
[CATransaction commit];
When looking at the console I get this:
2014-03-17 15:44:12.995 BarTap[89934:70b] Animation begins
2014-03-17 15:44:12.997 BarTap[89934:70b] Animation ends
So appearently the Completionblock starts 0.002 seconds after the animation begins, but the animation definitely takes longer than that.
Could anyone help me? Thanks!
Animation completion triggers immediately because there are no tasks for animation in your code.
CAAnimation effects to CALayer properties and it finishes immediately if there no changes in animatable properties.
Try this:
[UIView animateWithDuration:timeInterval animations:^{
[tableView setEditing:NO animated:NO];
} completion:^(BOOL finished) {
// Perform tasks after animation completion here
}];

iOS - Do an action every second [duplicate]

This question already has answers here:
Update a label with speed every x seconds
(2 answers)
Closed 9 years ago.
I want to make an action when every second is passed, for example I got a label and I want it every second to do this :
theLabel.text = #"Hey";
So, how I'll do it?
I think I'll need to use the NSTimer right?
Thanks in Advance
Just use a NSTimer:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(doSomething:) userInfo:nil repeats: YES];
And to stop it:
[timer invalidate];
Find the documentation here:
NSTimer Doc
A "plan B" solution:
[self performSelector:#selector(foo) withObject:nil afterDelay:{some time interval}];
-(void)foo
{
//do something..
[self performSelector:#selector(foo) withObject:nil afterDelay:{some time interval}];
}
Yes #kevin you are right NSTimer is the best
just initialize your timer on did load
NSTimer *timerCounter = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(doMyWork) userInfo:nil repeats: YES]; //make repeats YES
and do the below
-(void)doMyWork
{
theLabel.text = #"Hey";
// Or what ever you want to do
}

Tick timer in Objective C / iPhone? [duplicate]

This question already has answers here:
How do I use NSTimer?
(6 answers)
Closed 9 years ago.
I am making a Simon Says app to learn more about Objective C.
My SimonSaysViewController has 4 buttons. Their image needs to change accordingly when the pattern is being shown to the user.
A fixed interval timer will be absolutely fine.
I just cannot seem to find an example.
I basically would want a sort of setup like:
TimerTicked callback when I can do the image swapping logic.
Ideally, the TimerTicked method would be a method of my SimonSaysViewController.
How would this be done?
Thanks
NSTimer is your friend! Add an NSTimer property to your SimonSaysViewController.
#property (strong, nonatomic) NSTimer *tickTockTimer;
Depending on when you want the timer to start, you'll want to set up the timer then. Say you wanted the timer to start when the view first appears:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.tickTockTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerFired:) userInfo:nil repeats:YES];
}
Then implement the timerFired method and do what you need there.
- (void)timerFired:(NSTimer *)timer {
//change the image.
}
Don't forget to invalidate the timer when you are done.
- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.timer invalidate];
self.timer = nil;
}
This kind of thing usually works for me
NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:2.0]; // 2 sec from now
NSTimer *self.timer = [[NSTimer alloc] initWithFireDate:fireDate interval:5 target:self selector:#selector(timerDidTick) userInfo:nil repeats:YES]; // fire 5 sec apart
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:self.timer forMode:NSDefaultRunLoopMode];

iOS fading animation for multiple objects

I have an iOS app in which every time the view is loaded, all of the objects in the view quickly fade in.
In my code, I have a
- (void)fadeInEverything
function, and within this function, I call five different functions, with an NSTimer on each of them, so that the objects fade in sequentially, like this:
[self fadeInLabel];
[NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:#selector(fadeInTextField)
userInfo:nil
repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.4
target:self
selector:#selector(fadeInButton)
userInfo:nil
repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.6
target:self
selector:#selector(fadeInTextView)
userInfo:nil
repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.8
target:self
selector:#selector(fadeInFlipsideViewButton)
userInfo:nil
repeats:NO];
I have a function that looks like this for each object:
- (void)fadeInTextView
{
[resultView setAlpha:0];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[resultView setAlpha:1];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
[UIView commitAnimations];
}
My question is, would there we a way to make one function that would take a parameter (the object) and fade it in, so I don't have to have five almost identical functions? It would save me a lot of space and clutter.
You could pass your object by reference to accomplish that:
-(void)fadeInView:(UIView **)aView { // the ** indicates that this method is expecting a reference
// your fading code
// this works, because all view's (UILabels, UITextFields etc. inherit UIView.
}
And the calling method could look like this:
-(void)callFader {
[self fadeInView:&mytextView]; // make sure to put the & before the variable so that it's sent as a reference
// and more fadeInView calls...
}

How to stop NStimer event? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
NSTimer doesn't stop
In my application I am using NStimer to call an animation function every 3 seconds. I want to stop this timer and called another event while the timer is still running. Is this possible?
#interface
NSTimer *autoTimer;
#implementation
// Start timer
autoTimer = [NSTimer scheduledTimerWithTimeInterval:(3.0)
target:self
selector:#selector(autoTimerFired:)
userInfo:nil
repeats:YES];
// Stop timer:
[autoTimer invalidate];
autoTimer = nil;
First, you want to keep a pointer to the timer
self.packetTimer = [NSTimer timerWithTimeInterval:CONNECTION_TIMEOUT target:self selector:#selector(connectionTimeout:) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:packetTimer forMode:NSDefaultRunLoopMode];
If somewhere else in your code you want to cancel it, just call:
[self.packetTimer invalidate];
self.packetTimer = nil;

Resources