OStack程序员社区-中国程序员成长平台

标题: ios - swift:文件解压缩时显示 UIActivityIndi​​catorView [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:25
标题: ios - swift:文件解压缩时显示 UIActivityIndi​​catorView

我使用此代码下载我的文件并在我的标签中显示下载进度。

我的代码:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MasterViewCell

    let cellFilePath = "\(indexPath.section)\(indexPath.row).zip"
    let indexOfTask = allDownloadTasks.index { (task:URLSessionDownloadTask) -> Bool in
        return task.currentRequest?.url?.lastPathComponent == cellFilePath
    }

    if indexOfTask == nil {

        //cell.label?.isHidden = true
    }
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

    let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
    let documentDirectoryPath:String = path[0]
    let fileManager = FileManager()
    let destinationURLForFile = URL(fileURLWithPath: documentDirectoryPath.appendingFormat("/file.png"))

     if fileManager.fileExists(atPath: destinationURLForFile.path){
         animation()
     } else {

         let url = URL(string: "link")!
         let downloadTaskLocal = self.backgroundSession.downloadTask(with: url)
         self.allDownloadTasks.append(downloadTaskLocal) // Add a new task to the array
         downloadTaskLocal.resume()

         cell.label?.frame = CGRect(x: 70, y: 128, width: 82, height: 21)
         cell.label?.isHidden = false

     }
}

func urlSession(_ session: URLSession,
                    downloadTask: URLSessionDownloadTask,
                    didWriteData bytesWritten: Int64,
                    totalBytesWritten: Int64,
                    totalBytesExpectedToWrite: Int64){

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

            if let visibleIndexPath = self.collectionView?.indexPathsForVisibleItems {
                for visibleIndexPath in visibleIndexPath {
                    if (downloadTask.currentRequest?.url?.lastPathComponent == "\(visibleIndexPath.section)\(visibleIndexPath.row).zip") {

                        var myCell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: visibleIndexPath) as! MasterViewCell
                        myCell = self.collectionView?.cellForItem(at: visibleIndexPath) as! MasterViewCell

                        myCell.label.text = "\(Int(CGFloat(totalBytesWritten) / CGFloat(totalBytesExpectedToWrite) * 100.0))%"

                    if myCell.label?.text == "100%" {
                        myCell.label?.isHidden = true
                        myCell.activityIndicator?.isHidden = true
                        myCell.activityIndicator?.startAnimating()
                    }

                    }
                }
            }
        })
    }

我有这个代码来解压缩我的文件:

func urlSession(_ session: URLSession,
                downloadTask: URLSessionDownloadTask,
                didFinishDownloadingTo location: URL){

    let fileName = downloadTask.originalRequest?.url?.lastPathComponent
    let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
    let documentDirectoryPath:String = path[0]
    let fileManager = FileManager()
    let destinationURLForFile = URL(fileURLWithPath: documentDirectoryPath.appendingFormat("/\(String(describing: fileName!))"))

    do {
        try fileManager.moveItem(at: location, to: destinationURLForFile)
    }catch{
        print("error")
    }

    let indexOfComplatedTask = allDownloadTasks.index(of: downloadTask)
    if indexOfComplatedTask != nil {

        SSZipArchive.unzipFile(atPath: documentDirectoryPath.appendingFormat("/\(String(describing: fileName!))"), toDestination:documentDirectoryPath, delegate:self)

        do {
            try fileManager.removeItem(atPath: documentDirectoryPath.appendingFormat("/\(String(describing: fileName!))"))
        }
        catch let error as NSError {
            print("Ooops! Something went wrong: \(error)")
        }

    }
}

我想在文件解压缩时显示我的 activityIndi​​cator 动画,并在文件停止解压缩后停止动画并删除它。我还在 Storyboard 中创建标签和事件指示器。

怎么做??????



Best Answer-推荐答案


检查下面的代码。

do {
        try fileManager.moveItem(at: location, to: destinationURLForFile)
    }catch{
        print("error")
    }

    let indexOfComplatedTask = allDownloadTasks.index(of: downloadTask)
    if indexOfComplatedTask != nil {
    **// Start animating your activityIndicator here.
    // Use unzipFile API having completion block callback
    // In the completion callback stop your activityIndicator**
        SSZipArchive.unzipFile(atPath: documentDirectoryPath.appendingFormat("/\(String(describing: fileName!))"), toDestination:documentDirectoryPath, delegate:self)

如果有任何疑问,请告诉我。

关于ios - swift:文件解压缩时显示 UIActivityIndi​​catorView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51629230/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4