简体   繁体   中英

UIDynamicAnimator Shake with spring effect

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM