Improve highlight animation for item cells

This commit is contained in:
Shadowfacts 2022-01-14 11:43:21 -05:00
parent f8026125cc
commit 83c3bc927e
2 changed files with 13 additions and 7 deletions

View File

@ -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

View File

@ -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
}