UIDynamicAnimator具有弹簧效果

时间:2015-07-15 16:10:49

标签: ios objective-c swift core-animation uikit-dynamics

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

    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组合在一起来实现弹簧效果

    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)

问题是附件行为似乎不起作用,因为视图在被推动后不会弹回原始位置。动画实际上将视图位置推到右侧100点。每次通话时origCirclesContainerCenter都是一样的。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

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