beginAnimations commitAnimations保留视图?

时间:2011-10-06 11:46:16

标签: iphone animation retain

假设,(伪代码)

view.alpha = 1.0;  
[beginAnimmations]  
[animationDuration = 1.0]
view.alpha = 0.0;
[commitAnimations]

[view removeFromSuperView];

当视图不在其超级视图以外的任何地方保留时,因此[view removeFromSuperView]将使视图被解除分类。

这样安全吗?或者我该如何正确地做到这一点?

1 个答案:

答案 0 :(得分:0)

如果 view 除了 superview 之外没有保留,则调用 [view removeFromSuperView]; 完全没问题。而且,正如您所担心的那样, view 将在那时发布。因此,在 removeFromSuperView 方法调用之后访问它将不安全。

您应该保留 view ,以便在将其从超级视图中删除后再访问它。

代码更改

动画结束后,您必须删除 view 。在创建动画时添加以下行。

[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(onAnimationStopped)];

onAnimationStopped 方法,

- (void)onAnimationStopped {

    [view removeFromSuperview];
}