Не удается изменить продолжительность анимации для отмены выбора анимации - PullRequest
3 голосов
/ 18 мая 2019

Я пытаюсь реализовать пользовательскую анимацию для ячеек в ячейке UICollectionView, но не могу реализовать анимацию для отмены выбора действия.

Я использую быстрый и развивающийся проект для iOS> = 10

Этоэто пример проекта https://drive.google.com/file/d/1nUVVBFBA7N6ZHNIKOdHf21j4rv1w0SfL/view?usp=sharing

Это код из примера проекта

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        var selectedIndex: Int?

        if let selectedIndexPathes = collectionView.indexPathsForSelectedItems {
            if (selectedIndexPathes.count > 0) {
                selectedIndex = selectedIndexPathes[0].item
            }
        }

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell

        if (selectedIndex == indexPath.item) {
            cell.configureForSelected()
        }
        else {
            cell.configureForUnselected()
        }

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        var selectedIndex: Int?

        if let selectedIndexPathes = collectionView.indexPathsForSelectedItems {
            if (selectedIndexPathes.count > 0) {
                selectedIndex = selectedIndexPathes[0].item
            }
        }

        if (selectedIndex == indexPath.item) {
            return CGSize(width: collectionView.frame.width, height: 200)
        }

        return CGSize(width: collectionView.frame.width, height: 100)
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        UIView.animate(withDuration: 3.0) {
            collectionView.performBatchUpdates({

                if (self.lastSelectedIndexPath != nil) {
                    let lastCell = collectionView.cellForItem(at: self.lastSelectedIndexPath!) as! CollectionViewCell
                    lastCell.configureForUnselected()
                }

                let cell = collectionView.cellForItem(at: indexPath) as! CollectionViewCell
                cell.configureForSelected()
            }, completion: nil)
        }
        lastSelectedIndexPath = indexPath
    }

enter image description here

Как плавно анимировать отмену выделения?

Ответы [ 2 ]

3 голосов
/ 19 мая 2019

Вам просто нужно реализовать метод ниже в вашем коде,

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    UIView.animate(withDuration: 3.0) {
        collectionView.performBatchUpdates({

            if (self.lastSelectedIndexPath != nil) {
                let lastCell = collectionView.cellForItem(at: self.lastSelectedIndexPath!) as! CollectionViewCell
                lastCell.configureForUnselected()
            }

      }, completion: nil)
    }
}

enter image description here

0 голосов
/ 19 мая 2019

решение очень простое, если вы решите проблему в ячейке представления коллекции

, пожалуйста, замените функции внутри этого класса на

 public func setup() {

    self.backgroundColor = UIColor.gray
}

public func configureForSelected() {
    self.backgroundColor = UIColor.red
}

public func configureForUnselected() {
    self.backgroundColor = UIColor.gray
}

вместо

  public func setup() {

    self.contentView.backgroundColor = UIColor.gray
}

public func configureForSelected() {
    self.contentView.backgroundColor = UIColor.red
}

public func configureForUnselected() {
    self.contentView.backgroundColor = UIColor.gray
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...