gpt4 book ai didi

ios - 具有 Spring 效果的旋转动画

转载 作者:可可西里 更新时间:2023-11-01 01:34:42 25 4
gpt4 key购买 nike

我想用 Spring 效果旋转 CAShapeLayer(比如在 UIView.animateWithDuration(_:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:) 中)但是在层上而不是在 View 上.

当点击按钮时,它的主层子层应该旋转到 3*PI/4 并且 Spring 应该反弹到 2*PI/3。然后,当再次点击按钮时,图层旋转应该按照与之前相反的顺序进行:首先反弹到 2*PI/3,然后旋转到初始位置(在第一次旋转之前)。

我怎么做到的?我无法通过 UIView.animateWithDuration(_:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:) 实现它,因为默认情况下层的转换属性是可动画的。

我试过更改 CATransaction 但它只旋转了一个角度(没有考虑其他旋转):

let rotation1 = CGAffineTransformRotate(CGAffineTransformIdentity, angle1)
let rotation2 = CGAffineTransformRotate(CGAffineTransformIdentity, angle2)
let transform = CGAffineTransformConcat(rotation1, rotation2)
CATransaction.begin()
CATransaction.setAnimationDuration(0.6)
self.plusLayer.setAffineTransform(transform)
CATransaction.commit()

更新

根据 Duncan C post我尝试使用 CASpringAnimation 并在一个方向上实现动画:

myLayer.setAffineTransform(CGAffineTransformMakeRotation(angle))
let spring = CASpringAnimation(keyPath: "transform.rotation")
spring.damping = 12.0
spring.fromValue = 2.0 * CGFloat(M_PI)
spring.toValue = 3.0 * CGFloat(M_PI_4)
spring.duration = 0.5
myLayer.addAnimation(spring, forKey: "rotation")

但是如何在点击按钮时反转动画?

预先感谢您的帮助。

最佳答案

UIView block 动画用于动画 View 属性。您可以使用 UIView.animateWithDuration(_:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:

为按钮的变换(CGAffineTransform 类型,二维变换)设置动画

但是,如果您需要为图层属性设置动画,则需要使用 Core Animation。

Apple 似乎在 iOS 9 中添加(或公开)了一个 spring CAAnimation。不过它似乎不在 Xcode 文档中。

查看此线程:

SpringWithDamping for CALayer animations?

关于ios - 具有 Spring 效果的旋转动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38682580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com