我正在尝试创建一个类似于tableView滑动的功能来显示删除按钮,唯一的区别是我显示了多个按钮并将其实现在collectionView
内的collectionViewCell
上。我希望能够将内部collectionView向右滑动,并在左侧将多个选项按钮捕捉到视图中。
像这样的东西:
我明白我可能需要使用UIPanGestureRecognizer
,对我来说问题是panView的集合嵌套在另一个collectionView
中,我不确定如何正确使用UIGestureRecognizer
以便细胞一起滑动和按钮卡入视图。
任何建议都非常感谢。
在集合视图中设置委托以获取操作
protocol ColumnBookCellDelegate: class {
func deleteBook(_ book: Book)
}
class ColumnBookCell: AZCollectionViewCell{
weak var deleteDelegate: ColumnBookCellDelegate?
var canBeRemove: Bool = false{
didSet{
if self.canBeRemove{
let swipeL = UISwipeGestureRecognizer(target: self, action: #selector(self.showDelete))
swipeL.numberOfTouchesRequired = 1
swipeL.direction = .left
self.addGestureRecognizer(swipeL)
let swipeR = UISwipeGestureRecognizer(target: self, action: #selector(self.hideDelete))
swipeR.numberOfTouchesRequired = 1
swipeR.direction = .right
self.addGestureRecognizer(swipeR)
}
}
}
// Show Delete Button
func showDelete(){
// unhidden button here
// self.button.isHidden = false
UIView.animate(withDuration: 0.2) {
self.layoutIfNeeded()
}
}
// Hide Delete Button
func hideDelete(){
// hidden button here
// self.button.isHidden = true
// self.deleteButton.aZConstraints.width?.constant = 0
UIView.animate(withDuration: 0.2) {
self.layoutIfNeeded()
}
}
// Delete Action
func deleteAction(){
self.deleteDelegate?.deleteBook(self.book)
self.hideDelete()
}
// blob blob blob
}