具有 spring 效果的 UIDynamicAnimator 摇动

UIDynamicAnimator Shake with spring effect

我正在尝试在此代码中替换我的核心动画抖动效果

    let shake = CAKeyframeAnimation(keyPath: "position.x")
    shake.values = [0, 20, -20, 20, -15, 15, -5, 5, 0]
    shake.keyTimes = [0, 1/10.0, 3/10.0, 5/10.0, 6/10.0, 7/10.0, 8/10.0, 9/10.0, 1]
    shake.duration = 0.4
    shake.additive = true
    circlesContainer.layer.addAnimation(shake, forKey: "shakeYourBooty")

通过像这样组合 UIPushBehaviorUIAttachmentBehavior 具有 spring 效果

    if origCirclesContainerCenter == nil {
        origCirclesContainerCenter = circlesContainer.center
    }

    shake = UIDynamicAnimator(referenceView: self.view)
    let push = UIPushBehavior(items: [circlesContainer], mode: UIPushBehaviorMode.Continuous)
    push.pushDirection = CGVectorMake(100.0, 0.0)

    print(origCirclesContainerCenter)
    let attachement = UIAttachmentBehavior(item: circlesContainer, attachedToAnchor:origCirclesContainerCenter!)
    attachement.frequency = 3.0
    attachement.damping = 0.5

    shake.addBehavior(attachement)
    shake.addBehavior(push)

问题是附件行为似乎不起作用,因为视图在被推后没有 spring 回到原来的位置。动画实际上将视图位置向右推了 100 点。 origCirclesContainerCenter 每次调用都相同。有什么想法吗?

我将推送行为模式更改为 UIPushBehaviorMode.Instantaneous 以解决问题。