我使用以下方法为表格 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/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |