DragSelectCollectionView

public class DragSelectCollectionView: UICollectionView

A UICollectionView subclass that enables contiuous selection of cells while dragging. Use this class as you would use a regular instance of UICollectionView, i.e. with a UICollectionViewDataSource and a UICollectionViewDelegate. Call beginDragSelection(at:) when you want to start a continuous selection event starting at a particular IndexPath. Throughout the selection process, this class will ask its delegate if it should select / deselect each cell it encounters.

  • Sets a maximum number of cells that may be selected, nil by default. Setting this value to a value of 0 or lower effectively disables selection. Setting this value to nil removes any upper limit to selection. If when setting a new value, the collection view already has a greater number of cells selected, then the apporpriate number of cells will be deselected from the end of the list.

    Declaration

    Swift

    public var selectionLimit: Int?
  • Height of top and bottom hotspots for auto scrolling. Defaults to 100. Set this to 0 to disable auto scrolling.

    Declaration

    Swift

    public var hotspotHeight: CGFloat = 100
  • Padding between top of collection view and top hotspot. Defaults to 0.

    Declaration

    Swift

    public var hotspotOffsetTop: CGFloat = 0
  • Padding between bottom of collection view and bottom hotspot. Defaults to 0.

    Declaration

    Swift

    public var hotspotOffsetBottom: CGFloat = 0
  • Used to calculate auto scroll speed. Defaults to 0.5. Auto scroll speed is calculated as baseAutoScrollVelocity times however many points into the hotspot the user has touched, per 0.025 seconds. For example, if the user has traversed 5 points from the middle of the screen into either of the hotspot, and this value is 0.5, then the velocity will be 0.5 * 5 / 0.025 = 100 points/second.

    Declaration

    Swift

    public var baseAutoScrollVelocity: CGFloat = 0.5
  • Toggles selection and scrolling information output to console. Defaults to false.

    Declaration

    Swift

    public static var logging = false
  • Toggles whether to show a faded green view where the hotspots are. Defaults to false. Useful for debugging.

    Declaration

    Swift

    public var showHotspots = false
  • Attempts to begin drag selection at the provided index path.

    Declaration

    Swift

    @discardableResult public func beginDragSelection(at selection: IndexPath) -> Bool

    Parameters

    selection

    the index path at which to begin drag selection.

    Return Value

    false if drag selection is alreay in progress or selection cannot be selected (decided by the UICollectionViewDelegate), true otherwise.

  • Attempts to select all items, starting from the first item in the collection. If an item cannot be selected (decided by the UICollectionViewDelegate), the item is skipped. If selectionLimit is reached, this method terminates. The collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) method of the UICollectionViewDelegate is called for each selected item.

    Declaration

    Swift

    public func selectAll()
  • Deselects all selected items. The collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) method of the UICollectionViewDelegate is called for each deselected item.

    Declaration

    Swift

    public func deselectAll()