【问题标题】:UITableViewCell animation in only one directionUITableViewCell 动画只有一个方向
【发布时间】:2018-03-29 11:45:42
【问题描述】:

我只想在一个方向[向下]为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
        })
    }

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

【问题讨论】:

标签: ios swift uitableview


【解决方案1】:

如果您只有一个部分,您可以简单地将当前的 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) {

【讨论】:

    猜你喜欢
    • 2018-08-20
    • 1970-01-01
    • 2012-06-15
    • 2014-11-14
    • 2019-11-02
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多