2016-11-14 71 views
0

我有一个像Snapchat和Instagram故事一样的进度视图。进度视图到达最后或点击按钮后,我的内容会发生变化。重置UIProgressView并立即开始使用Swift 3

我正在重置内容更改时的进度视图。一切都按预期工作,而没有干预。但是,当点击下一个按钮时,进度视图不会再次开始,直到其他循环执行。您可以快速看到video here

我碰到this question出来,而我研究,我也有同样的情况,但我无法适用的原则与SWIFT 3.

这里一个新手是我的代码,任何帮助,将不胜感激:

func startTimer(){ 
    self.timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(myVievController.nextItem), userInfo: nil, repeats: false) 
} 

func updateProgressBar() { 
    self.progressView.setProgress(1.0, animated: false) 
    UIView.animate(withDuration: 2.8, animations: {() -> Void in 
     self.progressView.layoutIfNeeded() 
    }, completion: { finished in 
     if finished { 
      self.progressView.setProgress(0, animated: false) 
     } 
    }) 
} 

func nextItem(){ 
    // ... 
    self.updateUI(item: self.myCurrentItem) 
    // ... 
} 

func updateUI(item:MyItem){ 
    self.updateProgressBar() 
    self.timer.invalidate() 
    self.startTimer() 

    // Clear old item and fill new values etc... 
} 

@IBAction func nextButtonPressed(_ sender: UIButton) { 
    self.nextItem() 
} 

回答

0

你可能需要这样的更新您的进度条:self.progressView.setProgress(0, animated: false)

func updateProgressBar() { 
    DispatchQueue.main.async { 
     self.progressView.setProgress(0.1, animated: false) 
    } 
    if self.progressView.Progress == 1.0 { 
     self.timer.invalidate() 
    } else { 
     self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(myVievController.updateProgressBar), userInfo: nil, repeats: false) 
    } 
} 

然后你就可以invalidat E中的计时器和停止更新,当你在100%

+0

谢谢。我试过你的解决方案,但它不能帮助我。我刚刚编辑了问题以添加视频。也许它可能更有帮助。 –

+0

尝试在“updateUI”内调用“self.updateProgressBar()” –

+0

这不是已经存在吗? –

0

也可以用做到这一点:

class HelloWorld: UIProgressView { 

    func startProgressing(duration: TimeInterval, resetProgress: Bool, completion: @escaping (Void) -> Void) { 
     stopProgressing() 

     // Reset to 0 
     progress = 0.0 
     layoutIfNeeded() 

     // Set the 'destination' progress 
     progress = 1.0 

     // Animate the progress 
     UIView.animate(withDuration: duration, animations: { 
      self.layoutIfNeeded() 

     }) { finished in 
      // Remove this guard-block, if you want the completion to be called all the time - even when the progression was interrupted 
      guard finished else { return } 

      if resetProgress { self.progress = 0.0 } 

      completion() 
     } 
    } 

    func stopProgressing() { 
     // Because the 'track' layer has animations on it, we'll try to remove them 
     layer.sublayers?.forEach { $0.removeAllAnimations() } 
    } 
} 

可以通过调用-startProgressing()时,你永远需要重新启动progressView使用从头开始。该完成被调用的时候它有完成动画,这样你就可以改变看法等

使用的例子:

progressView.startProgressing(duration: 5.0, resetProgress: true) { 
    // Set the next things you need... change to a next item etc. 
} 
+0

谢谢。我按照你的建议改变了我的代码。这改善了我的结构,但现在进度条加载一次,并且不会重置。 :( –

+0

@MertDiricanlı我更新了代码,所以你可以做到这一点,它也可在https://github.com/doofyus/animatingProgressView –

+0

我得到了与我的第一个代码相同的结果。程序员,我不知道,也许我做错了,已经差不多3个小时了,但是我找不到新的解决方案,在我没有点击下一个按钮时效果很好,但是当我点击它时,进度没有开始,它在3秒后重新开始 您是否在这种情况下测试了您的代码?非常感谢您的帮助! –