UIView animateWithDuration not respecting delay - iphone

I want to schedule a series of animations which animate intro text across the screen. The very last animation, on completion, should fire off game tick logic that should run the game.
For some reason everything is happening immediately. Can anyone shed any light on why this is happening or a better alternative?
[self.view bringSubviewToFront:_ViewIntroSeconds];
[UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
animations:^(void) {
[_IntroLabel1 setAlpha:1];
}
completion:^(BOOL finished){
}];
[UIView animateWithDuration:0.4 delay:3.0 options:UIViewAnimationOptionCurveEaseInOut
animations:^(void) {
[_IntroLabel2 setAlpha:1];
[_IntroLabel1 setAlpha:0];
[_IntroLabel1 setCenter:CGPointMake(0, _IntroLabel1.center.y)];
}
completion:^(BOOL finished){
}];
[UIView animateWithDuration:0.4 delay:5.0 options:UIViewAnimationOptionCurveEaseInOut
animations:^(void) {
[_IntroLabel3 setAlpha:1];
[_IntroLabel2 setAlpha:0];
[_IntroLabel2 setCenter:CGPointMake(0, _IntroLabel2.center.y)];
}
completion:^(BOOL finished){
[self updateGame];
[self updateClock];
[_ViewIntroSeconds setAlpha:0];
}];

My suggestion would be to set the delay to 0, and place the subsequent UIView animation blocks in the completion block of the previous animateWithDuration statements:
[UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
animations:^(void) {
[_IntroLabel1 setAlpha:1];
}
completion:^(BOOL finished){
[UIView animateWithDuration:0.4 delay:2.6 options:UIViewAnimationOptionCurveEaseInOut
animations:^(void) {
[_IntroLabel2 setAlpha:1];
[_IntroLabel1 setAlpha:0];
[_IntroLabel1 setCenter:CGPointMake(0, _IntroLabel1.center.y)];
}
completion:^(BOOL finished){
[UIView animateWithDuration:0.4 delay:1.6 options:UIViewAnimationOptionCurveEaseInOut
animations:^(void) {
[_IntroLabel3 setAlpha:1];
[_IntroLabel2 setAlpha:0];
[_IntroLabel2 setCenter:CGPointMake(0, _IntroLabel2.center.y)];
}
completion:^(BOOL finished){
[self updateGame];
[self updateClock];
[_ViewIntroSeconds setAlpha:0];
}];
}];
}];
This will give you the desired effect.
EDIT : The reason this is happening is, UIView animateWithDuration block begins animation, and moves on to execute the following code, while the animation is still in effect. Hence it is advised to perform the 'next' animation effects in the completion block. Otherwise, all animateWithDuration requests will be performed almost instantly.
EDIT 2: I have edited the delay in the blocks to give you the 'illusion' of 0, 3, and 5 seconds respectively.

Related

UIView Animation Block Makes No Changes

The following code is sheer simple. _facebookF is a UIImageView.
if (animationOn == YES) {
[UIView animateWithDuration:0.4 animations:^{
[_faceBookF setAlpha:0];
}];
[_faceBookF setImage:[UIImage imageNamed:#"facebookFBlue.png"]];
[UIView animateWithDuration:0.4 animations:^{
[_faceBookF setAlpha:1];
}];
} else {
[UIView animateWithDuration:0.4 animations:^{
[_faceBookF setAlpha:0];
}];
[_faceBookF setImage:[UIImage imageNamed:#"facebookF.png"]];
[UIView animateWithDuration:0.4 animations:^{
[_faceBookF setAlpha:1];
}];
}
The app gets to the method which contains this code fine, but no changes to the UI occur throughout the entire block of code. To my knowledge, the app is running on the main thread.
Any guesses?
UPDATE--In fact, the issue is that the image is not being changed at all--with or without the animation. Both images are accessed fine in other parts of the app.
It looks like you are trying to run sequential animations, but the way you have coded them, they are likely to run in parallel. What you need to do is this:
if (animationOn == YES) {
[UIView animateWithDuration:0.4 animations:^{
[_faceBookF setAlpha:0];
} completion:^(BOOL finished) {
[_faceBookF setImage:[UIImage imageNamed:#"facebookFBlue.png"]];
[UIView animateWithDuration:0.4 animations:^{
[_faceBookF setAlpha:1];
}];
}];
}
else {
[UIView animateWithDuration:0.4 animations:^{
[_faceBookF setAlpha:0];
}completion:^(BOOL finished) {
[_faceBookF setImage:[UIImage imageNamed:#"facebookF.png"]];
[UIView animateWithDuration:0.4 animations:^{
[_faceBookF setAlpha:1];
}];
}];
}
Try this. This starts the second animation after the first has completed. So it will fade out the UIImageView and then fade it up with different version of the image.
[UIView animateWithDuration: 0.4
animations:^
{
[_faceBookF setAlpha:0];
}
completion:^(BOOL finished)
{
[_faceBookF setImage:[UIImage imageNamed:#"facebookFBlue.png"]];
[UIView animateWithDuration:0.4
animations:^
{
[_faceBookF setAlpha:1];
}];
}];
Thanks for your responses. I've implemented the completion block you both mentioned to avoid both animations firing at once. However, the main issue had to do with the method not being accessed properly. I can't see exactly why, as the facebookF variable was not nil, and the if and log statements in the method worked fine. Anyway, I have switched to a NSNotification solution, and now all parts of the method are being accessed.

Running an animation one after the other fails

I'm trying to animate an image to rotate left and right when selected, basically to let the user which the object they're touching.
I found some code for animating:
- (void)rotateImage:(UIImageView *)image duration:(NSTimeInterval)duration
curve:(int)curve degrees:(CGFloat)degrees delay:(CGFloat)delay
{
// Setup the animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:delay];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
[UIView setAnimationBeginsFromCurrentState:YES];
// The transform matrix
CGAffineTransform transform =
CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees));
image.transform = transform;
// Commit the changes
[UIView commitAnimations];
}
However, when I try to run two animations, only the last one ever works. Even with a proper delay, only the second animation will show:
[self rotateImage:self duration:.5
curve:UIViewAnimationCurveEaseIn degrees:60 delay:0];
[self rotateImage:self duration:.5
curve:UIViewAnimationCurveEaseIn degrees:-60 delay:5];
What can I do to create the animation so it rotates left, then rotates right?
[UIView animateWithDuration:0.2 animations:^{
// animation left
[UIView setAnimationDelay:10];
} completion:^(BOOL finished){
[UIView animateWithDuration:0.2 animations:^{
// animation right
} completion:^(BOOL finished){
// done
}];
}];
found it here
https://developer.apple.com/library/content/featuredarticles/Short_Practical_Guide_Blocks/
[UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
self.view.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(60));
} completion:^(BOOL finished){
[UIView animateWithDuration:0.5f delay:5.0f options:UIViewAnimationOptionCurveEaseIn
animations:^{
self.view.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-60));
}
completion:^(BOOL finished){}];
}];
Animation blocks are definitely the way to go. This should replicate what you're trying to do, including the easing. This assumes your calling from a view controller, but if your putting this code into a UIView or UIImageView, then just replace self.view with self.
[UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
self.view.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(60));
} completion:^(BOOL finished){}];
[UIView animateWithDuration:0.5f delay:5.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
self.view.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-60));
} completion:^(BOOL finished){}];

