ios - UIDynamicAnimator 摇动 Spring 效果

标签 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 来实现 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)

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

最佳答案

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

关于ios - UIDynamicAnimator 摇动 Spring 效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31435673/

相关文章:

objective-c - 异步 setUp 方法完成后运行测试

ios - 选择另一个表行时如何删除 UITableViewCellAccessoryCheckmark 的自定义图标

ios - 从嵌入的 tableviewcontroller 中执行Segue时如何保留NavigationController

ios - WKWebView 白屏(Objective-C)

objective-c - 将小时和分钟与当前时间进行比较

iphone - iOS MKMapView,底部有位置搜索

ios - NSData 未保存在本地文件中

iOS purgeIdleCellConnections

ios - 分页限制在 getStream iOS 中不起作用

ios - 改变 UIView 相对于 UITableview 的高度