Last active
February 9, 2021 04:18
-
-
Save fethica/4d33d7b3798e79139ddcbfb76b49e57e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From: https://stackoverflow.com/questions/55592124/ios-how-to-give-a-peek-at-the-swipe-to-delete-action-on-a-table-view-cell | |
extension UITableView { | |
func presentTrailingSwipeHint() { | |
let cell = self.cellForRow(at: IndexPath(item: 0, section: 0)) | |
cell?.presentTrailingSwipeHint(actionColor: UIColor.red) | |
} | |
} | |
fileprivate extension UITableViewCell { | |
func presentTrailingSwipeHint(actionColor: UIColor, hintWidth: CGFloat = 10, hintDuration: TimeInterval = 0.5) { | |
// Create fake action view | |
let dummyView = UIView() | |
dummyView.backgroundColor = .red | |
dummyView.translatesAutoresizingMaskIntoConstraints = false | |
addSubview(dummyView) | |
clipsToBounds = false | |
// Set constraints | |
NSLayoutConstraint.activate([ | |
dummyView.topAnchor.constraint(equalTo: topAnchor), | |
dummyView.leadingAnchor.constraint(equalTo: trailingAnchor, constant: 0), | |
dummyView.bottomAnchor.constraint(equalTo: bottomAnchor), | |
dummyView.widthAnchor.constraint(equalToConstant: hintWidth) | |
]) | |
// This animator reverses back the transform. | |
let secondAnimator = UIViewPropertyAnimator(duration: hintDuration / 2, curve: .easeOut) { | |
self.transform = .identity | |
} | |
// Don't forget to remove the useless view. | |
secondAnimator.addCompletion { position in | |
dummyView.removeFromSuperview() | |
} | |
// We're moving the cell and since dummyView | |
// is pinned to cell's trailing anchor | |
// it will move as well. | |
let transform = CGAffineTransform(translationX: -hintWidth, y: 0) | |
let firstAnimator = UIViewPropertyAnimator(duration: hintDuration / 2, curve: .easeIn) { | |
self.transform = transform | |
} | |
firstAnimator.addCompletion { position in | |
secondAnimator.startAnimation() | |
} | |
// Do the magic. | |
firstAnimator.startAnimation() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment