代码之家  ›  专栏  ›  技术社区  ›  Coder221

为多个原型TableView单元格设置动画

  •  0
  • Coder221  · 技术社区  · 6 年前

    我有两个原型电池 myTableView ,即 myTableViewCell myHeaderCell . 我正在使用 willDisplay cell 动画制作方法如下

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let rotationTransform = CATransform3DTranslate(CATransform3DIdentity, -500, 10, 0)
        cell.layer.transform = rotationTransform
    
        UIView.animate(withDuration: 1.0, animations : { () -> Void in
            cell.layer.transform = CATransform3DIdentity
        })
    }
    

    但只有 我的表视图单元格 正在制作动画,但没有 我的校长 . 如何使两个原型单元同时动画?

    我在willDisplayHeaderView方法中添加了以下代码,但仍然不起作用

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    
        let cell = tableView.dequeueReusableCell(withIdentifier: "myHeaderCell") as! HeaderCell
        let rotationTransform = CATransform3DTranslate(CATransform3DIdentity, -500, 10, 0)
        cell.layer.transform = rotationTransform
        UIView.animate(withDuration: 1.0, animations : {() -> Void in
            cell.layer.transform = CATransform3DIdentity
        })
    }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Kamran    6 年前

    你可以将动画代码包装在一个方法中,

    private func animate(view: UIView) {
       let rotationTransform = CATransform3DTranslate(CATransform3DIdentity, -500, 10, 0)
        view.layer.transform = rotationTransform
        UIView.animate(withDuration: 1.0, animations : {() -> Void in
            view.layer.transform = CATransform3DIdentity
        })
    }
    

    然后你可以通过任何你想要应用这个动画效果的视图。例如,在上面的例子中,您希望动画 cell table 所以你只需要打电话给 animate 方法。

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        self.animate(view: cell)
    }
    
    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
       self.animate(view: view)
    }
    

    记得 UITableViewCell TableView 标题都是 UIView 所以 使有生气 方法将按预期工作。

    从来没有 deque... 任何 细胞 section 收割台内侧 willDisplay 委托方法。顾名思义,单元格/标题只是要显示,它已经创建/取消,所以您不需要再次创建它。

        2
  •  1
  •   Andrew James Ramirez    6 年前

    那应该管用。我唯一能想到的就是你 myHeaderCell 未设置为节的单元格。

    但是一个头像/细胞。

    如果你使用 我的校长 在这里:

    tableView(_:viewForHeaderInSection:)
    

    然后你也应该把你的动画代码

    tableView(_:willDisplayHeaderView:forSection:)
    

    单元格的头委托方法不同。

    编辑:在您编辑的问题中,您不需要将任何内容出列。我很惊讶没有出错。

    您已经通过委托保留了视图头实例。

    直接使用视图实例。