From 3e0feba2739af6fbe5768d9c8d693053be093c96 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 20 Jan 2023 10:51:56 -0500 Subject: [PATCH] Fix collapse button disappearing when navigating away --- .../Status/BaseStatusTableViewCell.swift | 40 +++++++++++-------- .../ConversationMainStatusTableViewCell.xib | 6 ++- .../Status/TimelineStatusTableViewCell.xib | 8 +++- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/Tusker/Views/Status/BaseStatusTableViewCell.swift b/Tusker/Views/Status/BaseStatusTableViewCell.swift index bab4031c..37f20974 100644 --- a/Tusker/Views/Status/BaseStatusTableViewCell.swift +++ b/Tusker/Views/Status/BaseStatusTableViewCell.swift @@ -361,23 +361,31 @@ class BaseStatusTableViewCell: UITableViewCell { let buttonImage = UIImage(systemName: collapsed ? "chevron.down" : "chevron.up")! - if animated, let buttonImageView = collapseButton.imageView { - // we need to use a keyframe animation for this, because we want to control the direction the chevron rotates - // when rotating ±π, UIKit will always rotate in the same direction - // using a keyframe to set an intermediate point in the animation allows us to force a specific direction - UIView.animateKeyframes(withDuration: 0.2, delay: 0, options: .calculationModeLinear, animations: { - UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.5) { - buttonImageView.transform = CGAffineTransform(rotationAngle: collapsed ? .pi / 2 : -.pi / 2) - } - UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5) { - buttonImageView.transform = CGAffineTransform(rotationAngle: .pi) - } - }, completion: { (finished) in - buttonImageView.transform = .identity - self.collapseButton.setImage(buttonImage, for: .normal) - }) - } else { + if let buttonImageView = collapseButton.imageView { collapseButton.setImage(buttonImage, for: .normal) + + if animated { + buttonImageView.layer.opacity = 0 + + // this whole hack is necessary because when just rotating buttonImageView, it moves to the left of the button and then animates back to the center + let imageView = UIImageView(image: buttonImageView.image) + imageView.translatesAutoresizingMaskIntoConstraints = false + contentView.addSubview(imageView) + NSLayoutConstraint.activate([ + imageView.widthAnchor.constraint(equalTo: buttonImageView.widthAnchor), + imageView.heightAnchor.constraint(equalTo: buttonImageView.heightAnchor), + imageView.centerXAnchor.constraint(equalTo: collapseButton.centerXAnchor), + imageView.centerYAnchor.constraint(equalTo: collapseButton.centerYAnchor), + ]) + imageView.tintColor = .white + + UIView.animate(withDuration: 0.3, delay: 0) { + imageView.transform = CGAffineTransform(rotationAngle: .pi) + } completion: { _ in + imageView.removeFromSuperview() + buttonImageView.layer.opacity = 1 + } + } } if collapsed { diff --git a/Tusker/Views/Status/ConversationMainStatusTableViewCell.xib b/Tusker/Views/Status/ConversationMainStatusTableViewCell.xib index 5bcd4dae..1437e6eb 100644 --- a/Tusker/Views/Status/ConversationMainStatusTableViewCell.xib +++ b/Tusker/Views/Status/ConversationMainStatusTableViewCell.xib @@ -81,10 +81,14 @@ - + + + + + diff --git a/Tusker/Views/Status/TimelineStatusTableViewCell.xib b/Tusker/Views/Status/TimelineStatusTableViewCell.xib index 000fe534..3a19fff3 100644 --- a/Tusker/Views/Status/TimelineStatusTableViewCell.xib +++ b/Tusker/Views/Status/TimelineStatusTableViewCell.xib @@ -109,10 +109,16 @@ - + + + + + + +