UIKit Dynamics 通过碰撞更改视图框架

UIKit Dynamics change a view frame with collisions

我的项目有几个视图在其父视图的中心受到径向重力和弹性碰撞,因此它们不会重叠。它工作得很好,但有时我需要调整视图的大小,并且当一个视图变大时,其他视图必须远离它,以便所有视图保持不重叠。 我希望碰撞行为能够意识到物体正在接触并将它们分开,但它似乎没有。

这是我现在正在尝试的:

[UIView animateWithDuration:0.5 animations:^{
    view.frame = CGRectInset(view.frame, -30, -30);
} completion:^(BOOL finished) {
    [self.animator updateItemUsingCurrentState:view];
}];

视图增长得很好,动画委托被告知动画已经恢复,但其他视图并没有离开。新放大的视图与其他视图重叠(或根据 z 顺序重叠)。

我尝试更改变换而不是框架 - 不行。我尝试从所有行为中删除视图 - 没有骰子。我尝试从动画师中删除一些和所有行为 - 没有骰子。我尝试在所有视图上调用 updateItemUsingCurrentState: - 不行。我需要一些骰子!

你能帮忙吗?谢谢...(swift-ies,随便回答是swift。我会翻译的)

感谢 this post,我了解到必须从动画器中删除所有包含正在更改的项目的行为,然后,在更改视图的属性后,应将这些行为添加回动画器。

我认为这很疯狂,但它奏效了。

[self.animator removeBehavior:self.behavior]; // for all behaviors that have view as an item

[UIView animateWithDuration:0.5 animations:^{
    view.frame = CGRectInset(view.frame, -30, -30);
} completion:^(BOOL finished) {
    [self.animator updateItemUsingCurrentState:view];
    [self.animator removeBehavior:self.behavior];
}];