因此,我使用 UI Kit Dynamics 创建一个 slider ,可以将其滑开以显示其背后的内容。我使用两个 UIFieldBehaviour、附加到 UIPanGestureRecognizer 的附件行为和 UICollisionBehaviour 来实现此目的。 Interface Builder 中的结构如下所示: Here an image of my view hierarchy
TrayContainerView 应用了约束,它也是我的 DynamicAnimator 的引用 View 。托盘View是dynamicAnimator作用的dynamicItem。
这工作正常,直到我从 Interface Builder 中向托盘 View 添加 subview ,此时每当我的应用程序切换到该特定 viewController 时我都会收到此消息:
由于未捕获的异常“无效关联”而终止应用程序,原因:“UIDynamicAnimator 最多支持 32 个不同的字段”
奇怪的是,如果我以编程方式添加 subview ,除了布局的一些问题之外,它可以正常工作。
这是我的动态行为的设置
container = UICollisionBehavior(items: [self])
let boundaryWidth = UIScreen.mainScreen().bounds.size.width
container.addBoundaryWithIdentifier("upper", fromPoint: CGPointMake(0, offset), toPoint: CGPointMake(boundaryWidth, offset))
let boundaryHeight = self.superview!.frame.size.height + self.frame.size.height - self.layoutMargins.top
container.addBoundaryWithIdentifier("lower", fromPoint: CGPointMake(0,boundaryHeight), toPoint: CGPointMake(boundaryWidth,boundaryHeight))
tractorField = UIFieldBehavior.linearGravityFieldWithVector(CGVectorMake(0, 30))
tractorField.direction = CGVectorMake(0, -10)
let referenceRegion = (superview?.frame)!
let fieldCoefficient:CGFloat = 1
tractorField.region = UIRegion(size: CGSize(width: referenceRegion.width, height: referenceRegion.height*fieldCoefficient))
tractorField.position = CGPointMake((self.superview?.center.x)!, referenceRegion.height*fieldCoefficient/2)
tractorField.addItem(self)
gravityField = UIFieldBehavior.linearGravityFieldWithVector(CGVectorMake(0, 30))
gravityField.direction = CGVectorMake(0, 10)
gravityField.region = UIRegion(size: CGSize(width: referenceRegion.width, height: referenceRegion.height*fieldCoefficient))
gravityField.position = CGPointMake((self.superview?.center.x)!, referenceRegion.height + referenceRegion.height*fieldCoefficient/2)
gravityField.addItem(self)
animator.addBehavior(gravityField)
animator.addBehavior(tractorField)
dynamicItem = UIDynamicItemBehavior(items: [self])
animator.addBehavior(container)
animator.addBehavior(dynamicItem)
任何想法将不胜感激。
最佳答案
想通了。问题是我尝试子类化 UIView 并在那里执行所有动态操作。不幸的是,这不起作用,因为我怀疑动态动画师被提前调用了?我在 awakeFromNib 中实例化了它,这就是崩溃的原因。无论如何,在 viewController 的 viewDidLoad 方法中实现整个事情,并且一切都按预期工作。
关于ios - 在 Interface Builder 中将 subview 添加到动态项时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36611152/