ios - 对表格 View 单元格(包括标题)进行动画处理

标签 ios swift uitableview

我使用以下方法对表格 View 的单元格进行动画处理:

func animateCells(tableView: UITableView) {
    tableView.reloadData()

    let cells = tableView.visibleCells
    let tableHeight: CGFloat = tableView.bounds.size.height

    for i in cells {
        let cell: UITableViewCell = i as UITableViewCell
        cell.transform = CGAffineTransformMakeTranslation(0, tableHeight)
    }

    var index = 0

    for a in cells {
        let cell: UITableViewCell = a as UITableViewCell
        UIView.animateWithDuration(animationDuration, delay: animationDelay * Double(index),usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
            cell.transform = CGAffineTransformMakeTranslation(0, 0);
            }, completion: nil)

        index += 1
    }
}

这仅适用于表格 View 中的单元格。但我也有一些带有标题的表格,在这些情况下动画不起作用。如何在上面的动画代码中包含标题?谢谢。

最佳答案

解决了 tableView 部分标题动画的问题:

// animate section header
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let numberOfSections = tableView.numberOfSections
    let tableHeight: CGFloat = tableView.bounds.size.height
    var index = 0

    for i in 0...numberOfSections - 1 {
        if (section == i) {
            view.transform = CGAffineTransformMakeTranslation(0, tableHeight)
            UIView.animateWithDuration(1.0, delay: 0.05 * Double(index),usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: { () -> Void in
                view.transform = CGAffineTransformMakeTranslation(0, 0)
                }, completion: nil)
        }
        index += 1
    }
}

对于单元格:

// animate cells
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    let numberOfSections = tableView.numberOfSections
    let tableHeight: CGFloat = tableView.bounds.size.height
    var index = 0

    for i in 0...numberOfSections - 1 {
        let numberOfCells = tableView.numberOfRowsInSection(i)

        for j in 0...numberOfCells - 1 {
            if (indexPath.section == i && indexPath.row == j) {
                cell.transform = CGAffineTransformMakeTranslation(0, tableHeight)
                UIView.animateWithDuration(1.0, delay: 0.05 * Double(index),usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
                    cell.transform = CGAffineTransformMakeTranslation(0, 0);
                    }, completion: nil)
            }
            index += 1
        }
    }
}

感谢您的提示!!

关于ios - 对表格 View 单元格(包括标题)进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35582198/

相关文章:

swift - 在 Swift 中使用不带逗号、换行符和括号的可变参数打印字符串

ios - 一次加载 10 个单元格行 - swift

ios - UITableViewController selectionStyle 无不工作

iphone - UITableView 的委托(delegate)方法没有响应

ios - 如何在模拟器环境中使用 UIImagePickerController 的 CameraOverlayView 属性

ios - 无法在 iOS 上创建 400 多页的 PDF 文档

ios - 在 iOS 8 共享扩展和主应用程序之间共享数据

ios - 将两个 UIImage 并排保存为一个组合图像?

ios - 在 Swift 中更新 View

ios - Swift UITableView和PickerView/DatePicker