【问题标题】:How do I rotate a UIImage and then rotate it back in reverse?如何旋转 UIImage 然后反向旋转它?
【发布时间】:2020-03-02 21:40:48
【问题描述】:

我有一个带有 UIImageView 的 UITableViewCell。当用户在我的表格视图中点击单元格时,会调用 cell.isExpanded.toggle() 来旋转图像。

现在它从 0 旋转到 180,顺时针,但是当再次点击时,它从 180 旋转到 0,也是顺时针。

当值为false时如何让它逆时针旋转180到0?

class TVCell: UITableViewCell {

    var isExpanded = false {
        didSet {
            rotateImage(isExpanded)
        }
    }

    let iv = UIImageView()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        addSubview(iv)
        iv.center.x = 33
        iv.center.y = 6
        iv.frame.size = CGSize(width: 30, height: 30)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func rotateImage(_ val: Bool) {
        UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseInOut, animations: {
            if val {
                self.iv.transform = CGAffineTransform(rotationAngle: .pi)
            } else {
                self.iv.transform = .identity
            }
        }, completion: nil)
    }

}

【问题讨论】:

    标签: swift uikit cgaffinetransform


    【解决方案1】:

    在这种情况下,CGFloat.pi-CGFloat.pi 在同一位置进行动画处理。

    -(CGFloat.pi * 0.999)可以逆时针旋转

    self.iv.transform = CGAffineTransform(rotationAngle:-(CGFloat.pi * 0.999))
    

    【讨论】:

      【解决方案2】:

      这是我发现的最简单且效果最好的方法

      UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseInOut, animations: {
          let rotationAngle: CGFloat = val ? .pi : (-.pi * 2)
          self.iv.transform = .init(rotationAngle: rotationAngle)
      }, completion: nil)
      

      【讨论】:

        猜你喜欢
        • 2013-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 2021-08-04
        • 2013-09-27
        • 1970-01-01
        相关资源
        最近更新 更多