• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - swift:文件解压缩时显示 UIActivityIndi​​catorView

[复制链接]
菜鸟教程小白 发表于 2022-12-11 20:25:11 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

我的代码:

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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap