我有4个部分,每个部分有2个嵌套行.我通过点击每个部分来打开行.
当我点击一个部分时,我希望它(chevron.right
,制作为UIImageView
)顺时针旋转90度,并带有平滑的动画,当我再次点击同一部分时,逆时针旋转90度.
我有一个名为isOpened
的变量(bool,默认情况下为false),我将其从false更改为true,并在didSelectRowAt
中每次点击后返回.基于此,显示所有嵌套单元:
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)
}
}
用同样的方法,我try 旋转V形:
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)
}
}
}
但采用这种方法,我面临两个问题:
- 第一次点击时,它会根据需要将V形向下设置90度的动画,但当我再次点击时,它会旋转initial个V形(面向右侧),这会导致V形向上看的结果(-pi/2).如何修复该行为并将V形从其新位置旋转(从其面部向下回到初始右侧)?
- 当我在第一次旋转后try 点击时,动画将不会再次触发.这种转变是立竿见影的.如何解决这个问题?
下面是一个带有问题行为的GIF:CLICK