gpt4 book ai didi

ios - 重置 UIProgressView 并立即开始使用 Swift 3 制作动画

转载 作者:行者123 更新时间:2023-11-28 08:23:56 24 4
gpt4 key购买 nike

我有一个进度 View ,就像 Snapchat 和 Instagram Stories 一样。在进度 View 到达末尾或点击按钮时,我的内容会发生变化。

当内容更改时,我正在重置进度 View 。在没有干预的情况下,一切都按预期进行。但是当点击下一步按钮时,进度 View 不会再次启动,直到另一个循环执行。

你可以看到 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()
}

最佳答案

也可以用子类来实现:

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() }
}
}

当您需要从头开始再次启动 progressView 时,可以通过调用 -startProgressing() 来使用它。 completion完成 动画时调用,因此您可以更改 View 等。

使用示例:

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

关于ios - 重置 UIProgressView 并立即开始使用 Swift 3 制作动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40588879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com