我使用CAShapeLayer创建了一个循环进度活动视图,并将strokeEnd设置为0.3,并使用CABasicAnimation修改了strokeEnd。动画结束后,strokeEnd变为1,这是一个完整的圆,但是如果我按viewcontroller或pop viewcontroller,它将返回到圆的初始状态。当视图弹出或推入时,如何让strokeEnd保持为1?
https://giphy.com/gifs/JoyiYVXdiGO1drm1zJ
func animation() {
let basicanimation = CABasicAnimation(keyPath: "transform.rotation")
basicanimation.toValue = 2*CGFloat.pi
basicanimation.fromValue = 0
basicanimation.duration = 1
basicanimation.repeatCount = Float.infinity
basicanimation.fillMode = CAMediaTimingFillMode.forwards
basicanimation.isRemovedOnCompletion = false
//basicanimation.isAdditive = true
tracklayer2.add(basicanimation, forKey: "basic")
}
func startanimation() {
if connected {
self.cir.startstoplabel.text = "Start"
self.connected = false
self.cir.tracklayer2.isHidden = true
self.cir.tracklayer.isHidden = false
self.cir.tracklayer3.isHidden = true
}
else {
tapgesture.isEnabled = false
cir.startstoplabel.text = "Connecting..."
cir.tracklayer2.strokeEnd = 0.3
cir.tracklayer3.isHidden = true
self.cir.shapeLayer.isHidden = true
self.cir.tracklayer2.isHidden = false
self.cir.tracklayer.isHidden = true
self.cir.animation()
let url = URL(string: "")
var request = URLRequest(url: url!)
let auth = Auth.auth().currentUser?.uid
request.addValue(auth!, forHTTPHeaderField: "")
request.addValue("application/json",forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let parameter:[String:Int] = ["":1]
let jsondata = try? JSONSerialization.data(withJSONObject: parameter)
request.httpBody = jsondata
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error{
print(error)
return
}
let json = try? JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any]
print(json)
self.connected = true
DispatchQueue.main.async {
self.cir.startstoplabel.font = UIFont(name: "HelveticaNeue", size: 28)
self.cir.tracklayer3.removeAllAnimations()
self.cir.tracklayer2.strokeEnd = 1
self.cir.tracklayer2.isHidden = true
self.cir.tracklayer3.isHidden = false
self.cir.shapeLayer.isHidden = false
self.tapgesture.isEnabled = true
}
}
task.resume()
}
}