2016-12-30 151 views
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 
      } 

回答

相关问题