4

I'm trying to replace my core animation shake 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 the spring effect by 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 the attachment behavior doesn't seem to work because the view does not spring back to the original position after being pushed. The animation actually pushes the view position to the right 100 points. origCirclesContainerCenter is the same on every call. Any idea?

imObjCSwifting
  • 743
  • 1
  • 12
  • 34

1 Answers1

1

I changed the push behavior mode to UIPushBehaviorMode.Instantaneous to fix the problem.

imObjCSwifting
  • 743
  • 1
  • 12
  • 34