我定义了 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
}
}