Swift使用CoreAnimation实现旋转动画,包括暂停旋转和继续旋转方法

 

func startRotate() {
        let rotateAni = CABasicAnimation(keyPath: "transform.rotation")
        rotateAni.fromValue = 0.0
        rotateAni.toValue = M_PI * 2.0
        rotateAni.duration = 10
        rotateAni.repeatCount = MAXFLOAT
        self.view.layer.addAnimation(rotateAni, forKey: nil)
    }
    
    func pauseRotate() {
        let pauseTime = self.view.layer.convertTime(CACurrentMediaTime(), toLayer: nil)
        self.view.layer.speed = 0.0
        self.view.layer.timeOffset = pauseTime
    }
    
    func resumeRotate() {
        let pauseTime = self.view.layer.timeOffset
        self.view.layer.speed = 1.0
        self.view.layer.timeOffset = 0.0
        self.view.layer.beginTime = 0.0
        let timeSincePause = self.view.layer.convertTime(CACurrentMediaTime(), toLayer: nil) - pauseTime
        self.view.layer.beginTime = timeSincePause
    }

 

你可能感兴趣的:(swift,CoreAnimation)