I have 4 sections, each section have 2 nested rows. I open the rows by tapping on each section.
When I tap on a section I want it accessory (chevron.right, made as UIImageView) be rotated clockwise by 90 degrees with a smooth animation and when I click again on the same section - rotate back, counterclockwise by 90 degrees.
I have a variable called isOpened (bool, false by default), which I change from false to true and back each tap in didSelectRowAt. Based on that a show all nested cells:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
sections[indexPath.section].isOpened = !sections[indexPath.section].isOpened
tableView.reloadSections([indexPath.section], with: .none)
}
}
In the same method I try to rotate the chevron:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
sections[indexPath.section].isOpened = !sections[indexPath.section].isOpened
tableView.reloadSections([indexPath.section], with: .none)
guard let cell = tableView.cellForRow(at: indexPath) as? MainSortTableViewCell else { return }
if sections[indexPath.section].isOpened {
UIView.animate(withDuration: 1.0) {
cell.chevronImage.transform = CGAffineTransform(rotationAngle: .pi/2)
}
} else {
UIView.animate(withDuration: 1.0) {
cell.chevronImage.transform = CGAffineTransform(rotationAngle: -.pi/2)
}
}
}
But with this approach I face a couple of problems:
- While first tap it animates chevron down on 90 degrees as I need, but when I tap again - it rotates initial chevron (face to the right) which causes a result as chevron looks up instead (-pi/2). How to fix the behaviour and rotate chevron from its new position (from when its face is down back to initial right)?
- When I try to tap after first rotation the animation will be never triggered again. The transition is immediate. How to fix that?
Here is a GIF with the problem behaviour: CLICK