Как синхронизировать анимацию клеток - PullRequest
0 голосов
/ 03 февраля 2020

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

Когда я запускаю приложение, оно прекрасно работает:

enter image description here

Но когда я прокручиваю влево, каждая ячейка получает анимацию:

enter image description here

То, что я хочу, это когда я прокручиваю ее влево работает как первое изображение. Какое-то время я просто поместил следующий код для анимации:

    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        guard let cell = cell as? ShimmeringCell else { return }
        UIView.animate(withDuration: 1.0, delay: 0, options: [.autoreverse, .repeat], animations: {
            cell.alpha = 0.4
        }, completion: nil)
    }

Это полный код: Github

Кто-нибудь может мне помочь?

1 Ответ

0 голосов
/ 04 февраля 2020

Я нашел ответ.

Чтобы решить эту проблему, мне пришлось создать новый UIView и добавить его в подпредставлении UICollectionView:

    let newCollectionView: UICollectionView = {
        // Here was creating a collectionView
        return collection
    }()

    let viewExample: UIView = {
        // Here was creating a new view
        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        newCollectionView.delegate = self
        newCollectionView.dataSource = self
        newCollectionView.register(ShimmeringCell.self, forCellWithReuseIdentifier: cellId)
        self.view.addSubview(self.newCollectionView)
        setupCollection()
    }

    private func setupCollection() {
        // Constraints...

        self.newCollectionView.addSubview(self.viewExample)
        setupViewExample()
    }

    private func setupViewExample() {
        // Constraints...
    }

После этого Я просто поместил анимацию в это представление:

    override func viewDidLoad() {
        super.viewDidLoad()

        newCollectionView.delegate = self
        newCollectionView.dataSource = self
        newCollectionView.register(ShimmeringCell.self, forCellWithReuseIdentifier: cellId)
        self.view.addSubview(self.newCollectionView)
        setupCollection()

        self.viewExample.alpha = 0.6
        UIView.animate(withDuration: 1.0, delay: 0, options: [.repeat, .autoreverse, .allowUserInteraction], animations: {
            self.viewExample.alpha = 0
        }, completion: nil)
    }

Получилось:

enter image description here

Спасибо всем и до встречи где-нибудь по миру.

Github с этим решением: Github

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...