在加载时在Swift(Up into TableView)中动画化TableView单元格

时间:2016-05-27 02:30:19

标签: ios swift uitableview

我设置了这个代码,以便在显示时调用tableview单元格并为其设置动画。我怎么能设置它以便它只动画在视图的初始加载时出现的单元格?

提前致谢...

  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)
    }
}

2 个答案:

答案 0 :(得分:2)

您可以使用变量maxCellIndexAppeared重新编码已出现的单元格的最大indexPath.row,这比使用包含所有indexPath的集合更好。

在initialize方法中将maxCellIndexAppeared设置为零,然后在单元格显示时将其与indexPath.row进行比较,如果当前单元格索引路径大于变量,则以其他方式返回。

就像这样:

    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.....
   }

答案 1 :(得分:0)

//Declare Bool array which will keep track of the rows which are animated 
var animationShown : [Bool]? 


// Initalize animationShown array in your viewDidLoad 
self.animationShown = [Bool] (repeating: false, count: YourTableViewRowsCount)


// MARK: - Animate Tableview Cell
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
{
if self.animationShown?[indexPath.row] == false
{
//Transform Animation
let transform = CATransform3DTranslate(CATransform3DIdentity, -200, 0, 0)
cell.layer.transform = transform
UIView.animate(withDuration: 0.5, animations:
{
cell.layer.transform = CATransform3DIdentity
})
self.animationShown?[indexPath.row] = true
}
}

Hope this helps!! Happy Coding...