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

时间:2018-03-05 06:08:38

标签: ios swift uicollectionview uigesturerecognizer uicollectionviewcell

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

这样的事情:

enter image description here

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

非常感谢任何建议。

1 个答案:

答案 0 :(得分: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

}