preface
This article mainly introduces in detail the iOS music player image rotation, the example code is very detailed, has a certain reference value, interested partners can refer to it.
By adding CABasicAnimation to CXGImageView, which inherits from UIImageView, the player image rotation effect is realized. Mainly provide three methods: startRotating stopRotating, resumeRotate
###1. startRotating
Func startRotating() {let rotateAnimation = CABasicAnimation(keyPath: "Transform. Rotation") rotateAnimation. IsRemovedOnCompletion = false / / avoid click the Home button to return to, animation stop rotateAnimation. FromValue = 0.0 rotateAnimation. ToValue = Double. PI * 2 rotateAnimation. Duration = 20 rotateAnimation. RepeatCount = MAXFLOAT self.layer.add(rotateAnimation, forKey: nil) isRotating = true }Copy the code
2. stopRotating
/// Stop animating func stopRotating() {if! isRotating { return } let pausedTime = self.layer.convertTime(CACurrentMediaTime(), from: Self.layer. speed = 0 self.layer.timeOffset = pausedTime isRotating = false }Copy the code
3.resumeRotate
Func resumeRotate() {if isRotating {return} if self.layer.timeoffset == 0 {startRotating() return} let pausedTime = self.layer.timeOffset // 1. Let CALayer's time continue walking self.layer.speed = 1.0 // 2. Cancels last recorded dwell time self.layer.timeOffset = 0.01 // 3. Unset time self.layer.beginTime = 0.0 // 4. Let timeWhenpause = sell.layer. convertTime(CACurrentMediaTime(), from: nil) - pausedTime // 5. Self.layer. beginTime = timeWhenpause isRotating = true} Self.layer. beginTime = timeWhenpause isRotating = true}Copy the code
Note: When you press the Home button to return to the application again, the image stops moving, and you need to set isRemovedOnCompletion to false.
The above is all the content of this article, I hope to help you
The original address: blog.csdn.net/CuiXg/artic…