我仍然对Swift相当新鲜。我想知道如何在执行函数之外的swift中终止动画功能。在下面的情况下,我运行动画15秒,但只要用户按下某个按钮,我希望动画停止,元素消失。关于最佳方式的任何想法?这是功能。
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
}
答案 0 :(得分:2)
动画实际上是由视图的图层处理的,因此您可以使用CALayer.removeAllAnimations()取消动画:
view.layer.removeAllAnimations()