Reputation: 97
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.
Upvotes: 3
Views: 9269
Reputation: 1
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.
Upvotes: 0
Reputation: 1996
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.
}
Upvotes: 7