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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28447599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:37:56  来源:igfitidea点击:

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

iosobjective-cswiftxcodecore-animation

提问by Wingzero

I got a button and want to rotate the image inside it:

我有一个按钮,想旋转里面的图像:

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

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

The image is a up-arrow to down-arrow after user click it When clicking the button again, I want the down-arrow rotate to up-arrow again, but it is the same counterclockwise rotation, but I just want the arrow reverse back to up-arrow

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

rotate back code:

旋转回代码:

self.DashButton.imageView.transform = CGAffineTransformIdentity;

self.DashButton.imageView.transform = CGAffineTransformIdentity;

I tried

我试过

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

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

What I want is not rotate the full circle, just 180 rotate and -180 rotate, not a 360 full rotate

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

回答by Wingzero

instead of -M_PI, use -3.14159. Kind of a trick, but below code saved my day.

使用 -3.14159 代替 -M_PI。有点技巧,但下面的代码拯救了我的一天。

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

回答by Si Fisher

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

回答by Maria

what you need is a negative angle to rotate it in the opposite direction

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

CGAffineTransformMakeRotation( 180 * CGFloat(M_PI/180))

CGAffineTransformMakeRotation(180 * CGFloat(M_PI/180))

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

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

take a look at this answer for an explanation About CGAffineTransformMakeRotation rotation direction?

看看这个答案的解释About CGAffineTransformMakeRotation rotation direction?

回答by Lance Samaria

Swift 4 version of @Maria working answer

Swift 4 版本的@Maria 工作答案

Rotate 180 degrees counter clockwise:

逆时针旋转 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))
}

Rotate back 180 degrees clockwise:

顺时针旋转 180 度:

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

回答by AdrianHor

try this

尝试这个

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

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

回答by Carien van Zyl

One can also use

一个也可以使用

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

回答by wsnjy

how about this?

这个怎么样?

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

it works on my code :)

它适用于我的代码:)

回答by Krunal

Swift 3:

斯威夫特 3:

Using CABasicAnimation

使用 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")


Here is an extensionfunctions for UIView that handles start & stop rotation operations:


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

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")
    }
}


Now using, UIView.animationclosure:


现在使用,UIView.animation关闭:

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