Progressview 在触摸 UIButton 时停止

Progressview stop when retouch the UIButton

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

我有一个下载按钮,点击它开始下载文件:

@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
}

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