UIKit Dynamics change a view frame with collisions

心已入冬 提交于 2019-12-11 14:29:07

问题


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

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.

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!

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


回答1:


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


来源:https://stackoverflow.com/questions/45339297/uikit-dynamics-change-a-view-frame-with-collisions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!