gpt4 book ai didi

ios - 修饰 UIButton 时进度 View 停止

转载 作者:行者123 更新时间:2023-11-30 12:26:54 28 4
gpt4 key购买 nike

这是我关于使用 Swift 运行的 iOS 应用程序的情况。

我有一个下载按钮,触摸后即可开始下载文件:

@IBAction func downloadButtonPressed() {
if let downloadTask = downloadTask {
downloadTask.cancel()
statusLabel.text = "HELLO"


} else {
statusLabel.text = "Downloading video"
downloadButton.setTitle("Stop download", for: UIControlState())
createDownloadTask()

let urlString = "\(posts[selectedIndexPath].link)"

DispatchQueue.global(qos: .default).async(execute: {
//All stuff here

print("downloadVideo");
let url=NSURL(string: urlString);
let urlData=NSData(contentsOf: url! as URL);

if((urlData) != nil)
{
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

let fileName = urlString as NSString;

let filePath="\(documentsPath)/\(fileName.lastPathComponent)";

DispatchQueue.main.async(execute: { () -> Void in

print(filePath)
urlData?.write(toFile: filePath, atomically: true);
print("video Saved to document directory of app");
self.downloadButton.alpha = 0;
self.progressView.alpha = 0;
self.cardboardButton.alpha = 1;
self.videoButton.alpha = 1;
})
}
})


}

触摸按钮时,我会显示一个小动画,以查看下载进行了多远:

 func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
progressView.animateProgressViewToProgress(progress)
progressView.updateProgressViewLabelWithProgress(progress * 100)
// progressView.updateProgressViewWith(Float(totalBytesWritten), totalFileSize: Float(totalBytesExpectedToWrite))
}

问题是如果我修改下载按钮,动画就会停止!所以它给人一种不再下载文件的感觉! (但它仍在下载文件)。

如何解决这个问题?理想情况下,我希望在修饰按钮时 - 什么也没有发生,并且渐进式动画继续进行......

非常感谢您的帮助和时间!

最佳答案

您可以在用户触摸按钮后设置 button.isUserInteractionEnabled = false 并在下载结束后通过设置恢复它:

DispatchQueue.main.async { 
button.isUserInteractionEnabled = true
}

对我来说这是最简单的方法。

关于ios - 修饰 UIButton 时进度 View 停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44071235/

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