【问题标题】:Inserting row after another row deletion animation在另一行删除动画之后插入行
【发布时间】:2016-04-04 10:11:19
【问题描述】:

我正在尝试在另一行删除动画完成后插入一行。我一直在尝试执行以下操作:

tableView.beginUpdates()
CATransaction.begin()

CATransaction.setCompletionBlock {
    tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Right)
}

tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Left)

CATransaction.commit()
tableView.endUpdates

当行数与预期不同时,这给了我通常的断言失败。

然后我尝试使用带有完成块的UIView 动画:

tableView.beginUpdates()

func animations() {
    tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Left)
}

func completion() {
    if count == self.payments.count && self.payments.isEmpty { insert() }
}

UIView.animateWithDuration(0.5, animations: { animations() }) { _ in completion() }

tableView.endUpdates()

两次尝试都给了我同样的错误。是否有可能或者我应该研究用于插入/删除 tableview 行的自定义动画?


编辑:

我设法通过将tableView.endUpdates() 移动到完成块来使其工作。但是插入动画在删除行的同时仍然有动画。

还有其他方法吗?

【问题讨论】:

    标签: ios swift uitableview animation


    【解决方案1】:

    如果你知道你的动画需要多少时间来完成,只需添加这个函数来等待给定的时间,然后再执行一些代码:

    func delay(delay:Double, closure:()->()) {
        dispatch_after(
            dispatch_time(
                DISPATCH_TIME_NOW,
                Int64(delay * Double(NSEC_PER_SEC))
            ),
            dispatch_get_main_queue(), closure)
    }
    

    用法:

    delay(seconds: 0.5) { 
        //code to be delayed "0.5 sec"
    }
    

    【讨论】:

      【解决方案2】:

      我认为您将代码放在错误的位置

      应该是这样的:

      CATransaction.begin()
      
      CATransaction.setCompletionBlock {
        // animation has finished
      }
      
      tableView.beginUpdates()
      // do some work
      tableView endUpdates()
      
      CATransaction.commit()
      

      参考:How to detect that animation has ended on UITableView beginUpdates/endUpdates?

      【讨论】:

        猜你喜欢
        • 2016-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-14
        • 2018-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多