如何从屏幕底部动画插入部分

How to animate insert sections from bottom of screen

我需要在 UITableView 中做一个动画来插入部分,但我需要这些部分从屏幕底部开始动画。

而且不像默认的 UITableViewRowAnimation 动画之一。

这是我需要的动画:

有什么建议吗?

谢谢

怎么样

  • 创建一个新的 table,将其添加为屏幕底部的子视图 并调用 reloadData 仅使用第二个数据来填充它 部分。
  • 将这个新的 table 动画化到现有位置的下方 部分。
  • 新部分就位后,重新加载包含所有数据的主 table,然后从其超级视图中删除第二个 table。

我想出了一个简单的解决方案。

public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    if shouldAnimate {
        cell.alpha = 0
        let transform = CATransform3DTranslate(CATransform3DIdentity, 0, UIScreen.main.bounds.height, 0)
        cell.layer.transform = transform

        UIView.animate(withDuration: 0.5, animations: {
            cell.alpha = 1
            cell.layer.transform = CATransform3DIdentity
        })
    }
}

public func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if shouldAnimate {
        view.alpha = 0
        let transform = CATransform3DTranslate(CATransform3DIdentity, 0, UIScreen.main.bounds.height, 0)
        view.layer.transform = transform

        UIView.animate(withDuration: 0.5, animations: {
            view.alpha = 1
            view.layer.transform = CATransform3DIdentity
        })
    }
}

public func addSections(sections: IndexSet) {
    shouldAnimate = true
    CATransaction.begin()
    CATransaction.setCompletionBlock({ self.shouldAnimate = false })

    tableView.beginUpdates()
    tableView.insertSections(sections, with: .top)
    tableView.endUpdates()

    tableView.scrollToRow(at: IndexPath(row: 0, section: 1), at: .top, animated: true)

    CATransaction.commit()
}

所以willDisplay cell/header从屏幕底部处理动画。

shouldAnimate 完成 insertSection

后取消单元格动画