滑动以在UICollectionView上显示其他按钮

问题描述 投票:0回答:1

我正在尝试创建一个类似于tableView滑动的功能来显示删除按钮,唯一的区别是我显示了多个按钮并将其实现在collectionView内的collectionViewCell上。我希望能够将内部collectionView向右滑动,并在左侧将多个选项按钮捕捉到视图中。

像这样的东西:

enter image description here

我明白我可能需要使用UIPanGestureRecognizer,对我来说问题是panView的集合嵌套在另一个collectionView中,我不确定如何正确使用UIGestureRecognizer以便细胞一起滑动和按钮卡入视图。

任何建议都非常感谢。

ios swift uicollectionview uigesturerecognizer uicollectionviewcell
1个回答
0
投票

在集合视图中设置委托以获取操作

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

}
© www.soinside.com 2019 - 2024. All rights reserved.