ios - UIKit 动态 "SolveVelocityConstraints"崩溃

标签 ios objective-c ios7 uikit-dynamics

我在使用 UIKit Dynamics 时总是遇到崩溃。它不会在每次代码运行时发生,但它经常发生。有人遇到过这个吗?

Assertion failed: (a.x >= 0.0f && a.y >= 0.0f), 
function SolveVelocityConstraints, file 
/SourceCache/PhysicsKit/PhysicsKit-
4.6/PhysicsKit/Box2D/Dynamics/Contacts/b2ContactSolver.cpp, line 422.

这是产生错误的代码。我猜测它与碰撞边界有关。

UIDynamicAnimator *animator = 
 [[UIDynamicAnimator alloc] initWithReferenceView:self];

UIGravityBehavior *gravityBehavior = 
 [[UIGravityBehavior alloc] initWithItems:@[self.rewardButton]];
gravityBehavior.gravityDirection = CGVectorMake(0.0, 1.0);
gravityBehavior.magnitude = 1.0f;

UICollisionBehavior* collisionBehavior = 
 [[UICollisionBehavior alloc] initWithItems:@[self.rewardButton, 
                                              self.processingView]];
collisionBehavior.collisionDelegate = self;
UIEdgeInsets insets = UIEdgeInsetsMake(-700, 60, 200, 60);
[collisionBehavior setTranslatesReferenceBoundsIntoBoundaryWithInsets:insets];

UIDynamicItemBehavior* propertiesBehavior = 
 [[UIDynamicItemBehavior alloc] initWithItems:@[self.rewardButton]];
float elasticity = arc4random() % 4 * 0.1 + 0.1f;
propertiesBehavior.elasticity = elasticity;

[animator addBehavior:propertiesBehavior];
[animator addBehavior:gravityBehavior];
[animator addBehavior:collisionBehavior];

self.animator = animator;

最佳答案

似乎对我有用的一点技巧是为 UIDynamicItemBehavior 设置非常大的密度量。使用 1 或 2 的密度仍然给我错误,但 500 似乎没有。就像我说的,它很老套,但在我非常有限的测试中,它似乎可以解决问题。

UIDynamicItemBehavior *itemBehavior = 
 [[UIDynamicItemBehavior alloc] initWithItems:self.items];
[itemBehavior setDensity:500.0f];
[self.animator itemBehavior];

感谢 KillerRhino 的评论和他链接的 Box2D 线程引导我走上了这条研究项目质量问题的道路。根据UIDynamicItemBehavior documentation密度及其大小决定了质量。希望此变通办法在其他条件/设置下有效。

认为这个问题只发生在配备新 A7 芯片的新设备上(iPhone 5s、iPad Air、iPad Mini Retina)。可能是 64 位问题?

关于ios - UIKit 动态 "SolveVelocityConstraints"崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19754357/

相关文章:

ios - UITapGestureRecognizer 为 locationInView : 提供无效值

objective-c - 检查 SKNode 是否在屏幕上且可见

ios7 - 更新 : Localization stopped working in existing iPad app

java - iOS 开发者的第一个 Android 应用程序

ios7 - UITextView 内容插入底部不起作用 iOS7

objective-c - [图像计数] : unrecognized selector sent to instance

iphone - 从文件中加载文本

ios - 如何使用 Swift 将 Mapbox 缩放到以千米为单位的给定半径?

android - Flutter - 像素溢出的不明原因

ios - 在应用程序委托(delegate)中返回之前等待异步任务完成完成 block