beginAnimations commitAnimations retain a view?
Suppose,(pseudo code开发者_高级运维)
view.alpha = 1.0;
[beginAnimmations]
[animationDuration = 1.0]
view.alpha = 0.0;
[commitAnimations]
[view removeFromSuperView];
When view is not retained anywhere else than its superview, hence [view removeFromSuperView] will make the view be dealloc-ed.
Is this safe? or How can I do this correctly?
If the view
is not retained anywhere except by its superview, calling [view removeFromSuperView];
is completely alright. And, as you've afraid, view
will be released at that time. So, accessing it after the removeFromSuperView
method call will not be safe.
You should be retaining view
inorder to acces it even after you remove it from its superview.
Changes in your code:
You have to remove the view
after the animation is over. Add the following lines while you create animation.
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(onAnimationStopped)];
The onAnimationStopped method,
- (void)onAnimationStopped {
[view removeFromSuperview];
}
精彩评论