【发布时间】:2016-10-19 15:45:33
【问题描述】:
我定义了 UIDynamicAnimator 属性:
lazy fileprivate var animator: UIDynamicAnimator = {
return UIDynamicAnimator(referenceView: self)
}()
self 是 UIView 的子类;
在 self 类的扩展中,同一个文件,我有动画逻辑,使用我的动画师,添加 UIDynamicBehavior 项:
let pushBehavior = UIPushBehavior(items: [stampView], mode: .continuous)
//some settings
let dynamicItemBehavior = UIDynamicItemBehavior(items: [stampView])
//some settings
let gravityBehavior = UIGravityBehavior(items: [stampView])
//some settings
let collisionBehavior = UICollisionBehavior(items: [stampView])
//some settings
一切正常,但是当我尝试使用 removeAllBehaviors() 停止所有动画时,动画会停止,但行为仍然在 animator.behaviors 中。我第二次调用它时,数组变为空。
//======
对于我的 pushBehavior,我添加了动作,它改变了 var,表明我达到了目标点:
pushBehavior.action = { [unowned stampView] in
if stampView.center.x <= endPosition.x {
lastJump = true
}
}
在 collisionBehavior 委托方法中,我检查此变量并尝试使用 removeAllBehaviors() 停止动画
public func collisionBehavior(_ behavior: UICollisionBehavior, beganContactFor item: UIDynamicItem, withBoundaryIdentifier identifier: NSCopying?, at p: CGPoint) {
if lastJump {
//animator.behaviors.count = 4
animator.removeAllBehaviors()
//still, animator.behaviors.count = 4
}
}
【问题讨论】:
-
您显示的代码完全错误!我在您的代码中的任何地方都没有看到
removeAllBehaviors。请显示那个代码,并说明你如何知道这些行为没有被删除。 -
@matt 现在好点了吗?
-
是的,现在我可以回答了。
标签: ios swift animation uidynamicanimator