旋转180度为UIImageView,并以相同的路线向后旋转

时间:2015-02-11 06:05:41

标签: ios objective-c swift xcode core-animation

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

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

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

旋转代码:

self.DashButton.imageView.transform = CGAffineTransformIdentity;

我试过

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

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

8 个答案:

答案 0 :(得分:17)

而不是-M_PI,请使用-3.14159。有点诡计,但是下面的代码节省了我的一天。

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

答案 1 :(得分:6)

你需要的是一个负角度来反向旋转

CGAffineTransformMakeRotation(180 * CGFloat(M_PI / 180))

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

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

答案 2 :(得分:6)

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

答案 3 :(得分:2)

@Maria工作答案的Swift 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
}

答案 4 :(得分:1)

试试这个

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

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

答案 5 :(得分:1)

怎么样?

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

它适用于我的代码:)

答案 6 :(得分:0)

也可以使用

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

答案 7 :(得分:-1)

斯威夫特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函数,用于处理start&停止旋转操作:

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 
}