How to move UITableViewCell back and forth to show it can be swiped?

前端 未结 2 1337
清酒与你
清酒与你 2021-02-19 13:37

I see in some apps when you come to a screen with a tableview there\'s a short animation of the cell starting to be swiped, showing the red \"swipe to delete\" button (UIContext

2条回答
  •  难免孤独
    2021-02-19 14:03

    I have a piece of code that I saw long time ago to animate a view. Since our UITableViewCell is also a view, we can use it :) You just need to get your visible cell to animate, like so:

    if let visibleCell = self.tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as? CustomCell {
            print("Started animation...")
            let animation = CAKeyframeAnimation(keyPath: "transform.translation.x")
            animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
            animation.duration = 0.6
            animation.values = [-20.0, 20.0, -20.0, 20.0, -10.0, 10.0, -5.0, 5.0, 0.0 ]
            visibleCell.layer.add(animation, forKey: "shake")
        }
    

    Let me know if this helps. Tested it.

    EDIT:

    Animating your UITableView to let the user see that they can swipe on a cell is pretty easy, try it like so:

        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
            self.tableView.setEditing(true, animated: true)
            DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
                self.tableView.setEditing(false, animated: true)
            }
        }
    

    HOWEVER, if you want to swipe programmatically your cell to show your custom row actions, (I've been researching this for an hour), you can only achieve this, as far as I know, by using method swizzling. See this SO answer: http://codejaxy.com/q/186524/ios-swift-uitableview-how-to-present-uitableviewrowactions-from-pressing-a-button

提交回复
热议问题