delay in getting complete boolean callback of UIView block animation (animateWithDuration)

i am doing block animation, my project is using opengl for gameplay and uiview for menu like stuff.
i am animating uiview using animateWithDuration method, bt facing issue at getting delay complete animation boolean callback. I tried the same code in UIKit app and it works very smoothly.
Below is my code:
-(void) playAnimation:(UIButton *)_button atPosition:(CGRect)_rect withDuration:(CGFloat)_duration andDelay:(CGFloat)_delay andDiection:(NSInteger)_direction
{
[UIView animateWithDuration:_duration
delay:_delay
options:UIViewAnimationCurveLinear
animations:^{
_button.frame = _rect;
}
completion:^(BOOL finished){
[UIView animateWithDuration:0.1
delay:0.0
options:UIViewAnimationCurveEaseInOut
animations:^{
CGRect rect = _rect;
rect.origin.x = _rect.origin.x + (10 * _direction);
_button.frame = rect;
}
completion:^(BOOL finished){
[UIView animateWithDuration:0.1
delay:0.0
options:UIViewAnimationCurveEaseInOut
animations:^{
_button.frame = _rect;
}
completion:^(BOOL finished){
}];
}];
}];
}

how to stop an already running animation

I am doing some animation using block level technique. I want to stop the animation on the basis of certain condition. How will I stop the animation from completing?
Following is my code for animation:
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction
animations:^{
if([arrAns count]>0)
vwb1.center = CGPointMake(260, 40);
}
completion:^(BOOL finished){
[UIView animateWithDuration:1.5 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction
animations:^{
if([arrAns count]>1)
vwb2.center = CGPointMake(260, 100);
}
completion:^(BOOL finished){
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction
animations:^{
if([arrAns count]>2)
vwb3.center = CGPointMake(260, 160);
}
completion:^(BOOL finished){
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction
animations:^{
if([arrAns count]>3)
vwb4.center = CGPointMake(260, 220);
}
completion:^(BOOL finished){
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
if([arrAns count]>4)
vwb5.center = CGPointMake(260, 280);
} completion:nil];
}];
}];
}
];
}];
I am not having any key here so I can nnot remove it on the basis of a key. There is also a feature as removeallanimation but I am not able to use it. If someone could help me on how to use that it will also be fine.
Edit: Ok I figured out how to do it, I am doing it using following code:
[self.view.layer removeAllAnimations];
But now I have problem that my block level animation which have not yet started is still running. So if I remove the animation at second block it still executes remaining block. I need to stop that also.
Thanks
Pankaj
You should check finished variable. And start next animation (inside a block) only if previous was finished (finished == YES), not stopped.
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
...
completion
A block object to be executed when the animation sequence
ends. This block has no return value and takes a single Boolean
argument that indicates whether or not the animations actually
finished before the completion handler was called. If the duration of
the animation is 0, this block is performed at the beginning of the
next run loop cycle.

Why does transition animation not work with block objects, but with deprecated animations API

I'm trying a flip or curl between to views in a container which works pretty well using the following code:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.8];
[UIView setAnimationTransition:animationOption
forView:container cache:YES];
[container addSubview:nuView];
[oldView removeFromSuperview];
[UIView commitAnimations];
but does not with
[UIView transitionWithView:container
duration:.8
options:animationOption
animations:^
{
[container addSubview:nuView];
[oldView removeFromSuperview];
}
completion:^(BOOL finished)
{
}];
Any ideas?
What is animationOption? This code works perfectly well in one of my apps:
[UIView transitionWithView:currentContainerView
duration:0.75
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[currentContainerView.frontView removeFromSuperview];
[currentContainerView addSubview:currentContainerView.flippedSideView];
}
completion:^(BOOL finished) {
// stuff
}];
Check if you are using UIViewAnimationOptionTransition or UIViewAnimationTransition !