How to swipe left to delete a CollectionCell
It is a tableView existed functionality. For collectionView you should write it on your own. There is no existed pre-built solution.
You can use the pod like this (which can what do you need)
https://github.com/SwipeCellKit/SwipeCellKit
Or custom written simple logic like here:
https://medium.com/@ales.musto/using-a-uipangesturerecognizer-to-delete-cells-in-a-uicollectionview-swift-3-e91cdfcce5be
Update:
There is actually a way to do this (now) with a built-in API:
How do you support swiping to delete a row in a UICollectionView list with compositional layout?
Embedded UITableView does not detect a swipe to delete row gesture
Try one of the two options:
- Look at
require(toFail otherGestureRecognizer: UIGestureRecognizer)
. Add the swipe gestures with this method to the collection view pan gesture when you create (or populate) the cells. - Try to look at
shouldRequireFailure(of: otherGestureRecognizer)
. SubclassUICollectionView
and override this method to return true whenotherGestureRecognizer
is actually a swipe to delete row gesture.
You just have to figure how you can identify/retrieve both of the gesture recognizers (hint: browse the gestureRecognizers
property to find some PanGestureRecognizers
)
And you should definitely refine that clunky design.
Swift CollectionView Remove Items with swipe gesture
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.
}
UICollectionView how to delete cells (equivalent of commitEditingStyle)?
I inserted this code in my view controller that includes the CollectionView and did it this way. You are probably already doing something like this with the tap gesture to detect selected cell.
- (IBAction)didLongPressCellToDelete:(UILongPressGestureRecognizer*)gesture {
CGPoint tapLocation = [gesture locationInView:self.myCollectionView];
NSIndexPath *indexPath = [self.myCollectionView indexPathForItemAtPoint:tapLocation];
if (indexPath && gesture.state == UIGestureRecognizerStateBegan) {
NSLog(@"image with index %d to be deleted", indexPath.item);
self.itemToBeDeleted = indexPath.item;
UIAlertView *deleteAlert = [[UIAlertView alloc]
initWithTitle:@"Delete?"
message:@"Are you sure you want to delete this image?"
delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil];
[deleteAlert show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"selected button index = %d", buttonIndex);
if (buttonIndex == 1) {
// Do what you need to do to delete the cell
[self.myCollectionView reloadData];
}
}
Swipe to delete entire section in UITableView (iOS)
I haven't tested this but the idea is below. Take a view (self.header
) and use the touchesBegan...
method to detect the user placing their finger on screen. Then, follow the finger with the touchesMoved...
method and calculate the difference between the last offset and the next. It should grow by 1 (or more) depending on how fast the user is moving their finger. Use this value to subtract the origin.x
of the cell's contentView
.
var header: UIView!
var tableView:UITableView!
var offset:CGFloat = 0
override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// Touches Began. Disable user activity on UITableView
if let touch = touches.first {
// Get the point where the touch started
let point = touch.location(in: self.header)
offset = point.x
}
}
override public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
// Get the point where the touch is moving in header
let point = touch.location(in: self.header)
// Calculate the movement of finger
let x:CGFloat = offset - point.x
if x > 0 {
// Move cells by offset
moveCellsBy(x: x)
}
// Set new offset
offset = point.x
}
}
override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
// Reset offset when user lifts finter
offset = 0
}
func moveCellsBy(x: CGFloat) {
// Move each visible cell with the offset
for cell in self.tableView.visibleCells {
// Place in animation block for smoothness
UIView.animate(withDuration: 0.05, animations: {
cell.contentView.frame = CGRect(x: cell.contentView.frame.origin.x - x, y: cell.contentView.frame.origin.y, width: cell.contentView.frame.size.width, height: cell.contentView.frame.size.height)
})
}
}
How do I get swipe-to-delete working when tableView's allowsMultipleSelectionDuringEditing property is YES?
The trick is to set allowsMultipleSelectionDuringEditing
to YES on entering edit mode and set it back to NO on exiting edit mode. This way, both swipe-to-delete and multiple selections in edit mode work.
If you've subclassed UITableViewController
(which you probably have), then you can simply do this:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
// Set allowsMultipleSelectionDuringEditing to YES only while
// editing. This gives us the golden combination of swipe-to-delete
// while out of edit mode and multiple selections while in it.
self.tableView.allowsMultipleSelectionDuringEditing = editing;
[super setEditing:editing animated:animated];
}
Related Topics
Symbolicate Crash in iOS8 with Xcode 6 .1
Coreanimation - Opacity Fade in and Out Animation Not Working
Xcode 4.3:Missing Icons for iOS Apps in Organizer's Archives
Titletextattributes Uiappearance Font in iOS 7
Can Swift Closures Be Set to a Default Value When Used as a Parameter in a Function
"Can't Find Model for Source Store" Occurring During iPhone "Automatic Lightweight Migration"
Firebase Retrieving Data in Swift
Xcode 8 Swift 3 Uitableview Space Between Cells
Core Data: Delete All Objects of an Entity Type, Ie Clear a Table
How to Animate Uitableviewcell Height Using Auto-Layout
iOS 10 Heading Arrow for Mkuserlocation Dot
Calling Function from Another Viewcontroller in Swift
Where Is the Run Script and Build Phase in Xcode
Encryption with Rsa Public Key on iOS