Ios 使用执行功能和UI更新重复动画

Ios 使用执行功能和UI更新重复动画,ios,swift,function,animation,uiviewanimation,Ios,Swift,Function,Animation,Uiviewanimation,我正在做一个类似冒险资本家的游戏,我遇到了这个问题 我有一个标签中只有文本,这个标签永远不会改变。 我有一个与前一个相同原点的动画标签,按下按钮时,该标签作为加载条进行动画。 我有一个标签,在执行函数时显示总和。 我有一个按钮,按下它会执行动画。 我有一个对两个数字求和的函数。 我有一个函数,可以更新显示上一个函数和的标签。 我的问题是,当我让我的代码只在一切按计划运行时执行它,但当我试图使它成为一个无限循环时,它将不起作用 func animate(){ UIView.animate

我正在做一个类似冒险资本家的游戏,我遇到了这个问题

我有一个标签中只有文本,这个标签永远不会改变。
我有一个与前一个相同原点的动画标签,按下按钮时,该标签作为加载条进行动画。
我有一个标签,在执行函数时显示总和。
我有一个按钮,按下它会执行动画。
我有一个对两个数字求和的函数。
我有一个函数,可以更新显示上一个函数和的标签。
我的问题是,当我让我的代码只在一切按计划运行时执行它,但当我试图使它成为一个无限循环时,它将不起作用

func animate(){
     UIView.animate(withDuration: 3, animation{
              self.animated.frame.size.width += 200
              self.animated.backgroundColor = UIColor(red: 0, green: 1, blue: 0, alpha: 0.5)
             }, completion: {
            (end: Bool)->Void in
            self.animated.frame.size.width = 0
            self.sumFunc()
            self.updateLabel()
            })
这段代码工作得很好,但当我尝试将它变成这样一个无限循环时,它就不起作用了

func animateInfinite(){
     UIView.animate(withDuration: 3, delay: 0, options: .repeat, animations: {
      self.animated.frame.size.width += 200
      self.animated.backgroundColor = UIColor(red: 0, green: 1, blue: 0, alpha: 0.5)
      // when I put sumFunc here and updateFunc here, the app breaks, and will not compile
     }, completion: {
            (end: Bool)->Void in
        // when I put sumFunc here and updateFunc here, the app runs, but it never executes, because I have noticed that .repeat animation never has a completion.

我也尝试过让任务在不同的线程中执行,但到目前为止我还没有成功。

非常感谢,我还没有想过在比赛中调用startAnimation()函数!再次感谢你。
class YourClass  {

 var isAnimationStop = false

 override func viewDidAppear(_ animated: Bool) {
  super.viewDidAppear(animated)
  startanimation()
 }

 func startanimation(){
   UIView.animate(withDuration: 2, animations: {
   self.animated.frame.size.width += 10
   self.animated.backgroundColor = UIColor(red: 0, green: 1, blue: 0, alpha: 0.5)
   }, completion: {_ in
   self.animated.frame.size.width = 0
   if !self.isAnimationStop{
     self. startanimation()
     self.sumFunc()
     self.updateLabel()
   }
  })
 }

// call stop when you want
 func stopAnimation()  {
  self.isAnimationStop = true
 }
}