我想仅在一个方向[向下]
为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
})
}
当用户向上滚动时,如何停止该动画[不执行动画]? 谢谢!
答案 0 :(得分: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) {