【问题标题】:Animate tableview cell when deleting删除时动画表格视图单元格
【发布时间】:2019-10-13 05:25:06
【问题描述】:

我有一个表格视图。 tableview 单元格包含图像、标签。在这个单元格中,我有一个删除该行的按钮。我想在删除过程中为单元格设置动画(例如向左或向右滑动等火种)或任何动画以使应用程序变得活跃。如何制作这种类型的动画。或者有什么推荐的图书馆?

【问题讨论】:

  • 通常,您只需从模型中删除与该行关联的对象,然后调用tableView.deleteRows(at:with:),提供被删除行的IndexPath 和所需的动画,这将产生动画删除行。
  • 我可以删除带有像 tinder 这样的动画的行吗?

标签: swift uitableview animation


【解决方案1】:

UITableViewCellEditingStyle 将为您完成这项工作,即

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        //do your necessary operations here
        //reload your UITableView here
    }
}

从右向左滑动查看动画。

【讨论】:

    【解决方案2】:

    您的案例的典型动画:

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
    
            // your code
    
            tableView.deleteRows(at: [indexPath], with: .automatic)
    
           // your code
    
        }
    }
    

    自定义动画的替代方式(更新)

    tableView.beginUpdates()
    tableView.deleteRows(at: [indexPath], with: .top) // Check (.)after for available animation example comment   out below -
    //tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], with: .left)
    //tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], with: .right)
    //tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], with: .up)
    //tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], with: .down)
    //tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], with: .fade)
    //tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], with: .middle)
    //tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], with: .bottom)
    tableView.endUpdates()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      相关资源
      最近更新 更多