How to implement UITableView`s swipe to delete for UICollectionView

前端 未结 6 512
你的背包
你的背包 2020-12-08 10:34

I just like to ask how can I implement the same behavior of UITableView`s swipe to delete in UICollectionView. I am trying to find a tutorial but I cannot find any.

6条回答
  •  臣服心动
    2020-12-08 11:22

    There is a more standard solution to implement this feature, having a behavior very similar to the one provided by UITableView.

    For this, you will use a UIScrollView as the root view of the cell, and then position the cell content and the delete button inside the scroll view. The code in your cell class should be something like this:

    override init(frame: CGRect) {
        super.init(frame: frame)
    
        addSubview(scrollView)
        scrollView.addSubview(viewWithCellContent)
        scrollView.addSubview(deleteButton)
        scrollView.isPagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
    }
    

    In this code we set the property isPagingEnabled to true to make the scroll view to stop scrolling only at the boundaries of its content. The layout subviews for this cell should be something like:

    override func layoutSubviews() {
        super.layoutSubviews()
    
        scrollView.frame = bounds
        // make the view with the content to fill the scroll view
        viewWithCellContent.frame = scrollView.bounds
        // position the delete button just at the right of the view with the content.
        deleteButton.frame = CGRect(
            x: label.frame.maxX, 
            y: 0, 
            width: 100, 
            height: scrollView.bounds.height
        )
    
        // update the size of the scrolleable content of the scroll view
        scrollView.contentSize = CGSize(width: button.frame.maxX, height: scrollView.bounds.height)
    }
    

    With this code in place, if you run the app you will see that the swipe to delete is working as expected, however, we lost the ability to select the cell. The problem is that since the scroll view is filling the whole cell, all the touch events are processed by it, so the collection view will never have the opportunity to select the cell (this is similar to when we have a button inside a cell, since touches on that button don't trigger the selection process but are handled directly by the button.)

    To fix this problem we just have to indicate the scroll view to ignore the touch events that are processed by it and not by one of its subviews. To achieve this just create a subclass of UIScrollView and override the following function:

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let result = super.hitTest(point, with: event)
        return result != self ? result : nil
    }
    

    Now in your cell you should use an instance of this new subclass instead of the standard UIScrollView.

    If you run the app now you will see that we have the cell selection back, but this time the swipe isn't working

提交回复
热议问题