0
我對Swift仍然很陌生。我想知道如何在函數執行之外快速終止動畫函數。在下面的情況下,我運行一個動畫15秒,但任何時候當用戶按下某個按鈕時,我希望動畫停止並且元素消失。任何關於這個最好的方法的想法?這是功能。Swift:如何在函數執行之外終止一個函數
func createTimerAnimation() {
// create timer line shapes
guard self.timerisActive else {
print("in guard 1")
return
}
let screenWidth = (self.view.bounds.width)
let screenHeight = (self.view.bounds.height)
let timerShapeLeft = drawTimerShape()
let timerShapeRight = drawTimerShape()
let timerShapeCenter = drawTimerShape()
view.addSubview(timerShapeLeft)
view.addSubview(timerShapeRight)
view.addSubview(timerShapeCenter)
UIView.animate(withDuration: 14.5, delay: 0.0, options: [.curveEaseOut], animations:{
timerShapeLeft.frame = CGRect (x: (screenWidth/2), y: (screenHeight - 60), width: timerShapeLeft.frame.width, height: (timerShapeLeft.frame.height - 30))
timerShapeRight.frame = CGRect (x: (screenWidth/2), y: (screenHeight - 60), width: timerShapeRight.frame.width, height: (timerShapeRight.frame.height - 30))
timerShapeCenter.frame = CGRect (x: (screenWidth/2), y: (screenHeight - 60), width: timerShapeCenter.frame.width, height: (timerShapeCenter.frame.height))
timerShapeLeft.alpha = 1.0
timerShapeRight.alpha = 1.0
timerShapeCenter.alpha = 0.4
timerShapeLeft.tintColor = self.hexGray
timerShapeRight.tintColor = self.hexGray
guard self.timerisActive else {
print("in guard 1")
return
}