0

好的,所以我将 UIView 子类化并且视图有一堆子视图。这些子视图是动画的。当我将另一个视图控制器推到顶部时,或者应用程序进入后台时,动画停止。

在我的子视图中重新启动这些动画的正确方法是什么。我不想让 View Controller 告诉 View 重新启动它们,因为我觉得 View 应该知道什么时候需要重新启动,而不会被另一个对象明确告知。

但是我不能在子类 UIView 上使用 layoutSubviews,因为正在发生的动画正在添加新的 UIImageViews。发生这种情况时,会在 UIView 子类上调用 layoutSubviews。所以 layoutSubviews 被调用了很多,我不能用它来重新启动动画。

想法?

更新 - 动画代码看起来像这样......但我认为这并不重要。

[UIView animateWithDuration:ANIMATION_DURATION delay:0 options:UIViewAnimationOptionCurveLinear     animations:^{
    [subview setFrame:finalFrame];
} completion:^(BOOL finished) {
    [subview removeFromSuperview];
}];
4

2 回答 2

0

使用 NSNotificationCenter 通知视图视图控制器再次出现,如下所示:

在您的 UIView 中,将其添加到 init 方法中:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(methodToExecuteWhenUIViewControllerComesBackUp:) name:@"Came Back" object:nil];

然后在您的“其他 viewController”中,在您再次展示原始控制器的方法中,添加:

[[NSNotificationCenter defaultCenter] postNotificationName:@"Came Back" object:nil];

然后,当您返回原始 viewController 时,您的 UIView 将收到通知。为每个要重新激活的子视图重复第一段代码。

当任何子视图被删除(释放)内存时,请确保执行:

[[NSNotificationCenter defaultCenter] removeObserver:self];

如果上面的代码没有执行,通知中心会尝试通知一个不再存在的对象,导致你的应用崩溃。

于 2013-03-23T02:56:35.840 回答
0

不要使用UIView Animation,你最好使用Core Animation

CAAnimation它有一个名为removedOnCompletiondefault 的属性将它YES设置为NO( yourCAAnimation.removedOnCompletion = NO;) 它将使您的应用程序从backgroundforeground,动画也animated

苹果说:

完成时移除

Determines if the animation is removed from the target layer’s animations upon completion.

@property(getter=isRemovedOnCompletion) BOOL removedOnCompletion
Discussion
When YES, the animation is removed from the target layer’s animations once its active duration has passed. Defaults to YES.

Availability
Available in iOS 2.0 and later.
Related Sample Code
MoveMe
Declared In
CAAnimation.h
于 2013-03-23T02:57:54.783 回答