Jua*_*ero 1 cocoa-touch rotation uiview ios swift
我很难尝试暂停并从旋转的当前状态继续任何UIView旋转技术.
尝试:
CGAffineTransformRotate
CGAffineTransformMakeRotation
CATransform3DMakeRotation
CABasicAnimation
Run Code Online (Sandbox Code Playgroud)
基本上,如果我打电话view.layer.removeAllAnimations()或,layer.speed = 0他们都重置当前的旋转度.
还试图快照视图以使用图像而不是真正的旋转,但没有运气,因为快照忽略了旋转.
最后得到它,在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)
}
}
Run Code Online (Sandbox Code Playgroud)
使用它yourView.startRotating(duration: 1, clockwise: true),稍后停止做yourView.stopRotating().
| 归档时间: |
|
| 查看次数: |
726 次 |
| 最近记录: |