我设置了这段代码,以便它在显示时调用 tableview 单元格并为其设置动画。我如何设置它,以便它只对出现在 View 初始加载时的单元格进行动画处理?
提前致谢...
var preventAnimation = Set<NSIndexPath>()
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
//
if !preventAnimation.contains(indexPath) {
preventAnimation.insert(indexPath)
let rotationTransform = CATransform3DTranslate(CATransform3DIdentity, 0, +600, 0)
cell.layer.transform = rotationTransform
UIView.animateWithDuration(0.6, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: .CurveEaseOut, animations: {
cell.layer.transform = CATransform3DIdentity
}, completion: nil)
}
}
最佳答案
您可以只使用一个变量maxCellIndexAppeared
重新编码已出现的单元格的最大indexPath.row
,这比使用一个集合包含所有indexPath 更好。
在initialize方法中将maxCellIndexAppeared
设置为零,然后将其与单元格显示时的indexPath.row
进行比较,如果当前单元格indexPath大于该变量则做你的动画,否则返回。
就像这样:
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if maxCellIndexAppeared > indexPath.row {
return
}
//here reset the current maximum index
maxCellIndexAppeared = indexPath.row
//And then do your animation
....do your animation.....
}
关于ios - 在加载时在 Swift 中为 TableView 单元格设置动画(Up into TableView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37473890/