3

I have a horizontal scrolling collection view.

I am looking for neat way to remove items with a swipe up or down gesture.
Also rearranging elements would be amazing but removal is more important at the moment.

I have found some Obj-C documents, but, since I am still new to swift Obj-C, it's too much for me.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Cory Trentini
  • 97
  • 1
  • 7
  • So how close are you so far? Can you grab an item and move it around, even if the change isn't saved? – Wain Apr 27 '15 at 07:14
  • 1
    [This very related question](http://stackoverflow.com/questions/14270023/how-to-implement-uitableviews-swipe-to-delete-for-uicollectionview) has answers for Objective-C, but you should be able to easily come up with Swift equivalents for the few API's listed there... – Michael Dautermann Apr 27 '15 at 07:14

2 Answers2

7

I have been dealing with the same situation for the last couple of days. Here is what i did with swift.. I checked Michael's link and did some couple of researching as well...

So..

add this

    let cSelector = Selector("reset:")
    let UpSwipe = UISwipeGestureRecognizer(target: self, action: cSelector )
    UpSwipe.direction = UISwipeGestureRecognizerDirection.Up
    cell.addGestureRecognizer(UpSwipe)

to

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

and then define your selector, which actually deletes the swiped item from your array and then reloads your collection view.

    func reset(sender: UISwipeGestureRecognizer) {
        let cell = sender.view as! UICollectionViewCell
        let i = self.favoritesCV.indexPathForCell(cell)!.item
        favoritesInstance.favoritesArray.removeAtIndex(i)  //replace favoritesInstance.favoritesArray with your own array
        self.favoritesCV.reloadData() // replace favoritesCV with your own collection view.
    }
Koray Birand
  • 1,956
  • 17
  • 22
0

You can also do it by using multiple views in cell. Here's my code. First use three views.

Example :-

@IBOutlet weak var YOURVIEW: UIView!
@IBOutlet weak var edit: UIView!
@IBOutlet weak var delete: UIView!

now make layout of leading and trailing of YOURVIEW

@IBOutlet weak var YOURLEADING: NSLayoutConstraint!
@IBOutlet weak var YOURTRAILING: NSLayoutConstraint!

add this into override func awakeFromNib()

let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
        swipeLeft.direction = .left
            self.YOURTOPVIEW.addGestureRecognizer(swipeLeft
            )

let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
        swipeRight.direction = .right
            self.YOURTOPVIEW.addGestureRecognizer(swipeRight)

now write this code in class body

@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {

        if let swipeGesture = gesture as? UISwipeGestureRecognizer {

            switch swipeGesture.direction {
            
            case .left:
                self.animate()
                self.YOURLEADING.constant = -100
                self.YOURTRAILING.constant = 100
               // YOUR OTHER ACTIONS HERE

                
            case .right:
                self.animate()
                self.YOURLEADING.constant = 100
                self.YOURTRAILING.constant = -100
              // YOUR OTHER ACTIONS HERE
               
            default:
                break
            }
        }
    }

also make a function to show animation

func animate()
    {
        UIView.animate(withDuration: 1,
                       delay: 0.0,
                                   animations: { () -> Void in
                                    self.YOURTOPVIEW.frame = CGRect(x: 0, y: 0, width: self.YOURTOPVIEW.frame.width, height: self.YOURTOPVIEW.frame.height)

        }, completion: { (finished: Bool) -> Void in })
    }

Now the gesture recognizer will work on that specific view and will look like you're swiping the collection view cell.

cigien
  • 57,834
  • 11
  • 73
  • 112