ios - UITableViewCell 动画只有一个方向

标签 ios swift uitableview

我只想在一个方向 [向下] 上设置 UITableViewCell 的动画

我的代码:

 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.alpha = 0
         let transform = CATransform3DTranslate(CATransform3DIdentity, 0, 200 , 0)
        cell.layer.transform = transform

        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
            cell.alpha = 1
            cell.layer.transform = CATransform3DIdentity
        })
    }

当用户向上滚动时,我如何停止该动画[不执行动画]?
谢谢!!

最佳答案

如果您只有一个部分,您可以简单地将当前 indexPath 与上次显示的那个进行比较,并且仅在新的 indexPath.row 出现时才设置动画更大:

fileprivate var lastIndexPath: IndexPath = IndexPath(row: -1, section: 0)

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

    // perform animation only when new indexPath.row is greater then the last presented
    if lastIndexPath.row < indexPath.row {
        cell.alpha = 0
        let transform = CATransform3DTranslate(CATransform3DIdentity, 0, 200 , 0)
        cell.layer.transform = transform

        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
            cell.alpha = 1
            cell.layer.transform = CATransform3DIdentity
        })
    }

    lastIndexPath = indexPath
}

如果是多个部分,方法是一样的,只是比较会稍微复杂一些,因为你必须考虑部分。所以不是:

if lastIndexPath.row < indexPath.row {

你必须使用类似的东西:

if lastIndexPath.section < indexPath.section ||
   (lastIndexPath.section == indexPath.section && lastIndexPath.row < indexPath.row) {

关于ios - UITableViewCell 动画只有一个方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49555223/

相关文章:

ios - 仅在发布版本中,clang : error: no such file or directory: '/Users/vagrant/Library/Developer/Xcode/DerivedData

ios - 如何将 "Open In"按钮添加到 iOS 8 共享扩展?

ios - 如何存档文本颜色为渐变的 UItextfield?

ios - 使用默认后退按钮时 Unwind 方法不起作用

iphone - 如何捕捉按下 TableViewCell 的事件?

ios - 我们可以从 WatchOS 2 扩展调用 IBM worklight 7 适配器​​吗?

ios - 如何使用依赖的 Firebase 创建 Pod 框架

ios - UILabel 找到适合宽度的高度

ios - 未使用 reloadData() 方法调用 cellForRowAt indexPath

ios - 查看层次结构约束错误