UIDynamicAnimator Shake with spring effect

I am trying to replace my main shaking animation effect in this code

    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")

      

with spring effect, combining UIPushBehavior

and UIAttachmentBehavior

like this

    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)

      

The problem is that the attachment behavior is not working because the view does not return spring to its original position after being clicked. The animation actually pushes the view position to the correct 100 points. origCirclesContainerCenter

the same for every call. Any idea?

+3


source to share


1 answer


I changed the push behavior mode to UIPushBehaviorMode. First, to fix the problem.



0


source







All Articles