如何为IndexPath行编辑动作动画来显示用户动作



我看到一些应用程序在用户加载tableView时实现了一个动画,显示他们可以在单元格上向左滑动。它显示正在被滑动的单元格,然后迅速关闭,以便用户可以做他们想做的事情。

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

我可以程序化地刷单元格吗?因此在viewDidLoad(或appear)上调用这个?

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

我绝对同意我面前的答案。

为了使它更逼真,这里是我的动画实现:

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那么你想在你的单元格下添加不止一个视图你也想让那些动画化。我的实现只是让用户知道这个单元格是可滑动的一种简单方法。

最新更新