MGD
MGD

Reputation: 783

UIView Animation Problem

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.30f];

[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:viewSettings cache:YES];   
viewSettings.alpha = 0;
[viewSettings removeFromSuperview];
[UIView commitAnimations];

I ve written the code above that works well when I add the view via animation, but it doesn't work when i remove the view from superview. Animation works if I remove [viewSettings removeFromSuperview] line. I don't know where I'm doing wrong.

Upvotes: 0

Views: 643

Answers (4)

MJeffryes
MJeffryes

Reputation: 458

You need to remove it from the superview after the animation has completed. This is very easy to accomplish if you use the blocks based API, which Apple is encouraging you to do:

[UIView transitionWithView:viewSettings 
                  duration:0.30f 
                   options:UIViewAnimationOptionTransitionNone  
                animations:^{ 
    [viewSettings setAlpha:0];
} completion:^(BOOL finished) {
    [viewSettings removeFromSuperview];
}];

You can read about all the options in Apple's documentation.

Upvotes: 2

Subhash
Subhash

Reputation: 544

Try removing view after the animation is completed. Initially alpha value of the view is 1 then, you apply the animation and make it 0. Now the view is still there but it is not visible. Once the animation is over then remove the view. I think it should work.

Upvotes: 1

highlycaffeinated
highlycaffeinated

Reputation: 19867

removeFromSuperview is not an animatable action, so it is getting performed immediately. Once you commitAnimations, your view is no longer part of it's superview, so you can't see the animation, if it is still even happening.

If you want your animation to happen, then the view to get removed, call removeFromSuperview when the animation ends, such as in a selector specified with setAnimationDidStopSelector:.

Upvotes: 1

user756245
user756245

Reputation:

I think viewSettings is removed before you commit the animation. Try inverting the two last lines.

Upvotes: 0

Related Questions