From 83c3bc927e73052d8e41d1b8c9de88060d6dcba2 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 14 Jan 2022 11:43:21 -0500 Subject: [PATCH] Improve highlight animation for item cells --- .../Items/ItemCollectionViewCell.swift | 19 ++++++++++++------- .../Screens/Items/ItemsViewController.swift | 1 + 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Reader/Screens/Items/ItemCollectionViewCell.swift b/Reader/Screens/Items/ItemCollectionViewCell.swift index b26633f..c74d560 100644 --- a/Reader/Screens/Items/ItemCollectionViewCell.swift +++ b/Reader/Screens/Items/ItemCollectionViewCell.swift @@ -19,6 +19,7 @@ class ItemCollectionViewCell: UICollectionViewListCell { private let feedTitleLabel = UILabel() private let contentLabel = UILabel() private var shouldInteractOnNextTouch = true + var scrollView: UIScrollView? private var item: Item! @@ -112,12 +113,16 @@ class ItemCollectionViewCell: UICollectionViewListCell { guard shouldInteractOnNextTouch else { return } shouldInteractOnNextTouch = false feedbackGenerator.prepare() - UIView.animateKeyframes(withDuration: 0.4, delay: 0, options: .allowUserInteraction) { - UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.5) { - self.backgroundColor = .appCellHighlightBackground - } - UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5) { - self.backgroundColor = nil + + // couldn't manage to do this with just the recognizers + if scrollView?.isDragging == false && scrollView?.isDecelerating == false { + UIView.animateKeyframes(withDuration: 0.4, delay: 0, options: .allowUserInteraction) { + UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.5) { + self.backgroundColor = .appCellHighlightBackground + } + UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5) { + self.backgroundColor = nil + } } } } @@ -149,7 +154,7 @@ private class DoubleTapRecognizer: UITapGestureRecognizer { super.touchesBegan(touches, with: event) onTouchesBegan() // shorten the delay before the single tap is recognized - DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(300)) { [weak self] in + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(400)) { [weak self] in guard let self = self else { return } if self.state != .recognized { self.state = .failed diff --git a/Reader/Screens/Items/ItemsViewController.swift b/Reader/Screens/Items/ItemsViewController.swift index 61f089f..3fe9bba 100644 --- a/Reader/Screens/Items/ItemsViewController.swift +++ b/Reader/Screens/Items/ItemsViewController.swift @@ -62,6 +62,7 @@ extension ItemsViewController: UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "itemCell", for: indexPath) as! ItemCollectionViewCell cell.delegate = self + cell.scrollView = collectionView cell.updateUI(item: resultsController.fetchedObjects![indexPath.row]) return cell }