gpt4 book ai didi

ios - 无限旋转 UIView 并暂停/停止动画,保持状态,并从当前状态继续旋转

转载 作者:搜寻专家 更新时间:2023-11-01 05:57:02 25 4
gpt4 key购买 nike

我很难尝试暂停并从当前的旋转状态继续任何 UIView 旋转技术。

尝试过:

CGAffineTransformRotate
CGAffineTransformMakeRotation
CATransform3DMakeRotation
CABasicAnimation

基本上,如果我调用 view.layer.removeAllAnimations()layer.speed = 0 ,它们都会重置当前的旋转角度。

还尝试使用图像而不是真正的旋转对 View 进行快照,但没有成功,因为快照忽略了旋转。

最佳答案

终于明白了,关于 SO 的答案不止一个,很多在 objective-c 中,将它们全部放在一个 UIView 扩展中,甚至记录在案:

extension UIView {

/**
Will rotate `self` for ever.

- Parameter duration: The duration in seconds of a complete rotation (360º).
- Parameter clockwise: If false, will rotate counter-clockwise.
*/
func startRotating(duration duration: Double, clockwise: Bool) {
let kAnimationKey = "rotation"
var currentState = CGFloat(0)

// Get current state
if let presentationLayer = layer.presentationLayer(), zValue = presentationLayer.valueForKeyPath("transform.rotation.z"){
currentState = CGFloat(zValue.floatValue)
}

if self.layer.animationForKey(kAnimationKey) == nil {
let animate = CABasicAnimation(keyPath: "transform.rotation")
animate.duration = duration
animate.repeatCount = Float.infinity
animate.fromValue = currentState //Should the value be nil, will start from 0 a.k.a. "the beginning".
animate.byValue = clockwise ? Float(M_PI * 2.0) : -Float(M_PI * 2.0)
self.layer.addAnimation(animate, forKey: kAnimationKey)
}
}

/// Will stop a `startRotating(duration: _, clockwise: _)` instance.
func stopRotating() {
let kAnimationKey = "rotation"
var currentState = CGFloat(0)

// Get current state
if let presentationLayer = layer.presentationLayer(), zValue = presentationLayer.valueForKeyPath("transform.rotation.z"){
currentState = CGFloat(zValue.floatValue)
}

if self.layer.animationForKey(kAnimationKey) != nil {
self.layer.removeAnimationForKey(kAnimationKey)
}

// Leave self as it was when stopped.
layer.transform = CATransform3DMakeRotation(currentState, 0, 0, 1)
}

}

yourView.startRotating(duration: 1, clockwise: true) 一样使用它,稍后停止执行 yourView.stopRotating()

关于ios - 无限旋转 UIView 并暂停/停止动画,保持状态,并从当前状态继续旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38471345/

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