修饰UIButton时进度停止

时间:2017-05-19 13:20:02

标签: ios swift uibutton uiprogressview

以下是关于使用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))
    }

问题是如果我修饰下载按钮,动画停止!所以它带来的感觉是它不再下载文件了! (但它仍在下载文件)。

怎么能修好?理想情况下,我想要修饰按钮 - 没有任何事情发生,渐进式动画继续... ...

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

1 个答案:

答案 0 :(得分:0)

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

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

至于我,这是最简单的方法。