3

I have seen a few apps that have implemented an animation when users load a tableView that shows them they can swipe left on a cell. It shows the cell being swiped then quickly closes so the user can do what they want.

I have searched for some pre-exiting libraries but haven't found any.

Can I programatically swipe the cell? And therefore call this on viewDidLoad (or appear)?

Craig.Pearce
  • 746
  • 7
  • 25

2 Answers2

1

In Swift you can do something like this, however, this does not reveal the underlying edit actions that are normally revealed by swiping left on a UITableViewcell. It only animates the swipe action

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")
}
jcpennypincher
  • 3,970
  • 5
  • 31
  • 44
1

I definitely second the answer before me.

In order to make it more realistic here is my implementation of the animation:

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

Now, I can imagine if you have more than one UITableViewRowAction then you want to add more than one view under your cell and you want to animate those as well. My implementation is just a simple way of letting users know this cell is swipeable.

sam k
  • 1,048
  • 2
  • 14
  • 22