ここでは、Swiftで動作するiOSアプリケーションに関する私の状況があります。UIButtonのレタッチ時のProgressviewの停止
私はtouch上でファイルをダウンロードするために開始され、ダウンロードボタンがあります。
@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;
})
}
})
}
ボタンのタッチで、私は、ダウンロードが使用して起こっているどのくらい見に小さなアニメーションhapennignをしました:
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))
}
問題は私がダウンロードボタンをレタッチすると、アニメーションが停止します。それはそれがファイルをもうダウンロードしていないという気持ちをもたらします! (ただし、まだファイルをダウンロードしています)。
どうすればこの問題を修正できますか?私は理想的にボタンのレタッチでそれを望みます - 何も起こりませんし、進歩的な人のアニメーションは続けることを続ける...
おかげさしであなたの助けと時間を!
フラグを設定して、アニメーションが既に実行されているかどうかを確認する必要があります。それが本当であれば、関数は再び呼び出されるべきではありません。 – D3myon
またはダウンロード中にボタンを無効にする – Retterdesdialogs