【问题标题】:How do I use placeholder API on UIElements correctly如何在 UIElements 上正确使用占位符 API
【发布时间】:2018-12-27 00:20:09
【问题描述】:

我正在我的 Xcode 项目中实现一个名为 Skeleton View 的 API。有时,当应用程序第一次加载时,加载 UI 元素需要更长的时间,因为数据来自 JSON,如果用户使用 4G 甚至是糟糕的互联网,将仍然是一个空白字段。此外,使用此 API,它在每个未接收数据的 UIElement 上显示为灰色视图动画占位符。

但是,我不知道如何检查 UIImage 何时接收到能够移除骨架效果并呈现图像的值。我会在下面发布一些图片。

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cellID") as! TableViewCell

            let model = arrCerveja[indexPath.row]

            cell.labelName.text = model.name
            cell.labelDetail.text = "Teor alcoólico: \(model.abv)"
            let resource = ImageResource(downloadURL: URL(string: "\(model.image_url)")!, cacheKey: model.image_url)

            if cell.imageViewCell.image != nil {
                cell.imageViewCell.kf.setImage(with: resource)
                cell.imageViewCell.hideSkeleton()
            }else{
               cell.imageViewCell.showAnimatedGradientSkeleton()

//The placeholder still there even when the images already got downloaded
//What I wanted was, while the UIImageView has no value, the placeholder 
//persists, but when receive the image the placeholder should be dismissed.
            }

            return cell
        }

这就是我在 UIImages 上得到的(它有一个模糊动画经过):

我面临的问题是如何在每个图像加载后消除这种效果?

【问题讨论】:

  • 如果您要复制自己的问题stackoverflow.com/questions/51411101/…,请删除其中一个。
  • 我刚刚删除了另一个问题,感谢您记住我。兄弟,你能给我一个解决方案吗?

标签: ios swift xcode placeholder


【解决方案1】:

您需要在开始下载时启动动画(通常在您获取数据的函数之前):

view.startSkeletonAnimation()

这里的 view 属性是您的视图控制器之一,SkeletonView 是递归的,它将为其中的所有视图设置动画。

然后在下载完成后(通常在您重新加载表格视图之前关闭,我假设您的所有图像都已下载并准备好显示)停止动画:

self.view.stopSkeletonAnimation()

骨架视图还为您提供 UITableView 的委托:

public protocol SkeletonTableViewDataSource: UITableViewDataSource {
    func numSections(in collectionSkeletonView: UITableView) -> Int
    func collectionSkeletonView(_ skeletonView: UITableView, numberOfRowsInSection section: Int) -> Int
    func collectionSkeletonView(_ skeletonView: UITableView, cellIdenfierForRowAt indexPath: IndexPath) -> ReusableCellIdentifier
}

正如documentation 所说(阅读有关集合的部分以了解其工作原理):

此协议继承自 UITableViewDataSource,因此您可以将此协议替换为框架协议。

您需要在 viewDidLoad() 函数中使您的 tableView 骨架可运行:

tableView.isSkeletonable = true

【讨论】:

  • 您好,谢谢您的帮助。好吧,我尝试实现这些方法,但我应该从我的应用程序中删除标准 UITableView 方法吗?
  • 是的,删除与骨架等效的那些并保留 tableView(_:cellForRowAt:) 以使用您的数据填充您的单元格。 Skeleton 协议扩展了 Apple 提供的协议,因此它们只是添加了功能。我可以建议你在 SkeletonView Github 上下载 repo,然后打开 ViewController.swift 并尝试了解它是如何工作的,它很简单,将帮助你在你的应用程序中实现它。
  • 我尝试实现返回单元格标识符的 Skeleton 方法,但是我收到了以下消息:Type 'ViewController' does not conform to protocol 'UITableViewDataSource'
  • 您需要通过在类声明期间添加“ViewController: SkeletonTableViewDataSource”并添加所有必需的方法来使您的视图控制器符合骨架协议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-12
  • 2013-07-18
  • 2019-08-16
  • 2019-02-20
  • 2014-03-29
  • 2019-04-07
相关资源
最近更新 更多