我只想在一个方向 [向下] 上设置 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
})
}
当用户向上滚动时,我如何停止该动画[不执行动画]?
谢谢!!
最佳答案
如果您只有一个部分,您可以简单地将当前 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) {
关于ios - UITableViewCell 动画只有一个方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49555223/