菜鸟教程小白 发表于 2022-12-11 20:25:11

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


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

<p>我的代码:</p>

<pre><code>func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: &#34;Cell&#34;, for: indexPath) as! MasterViewCell

    let cellFilePath = &#34;\(indexPath.section)\(indexPath.row).zip&#34;
    let indexOfTask = allDownloadTasks.index { (task:URLSessionDownloadTask) -&gt; 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
    let fileManager = FileManager()
    let destinationURLForFile = URL(fileURLWithPath: documentDirectoryPath.appendingFormat(&#34;/file.png&#34;))

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

         let url = URL(string: &#34;link&#34;)!
         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: {() -&gt; Void in

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

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

                        myCell.label.text = &#34;\(Int(CGFloat(totalBytesWritten) / CGFloat(totalBytesExpectedToWrite) * 100.0))%&#34;

                  if myCell.label?.text == &#34;100%&#34; {
                        myCell.label?.isHidden = true
                        myCell.activityIndicator?.isHidden = true
                        myCell.activityIndicator?.startAnimating()
                  }

                  }
                }
            }
      })
    }
</code></pre>

<p>我有这个代码来解压缩我的文件:</p>

<pre><code>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
    let fileManager = FileManager()
    let destinationURLForFile = URL(fileURLWithPath: documentDirectoryPath.appendingFormat(&#34;/\(String(describing: fileName!))&#34;))

    do {
      try fileManager.moveItem(at: location, to: destinationURLForFile)
    }catch{
      print(&#34;error&#34;)
    }

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

      SSZipArchive.unzipFile(atPath: documentDirectoryPath.appendingFormat(&#34;/\(String(describing: fileName!))&#34;), toDestination:documentDirectoryPath, delegate:self)

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

    }
}
</code></pre>

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

<p>怎么做??????</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>检查下面的代码。</p>

<pre><code>do {
      try fileManager.moveItem(at: location, to: destinationURLForFile)
    }catch{
      print(&#34;error&#34;)
    }

    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(&#34;/\(String(describing: fileName!))&#34;), toDestination:documentDirectoryPath, delegate:self)
</code></pre>

<p>如果有任何疑问,请告诉我。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - swift:文件解压缩时显示 UIActivityIndi​​catorView,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/51629230/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/51629230/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - swift:文件解压缩时显示 UIActivityIndi​​catorView