What should I do if I want to achieve icon management for iPhone types, with both large and small icons that can be dragged and deleted?
I tried using UICollectionview, but it doesn’t seem to achieve the desired effect. Because these icons vary in size, what layout should be used? When the large icon moves towards the small icon, how to calculate the surrounding small icons and exchange the positions of the four small icons with the large icon?
2
Answers
You can implement icon management using UICollectionview as follows:
By following these previous steps, you can achieve icon management using UICollectionview.
You can achieve your objectives by using UICollectionView. However, Compositional Layout might be easier to use (instead of the Flow Layout) as it doesn’t require you to provide exact sizes of each icon in your view. You can provide estimates instead. You might also find this library useful as it simplifies the implementation of Compositional Layout further. You would use this library by creating an enum and define the sections like this example:
And then in your view controller where the collection view is, you would generate the layout like so:
If you want to reorder items in your collection view, your data source must implement collectionView(_:moveItemAt:to:). As the documentation reads, if you don’t implement this method, you cannot reorder items in your collection view.
Lastly, if you want to support the drag and drop effect in your collection view, you must implement the functions specified in UICollectionViewDragDelegate protocol.