为 UIImageView 旋转 180 度,并以相同的路线旋转回来

rotate 180 degree for an UIImageView, and rotate back with same route

我有一个按钮,想旋转其中的图像:

self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI);

图像是用户点击后向上箭头到向下箭头 再次单击按钮时,我希望向下箭头再次旋转到向上箭头,但它是相同的逆时针旋转,但我只是希望箭头反转回向上箭头

轮回代码:

self.DashButton.imageView.transform = CGAffineTransformIdentity;

我试过了

self.DashButton.imageView.transform = CGAffineTransformMakeRotation(-M_PI);

我想要的不是整圈旋转,只是180度旋转和-180度旋转,而不是360度整圈旋转

试试这个

arrowLeft.transform = CGAffineTransformMakeRotation(180 *M_PI / 180.0);

arrowLeft.transform = CGAffineTransformMakeRotation(0*M_PI/180);

而不是 -M_PI,使用 -3.14159。有点技巧,但下面的代码拯救了我。

self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI);
self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI - 3.14159);

你需要的是一个负角,让它向相反的方向旋转

CGAffineTransformMakeRotation( 180 * CGFloat(M_PI/180))

CGAffineTransformMakeRotation(-1 * CGFloat(M_PI/180))

查看此答案以获得解释 About CGAffineTransformMakeRotation rotation direction?

self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI);
self.DashButton.imageView.transform = CGAffineTransformMakeRotation(0);

一个也可以用

imageView.image = UIImage(cgImage: imageView.image!.cgImage! , scale: 1.0, orientation: .down)

这个怎么样?

self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI);
self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI * 2);

它适用于我的代码:)

Swift 3:

使用CABasicAnimation

var rotationAnimation = CABasicAnimation()
rotationAnimation = CABasicAnimation.init(keyPath: "transform.rotation.z")
rotationAnimation.toValue = NSNumber(value: (Double.pi))
rotationAnimation.duration = 1.0
rotationAnimation.isCumulative = true
rotationAnimation.repeatCount = 100.0
view.layer.add(rotationAnimation, forKey: "rotationAnimation")


这是处理开始和停止旋转操作的 UIView 的 extension 函数:

extension UIView {

    func startRotation() {
        let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotation.fromValue = 0
        rotation.toValue = NSNumber(value: Double.pi)
        rotation.duration = 1.0
        rotation.isCumulative = true
        rotation.repeatCount = FLT_MAX
        self.layer.add(rotation, forKey: "rotationAnimation")
    }

    func stopRotation() {
        self.layer.removeAnimation(forKey: "rotationAnimation")
    }
}


现在使用,UIView.animation 闭包:

UIView.animate(withDuration: 0.5, animations: { 
      button.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi))) 
}) { (isAnimationComplete) in
    // Animation completed 
}

Swift @Maria 工作答案的 4 个版本

逆时针旋转180度:

let transform = CGAffineTransform.identity

UIView.animate(withDuration: 0.3, animations: {
      // to rotate counterclockwise set both of these in this exact order
      self.button.transform = transform.rotated(by: 180 * CGFloat(Double.pi))
      self.button.transform = transform.rotated(by: -1 * CGFloat(Double.pi))
}

顺时针向后旋转180度:

UIView.animate(withDuration: 0.3, animations: { 
      self.button.transform = CGAffineTransform.identity
}