如何为行的编辑操作设置动画在IndexPath处显示用户操作

时间:2016-06-26 13:41:40

标签: swift uitableview uitableviewrowaction

我看到一些应用程序在用户加载tableView时实现了动画,这些应用程序显示了它们可以在单元格上向左滑动。它显示正在滑动的单元格然后快速关闭,以便用户可以按照自己的意愿行事。

我搜索了一些预先存在的库,但没有找到任何库。

我可以以编程方式刷细胞吗?因此在viewDidLoad(或出现)上调用它?

2 个答案:

答案 0 :(得分:1)

在Swift中,您可以执行类似的操作,但是,这并未显示通常通过在UITableViewcell上向左滑动显示的基础编辑操作。它仅动画滑动动作

fileprivate func showSwipeAction() 
{
    let animation = CABasicAnimation(keyPath: "position")
    animation.duration = 0.08
    animation.repeatCount = 1
    animation.autoreverses = true
    animation.speed = 0.30
    let fromPoint:CGPoint = CGPoint(x: self.center.x, y: self.center.y)
    animation.fromValue = NSValue(cgPoint: fromPoint)
    let toPoint:CGPoint = CGPoint(x: self.center.x - 50, y: self.center.y)
    animation.toValue = NSValue(cgPoint: toPoint)
    self.setEditing(true, animated: true)
    self.layer.add(animation, forKey: "position")
}

答案 1 :(得分:1)

我绝对是第二个在我面前的答案。

为了使其更加真实,我的动画实现是:

if let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 0))
    {
        let favouriteView = createFakeFavouriteAction(frame: cell.frame)
        tableView.insertSubview(favouriteView, belowSubview: cell)

        CATransaction.begin()
        let animation = CABasicAnimation(keyPath: "position")
        animation.duration = 0.08
        animation.repeatCount = 1
        animation.autoreverses = true
        animation.speed = 0.3
        let fromPoint: CGPoint = CGPoint(x: cell.center.x, y: cell.center.y)
        animation.fromValue = NSValue(cgPoint: fromPoint)
        let toPoint: CGPoint = CGPoint(x: cell.center.x - 75, y: cell.center.y)
        animation.toValue = NSValue(cgPoint: toPoint)
        CATransaction.setCompletionBlock {
            favouriteView.removeFromSuperview()
        }
        cell.setEditing(true, animated: true)
        cell.layer.add(animation, forKey: "position")
    }

现在,我可以想象如果您有多个UITableViewRowAction,那么您希望在单元格下添加多个视图,并且您也希望为这些视图设置动画。我的实现只是让用户知道这个单元格是可刷的一种简单方法。