//declare the `UISwipeGestureRecognizer`
let swipeGesture = UISwipeGestureRecognizer()
//a call back to notify the swipe in `cellForRowAt` as follow.
var replyCallBack : ( () -> Void)?
//this view perform swiping in cell
@IBOutlet weak var containerView: UIView!
override func awakeFromNib() {
super.awakeFromNib()
//set up your UI
setSwipeGesture() //register gesture
}
func setSwipeGesture(){
// Add the swipe gesture and set the direction to the right or left according to your needs
swipeGesture.direction = .right
contentView.addGestureRecognizer(swipeGesture)
// Add a target to the swipe gesture to handle the swipe
swipeGesture.addTarget(self, action: #selector(handleSwipe(_:)))
}
@objc func handleSwipe(_ gesture: UISwipeGestureRecognizer) {
// Animate the action view onto the screen when the user swipes right
if gesture.direction == .right {
UIView.animate(withDuration: 0.15) {
self.messageContainerView.transform = CGAffineTransform(translationX: self.contentView.frame.width / 2.5, y: 0)
// Schedule a timer to restore the cell after 0.2 seconds or change it according to your needs
Timer.scheduledTimer(withTimeInterval: 0.2, repeats: false) { (_) in
UIView.animate(withDuration: 0.1) {
self.messageContainerView.transform = .identity
//callback to notify in cellForRowAt
self.replyCallBack!()
//your code when
}
}
}
} else {
// Animate the action view off the screen when the user swipes left
UIView.animate(withDuration: 0.3) {
self.messageContainerView.transform = .identity
}
}
}
2条答案
按热度按时间6yt4nkrj1#
我已经为此创建了一个演示。您可以使用https://github.com/Dharmesh-shah2412/demoWhatsappSwipeToReply
目标C:
我已将全景手势添加到表格视图单元格:
斯威夫特:
nbysray52#
我在Xcode 14.2上使用swift 5创建了滑动手势
下面是我在
UITableViewCell
中执行滑动的代码...你可以根据需要改变动画持续时间或者
.right
到.left
动画...在这种情况下,我只执行了右滑动...