Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - swipe UICollectionViewCell horizontally

I have a UICollectionView that looks like a tableView and I want the cells to swipe only horizontally.

I have managed to make them to move around, but the problem is I can move them up as well and basically I can move them in any direction and I want them to swipe like a tableViewCell when you delete it for example.

In the end I want to be able to just swipe a cell out of the screen horizontally

I have attached a image of how the collection view looks now and a cell that moves around(the red one)

enter image description here

like image 596
razvan Avatar asked Nov 19 '25 03:11

razvan


1 Answers

I found a way to do it with UIPanGestureRecognizer after all. All the operations are on the cell(inside the cell class). Below you have the solution that worked for me

var swipeGesture: UIPanGestureRecognizer!
var originalPoint: CGPoint!

func configureCell() {
    setupSwipeGesture()
}

func setupSwipeGesture() {
        swipeGesture = UIPanGestureRecognizer(target: self, action:#selector(swiped(_:)))
        swipeGesture.delegate = self

        self.addGestureRecognizer(swipeGesture)
    }


func swiped(_ gestureRecognizer: UIPanGestureRecognizer) {
let xDistance:CGFloat = gestureRecognizer.translation(in: self).x

        switch(gestureRecognizer.state) {
        case UIGestureRecognizerState.began:
            self.originalPoint = self.center
        case UIGestureRecognizerState.changed:
            let translation: CGPoint = gestureRecognizer.translation(in: self)
            let displacement: CGPoint = CGPoint.init(x: translation.x, y: translation.y)

            if displacement.x + self.originalPoint.x < self.originalPoint.x {
                self.transform = CGAffineTransform.init(translationX: displacement.x, y: 0)
                self.center = CGPoint(x: self.originalPoint.x + xDistance, y: self.originalPoint.y)
            }
        case UIGestureRecognizerState.ended:
            let hasMovedToFarLeft = self.frame.maxX < UIScreen.main.bounds.width / 2
            if (hasMovedToFarLeft) {
                removeViewFromParentWithAnimation()
            } else {
                resetViewPositionAndTransformations()
            }
        default:
            break
        }
    }

func resetViewPositionAndTransformations(){
        UIView.animate(withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.0, options: UIViewAnimationOptions(), animations: {
            self.center = self.originalPoint
            self.transform = CGAffineTransform(rotationAngle: 0)
        }, completion: {success in })
    }

func removeViewFromParentWithAnimation() {
        var animations:(()->Void)!
        animations = {self.center.x = -self.frame.width}

        UIView.animate(withDuration: 0.2, animations: animations , completion: {success in self.removeFromSuperview()})
    }
like image 180
razvan Avatar answered Nov 20 '25 15:11

razvan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!