我有一个 ViewController,已在 InterfaceBuilder 中配置为对其所有子视图使用 AutoLayout。布局工作正常。
我想使用 UIDynamicAnimator 提供的炫酷重力效果使其中一个子视图反弹。我认为这不适用于自动布局,但我尝试了一下,确实如此。效果很好!
我的问题是,这是预期的行为吗?它会在某个地方引起问题吗? Apple 是否提供有关结合 AutoLayout 和 UIDynamicAnimators 的任何指导?
这是代码,任何有兴趣的人:
var boundaryFrame = self.buttonBackgroundView.frame
boundaryFrame = CGRect(x: 0,
y: boundaryFrame.origin.y + (boundaryFrame.height + 1),
width: self.view.frame.width,
height: 2)
self.animator = UIDynamicAnimator(referenceView: self.view)
var gravity = UIGravityBehavior(items: [self.buttonBackgroundView])
gravity.magnitude = 0.5
var collision = UICollisionBehavior(items: [self.buttonBackgroundView])
var boundaryId: NSMutableString = NSMutableString(string: "bottomBoundary")
let boundaryPath = UIBezierPath(rect: boundaryFrame)
collision.addBoundaryWithIdentifier(boundaryId, forPath: boundaryPath)
let bounceProperties = UIDynamicItemBehavior(items: [self.buttonBackgroundView])
bounceProperties.elasticity = 0.5
// Move it up before causing it to bounce down to its original location
self.setMeUpTopConstraint.constant = 10
self.view.setNeedsUpdateConstraints()
self.view.layoutIfNeeded()
// Start animating
animator.addBehavior(gravity)
animator.addBehavior(collision)
animator.addBehavior(bounceProperties)
我的问题是,这是预期的行为吗?它会在某个地方引起问题吗? Apple 是否提供有关结合 AutoLayout 和 UIDynamicAnimators 的任何指导?
基本上,UIKit Dynamics 确实not与自动布局配合得很好。基本上,规则是改变某物的框架/中心与自动布局相反——而这正是 UIKit Dynamics 所做的。您没有遇到问题的唯一原因是,您正在制作动画的视图上碰巧没有发生布局。
测试的方法是这样的。运行您的代码,然后(当动画完成时;可能您需要使用延迟性能)运行
self.view.layoutIfNeeded
这会导致布局发生。如果那一刻事情发生了变化,那就暴露了问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)