播放自动布局时使用 UIDynamicAnimator 制作动画
Animating with UIDynamicAnimator when Autolayout is in play
我有一个 ViewController 已在 InterfaceBuilder 中配置为对其所有子视图使用 AutoLayout。布局工作正常。
我想使用 UIDynamicAnimator 提供的炫酷重力效果使其中一个子视图反弹。我认为这不适用于 AutoLayout,但我试过了,确实如此。效果很好!
我的问题是,这是预期的行为吗?它会在某处引起问题吗? 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)
是的,UIDynamicAnimator 与 AutoLayout 配合得很好。我有一个严重依赖 UIDynamicAnimator 并使用约束的应用程序,它运行良好。我发现的唯一问题是 UIView animateWithDuration 不适用于约束,但该方法无论如何都不适用于 UIDynamicAnimator。
希望对您有所帮助:)
My question is, is this the expected behavior? Is it going to cause problems somewhere? Does Apple provide any guidance around combining AutoLayout and UIDynamicAnimators?
基本上,UIKit Dynamics 不 与自动布局配合得很好。基本上,规则是改变某些东西的框架/中心是与自动布局相反的——而这正是 UIKit Dynamics 所做的。您没有遇到问题的唯一原因是,您正在设置动画的视图碰巧没有发生布局。
测试方法是这样的。 运行 您的代码,然后(当动画结束时;可能您需要使用延迟性能)运行
self.view.layoutIfNeeded
这会导致布局发生。如果那一刻事情突然发生,它就会暴露问题。
我有一个 ViewController 已在 InterfaceBuilder 中配置为对其所有子视图使用 AutoLayout。布局工作正常。
我想使用 UIDynamicAnimator 提供的炫酷重力效果使其中一个子视图反弹。我认为这不适用于 AutoLayout,但我试过了,确实如此。效果很好!
我的问题是,这是预期的行为吗?它会在某处引起问题吗? 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)
是的,UIDynamicAnimator 与 AutoLayout 配合得很好。我有一个严重依赖 UIDynamicAnimator 并使用约束的应用程序,它运行良好。我发现的唯一问题是 UIView animateWithDuration 不适用于约束,但该方法无论如何都不适用于 UIDynamicAnimator。 希望对您有所帮助:)
My question is, is this the expected behavior? Is it going to cause problems somewhere? Does Apple provide any guidance around combining AutoLayout and UIDynamicAnimators?
基本上,UIKit Dynamics 不 与自动布局配合得很好。基本上,规则是改变某些东西的框架/中心是与自动布局相反的——而这正是 UIKit Dynamics 所做的。您没有遇到问题的唯一原因是,您正在设置动画的视图碰巧没有发生布局。
测试方法是这样的。 运行 您的代码,然后(当动画结束时;可能您需要使用延迟性能)运行
self.view.layoutIfNeeded
这会导致布局发生。如果那一刻事情突然发生,它就会暴露问题。