4

Currently, I have a collectionView and its only function is to let the user choose a color. When the user chooses a color (clicks a cell), a selected indicator (UIView) animates inside the cell, letting the user know that that certain color is selected. The core functionality is there, but my question is based more off of UI. When I scroll, the cell thinks I am clicking it and it sets isSelected to true for a second until I remove my finger from the cell. This causes an animation to happen at unwanted times. So, I think the problem is in shouldSelectItemAt in the CollectionViewDelegate methods. With UIGestureRecognizer I can measure whether or not the sender state has .began or .ended. Would my question call for this or something similar?

Here are the selection collectionView delegate methods.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = colorPreviewCollectionView.cellForItem(at: indexPath) as! TrayColorPreviewCell
        // stuff with cell happens here
    }

func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
        let cell = colorPreviewCollectionView.cellForItem(at: indexPath) as! TrayColorPreviewCell
        guard cell.isSelected else { return true }
        return false
    }

In the TrayColorPreviewCell (custom UICollectionViewCell) class, I use the isSelected to run the animation. This animation is the one that is happening repeatedly when scrolling.

override var isSelected: Bool {
        didSet {
            changeSelectionStatus()
        }
    }

func changeSelectionStatus() {
        if isSelected {
            // animation to indicate selection

        } else {
            // animation to indicate changed selection
        }

    }
gmdev
  • 2,725
  • 2
  • 13
  • 28

0 Answers0