Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
We’ve seen a feature in our iOS devices home screen on long press to remove an app and drag and drop any of the app. So, I have written this blog to show how we can create this feature in our app while development.
I have used UICollectionView DataSources and Delegates to implement this functionality with a shaking animation.
Here is my view that looks like with few sets of images in a UICollectionView:
Add UILongPressGestureRecognizer on UICollectionView in viewDidLoad:
UILongPressGestureRecognizer action func definition as given below:
Different states of Long press is given below:
imgArr is an array of [String] type. The array consists of a set of sample images.
UICollectionViewCell consisting of an image view and a remove button.
The UIImageView is for displaying an image over the UICollectionView and UIButton for showing the cross button over the cell for deleting an item from collection view.
startAnimate snippet for animation over image in collection view:
The func will start toggling of the images.
stopAnimate snippet for animation over image in collection view:
The func will stop toggling of images.
Remove Button and done button is shown like in iPhone X :
On clicking Remove button action remove the item at that index from UICollectionView and reload it.
On Done button action hide the done button, stop the animation.
For changing the cell locations from one place to another or dragging the cells write following snippets:
–> Return true for enabling the drag feature.
–>
–> Swap item from source to destination
I hope from this, it will make you more comfortable using a LongPress and drag and drop feature of UICollectionView. Thanks for tuning in once again!
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { print(“Start index :- \(sourceIndexPath.item)”) print(“End index :- \(destinationIndexPath.item)”)
let tmp = imgArr[sourceIndexPath.item]
if sourceIndexPath.item <= destinationIndexPath.item { var index = sourceIndexPath.item while (index destinationIndexPath.item) { imgArr[index] = imgArr[index – 1] index -= 1 } imgArr[destinationIndexPath.item] = tmp } collectionView.reloadData() }
You can ask your queries and we will try to help you out.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
Name
Email
Subject
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.