如何来回移动UITableViewCell以显示它可以滑动?

Mar*_*ens 6 ios swift

我在某些应用程序中看到,当您进入带有表格视图的屏幕时,该单元格的简短动画开始被滑动,显示红色的“滑动以删除”按钮(UIContextualAction按钮),然后恢复正常。它给用户提示:“这些行可以刷卡。”

有没有办法达到这种效果?也许是一种以编程方式开始划行然后取消的方法?

Mar*_*ens 6

快速解决方案

好吧,大约1.5年后,我终于想出了一个解决方案。

第1步-单元界面设置

我像这样设置自定义表格视图单元: UI工作

  • A和B代表滑动动作的颜色。
  • C是我将左右动画的UIView。

第2步-将动画添加到单元格

func animateSwipeHint() {
    slideInFromRight()
}

private func slideInFromRight() {
    UIView.animate(withDuration: 0.5, delay: 0.3, options: [.curveEaseOut], animations: {
        self.cellBackgroundView.transform = CGAffineTransform(translationX: -self.swipeHintDistance, y: 0)
        self.cellBackgroundView.layer.cornerRadius = 10
    }) { (success) in
        UIView.animate(withDuration: 0.2, delay: 0, options: [.curveLinear], animations: {
            self.cellBackgroundView.transform = .identity
        }, completion: { (success) in
            // Slide from left if you have leading swipe actions
            self.slideInFromLeft()
        })
    }
}

private func slideInFromLeft() {
    UIView.animate(withDuration: 0.5, delay: 0, options: [.curveEaseOut], animations: {
        self.cellBackgroundView.transform = CGAffineTransform(translationX: self.swipeHintDistance, y: 0)
    }) { (success) in
        UIView.animate(withDuration: 0.2, delay: 0, options: [.curveLinear], animations: {
            self.cellBackgroundView.transform = .identity
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

步骤3-触发动画

viewDidLoad具有表视图的视图控制器的中,我有以下代码:

if self.tableView.visibleCells.count > 0 {
    let cell = self.tableView.visibleCells[0] as! TableViewCell
    cell.animateSwipeHint()
}
Run Code Online (Sandbox Code Playgroud)

例:

动画示例

视频解决方案

如果您想进一步了解此解决方案,创建一个视频:https : //youtu.be/oAGoFd_GrxE