Created
March 24, 2021 13:36
-
-
Save Heilum/fe156f740bbdb471a828f290e0cb1eed to your computer and use it in GitHub Desktop.
selecting_multiple_items_with_a_two-finger_pan_gesture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://developer.apple.com/documentation/uikit/uitableviewdelegate/selecting_multiple_items_with_a_two-finger_pan_gesture | |
//CollectionViewCell.swift | |
//===================== | |
/* | |
See LICENSE folder for this sample’s licensing information. | |
Abstract: | |
A custom collection view cell that `CollectionViewController` displays. | |
*/ | |
import UIKit | |
class CollectionViewCell: UICollectionViewCell { | |
static let reuseIdentifier = "reuseIdentifier" | |
@IBOutlet weak var imageView: UIImageView! | |
@IBOutlet weak var imageViewOverlay: UIView! | |
@IBOutlet weak var imageViewSelected: UIImageView! | |
@IBOutlet weak var imageViewUnselected: UIImageView! | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
// Turn `imageViewSelected` into a circle to make its background | |
// color act as a border around the checkmark symbol. | |
imageViewSelected.layer.cornerRadius = imageViewSelected.bounds.width / 2 | |
imageViewUnselected.layer.cornerRadius = imageViewSelected.bounds.width / 2 | |
self.imageViewOverlay.isHidden = true; | |
self.imageViewSelected.isHidden = true; | |
self.imageViewUnselected.isHidden = false; | |
} | |
func configureCell(with model: PhotoModel) { | |
if let image = model.image { | |
imageView.image = image | |
} | |
} | |
override var isSelected: Bool{ | |
didSet{ | |
if isSelected { | |
self.imageViewOverlay.isHidden = false; | |
self.imageViewSelected.isHidden = false; | |
self.imageViewUnselected.isHidden = true; | |
}else{ | |
self.imageViewOverlay.isHidden = true; | |
self.imageViewSelected.isHidden = true; | |
self.imageViewUnselected.isHidden = false; | |
} | |
} | |
} | |
} | |
//CollectionViewController.swift | |
//===================== | |
/* | |
See LICENSE folder for this sample’s licensing information. | |
Abstract: | |
A view controller showing that supports a two-finger pan gesture for selecting multiple items. | |
*/ | |
import UIKit | |
class CollectionViewController: UIViewController { | |
@IBOutlet weak var collectionView: UICollectionView! | |
private let photos = PhotoModel.generatePhotosItems(count: 100) | |
private let sectionInsets = UIEdgeInsets(top: 1.0, left: 1.0, bottom: 1.0, right: 1.0) | |
private var isPad = false | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
isPad = view.traitCollection.userInterfaceIdiom == .pad | |
collectionView.allowsSelection = true | |
collectionView.allowsMultipleSelection = true | |
if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout { | |
flowLayout.minimumLineSpacing = sectionInsets.left | |
flowLayout.minimumInteritemSpacing = sectionInsets.left | |
flowLayout.sectionInset = sectionInsets | |
flowLayout.itemSize = itemSize() | |
} | |
updateUserInterface() | |
} | |
func updateUserInterface() { | |
guard let button = navigationItem.rightBarButtonItem else { return } | |
button.title = isEditing ? "Done" : "Select" | |
} | |
func clearSelectedItems(animated: Bool) { | |
collectionView.indexPathsForSelectedItems?.forEach({ (indexPath) in | |
collectionView.deselectItem(at: indexPath, animated: animated) | |
}) | |
collectionView.reloadItems(at: collectionView.indexPathsForVisibleItems) | |
} | |
func itemSize() -> CGSize { | |
let itemsPerRow: CGFloat = isPad ? 10 : 3 | |
let paddingSpace = sectionInsets.left * (itemsPerRow + 1) | |
let availableWidth = view.frame.width - paddingSpace | |
let widthPerItem = availableWidth / itemsPerRow | |
return CGSize(width: widthPerItem, height: widthPerItem) | |
} | |
@IBAction func toggleSelectionMode(_ sender: Any) { | |
// Toggle selection state. | |
//setEditing(!isEditing, animated: true) | |
} | |
} | |
extension CollectionViewController: UICollectionViewDataSource { | |
func numberOfSections(in collectionView: UICollectionView) -> Int { | |
return 1 | |
} | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return photos.count | |
} | |
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CollectionViewCell.reuseIdentifier, for: indexPath) | |
if let photoCell = cell as? CollectionViewCell { | |
photoCell.configureCell(with: photos[indexPath.item]) | |
} | |
return cell | |
} | |
} | |
extension CollectionViewController: UICollectionViewDelegate { | |
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | |
// Automatically deselect items when the app isn't in edit mode. | |
} | |
// MARK: - Multiple selection methods. | |
/// - Tag: collection-view-multi-select | |
func collectionView(_ collectionView: UICollectionView, shouldBeginMultipleSelectionInteractionAt indexPath: IndexPath) -> Bool { | |
// Returning `true` automatically sets `collectionView.allowsMultipleSelection` | |
// to `true`. The app sets it to `false` after the user taps the Done button. | |
return true | |
} | |
func collectionView(_ collectionView: UICollectionView, didBeginMultipleSelectionInteractionAt indexPath: IndexPath) { | |
// Replace the Select button with Done, and put the | |
// collection view into editing mode. | |
print("\(#function)") | |
} | |
func collectionViewDidEndMultipleSelectionInteraction(_ collectionView: UICollectionView) { | |
print("\(#function)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment