使用CGAFFINETRANSFORM进行uiimageView错误旋转

发布于 2025-01-20 05:28:12 字数 1746 浏览 4 评论 0原文

我有 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)
      }
   }

用同样的方法,我尝试旋转 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)
            }
        }
     }

但使用这种方法,我面临几个问题:

  1. 虽然第一次点击它会根据需要将 V 形向下动画 90 度,但是当我再次点击时- 它旋转初始 V 形(面向右侧),从而导致 V 形向上查找的结果 (-pi/2)。如何修复该行为并从新位置旋转 V 形(从其面朝下回到初始右侧)?
  2. 当我尝试在第一次旋转后点击时,动画将永远不会再次触发。过渡是立即的。如何解决这个问题?

这是一个包含问题行为的 GIF:CLICK

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:

  1. 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)?
  2. 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2025-01-27 05:28:13

您误解了 transform 属性的含义。 transform 表示视图从其原始位置(在本例中面向右侧)的转换,因此将其设置为 CGAffineTransform(rotationAngle: -.pi / 2) 使视图变换为从其原始位置(即面朝上)旋转-.pi / 2弧度。

要实现所需的旋转,您可以使用视图的当前变换来组合-.pi/2旋转,或者仅设置transform.identity (从原始位置根本没有转换):

cell.chevronImage.transform = .identity
// or
cell.chevronImage.transform = cell.chevronImage.transform.rotated(by: -.pi / 2)

我怀疑您没有在 cellForRowAtIndexPath 中添加此旋转代码。 请这样做,只是没有动画。否则,您会遇到滚动表视图后 V 形重置等问题(另请参阅)。但是,这意味着当您调用 reloadSections 时,它会过早轮换。解决此问题的最简单方法是在动画之后移动 reloadSections

UIView.animate(withDuration: 1.0) {
    if sections[indexPath.section].isOpened {
        cell.chevronImage.transform = CGAffineTransform(rotationAngle: .pi/2)
    } else {
        cell.chevronImage.transform = .identity
    }
} completion: {_ in
    self.tableView.reloadSections([indexPath.section], with: .none)
}

You misunderstood what the transform property means. transform represents the transformation of the view from its original position (facing right, in this case), and so setting it to CGAffineTransform(rotationAngle: -.pi / 2) makes the view transform in such a way that it rotates -.pi / 2 radians from its original position, i.e. facing up.

To achieve the desired rotation, you can either compose this -.pi/2 rotation with the current transform of the view, or just set transform to .identity (no transform at all, from its original position):

cell.chevronImage.transform = .identity
// or
cell.chevronImage.transform = cell.chevronImage.transform.rotated(by: -.pi / 2)

I suspect that you haven't added this rotation code in cellForRowAtIndexPath. Please do that, just without the animation. Otherwise you would run into problems like the chevron resetting after scrolling the table view (see also). However, this would mean that it would be rotated too early, when you call reloadSections. The easiest way to solve this is to move reloadSections after the animation:

UIView.animate(withDuration: 1.0) {
    if sections[indexPath.section].isOpened {
        cell.chevronImage.transform = CGAffineTransform(rotationAngle: .pi/2)
    } else {
        cell.chevronImage.transform = .identity
    }
} completion: {_ in
    self.tableView.reloadSections([indexPath.section], with: .none)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文