UIKit Dynamics更改带有碰撞的视图框架 [英] UIKit Dynamics change a view frame with collisions

查看:108
本文介绍了UIKit Dynamics更改带有碰撞的视图框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目有几个视图,这些视图在其父视图的中心受到径向重力的作用,并发生弹性碰撞,因此它们不会重叠.它很好用,但是有时我需要调整视图的大小,并且当一个视图变大时,其他视图必须移开它,以便所有视图保持不重叠. 我希望碰撞行为能够意识到事物在相互接触并使它们分开,但这似乎并非如此.

My project has several views that are subject to radial gravity at the center of their parent view, and elastic collisions so that they don't overlap. It works great, but sometimes I need to resize the views, and, when a view gets larger, the other views must move away from it so that all the views remain non-overlapping. I would expect the collision behavior to realize things are touching and to move them apart, but it doesn't seem to.

这就是我现在正在尝试的内容:

Here's what I'm trying now:

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

该视图会很好地增长,动画代理会获悉该动画已恢复,但其他视图不会移开.新放大的视图仅与其他视图重叠(或根据z顺序重叠).

The view grows just fine, the animation delegate is informed that the animation has resumed, but the other views don't move away. The newly enlarged view just overlaps (or underlaps depending on z order) the other views.

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

I tried changing the transform and not the frame - no dice. I tried removing the view from all the behaviors - no dice. I tried removing some and all of the behaviors from the animator - no dice. I tried calling updateItemUsingCurrentState: on all of the views - no dice. I need some dice!

可以帮忙吗?谢谢...(swift-ies,请迅速回答.我会翻译)

Can you help? Thanks...(swift-ies, feel free to answer is swift. I'll translate it)

推荐答案

感谢这篇文章,我了解到所有必须从动画师中删除包含要更改的项目的行为,然后,在更改视图的属性后,应将这些行为重新添加到动画师中.

Thanks to this post, I learned that all behaviors that include the item being changed must be removed from the animator, then, after changing the view's properties, the behaviors should be added back to the animator.

我认为这很疯狂,但是确实有效.

I think that's crazy, but it worked.

[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];
}];

这篇关于UIKit Dynamics更改带有碰撞的视图框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