How to programmatically rotate the view by 180 deg

2019-03-13 02:56发布

How to programmatically rotate the view by 180 degrees in my iPhone App?

7条回答
迷人小祖宗
2楼-- · 2019-03-13 03:06

As ’CGAffineTransformRotate’ uses radians as its unit of measure, and 180 degrees is the same as PI, instead of the math provided in other answers you can simply do:

view.transform = CGAffineTransformRotate(view.transform, M_PI);

Swift 3:

view.transform = view.transform.rotated(by: .pi)

If you plan on doing a lot of transforms, it's probably best to read up on radians, so you understand what is going on.

查看更多
小情绪 Triste *
3楼-- · 2019-03-13 03:08

Should be possible by using CGAffineTransform

Quote from this question, this should do the trick:

CGFloat radians = atan2f(yourView.transform.b, yourView.transform.a);
CGFloat degrees = radians * (180 / M_PI);
CGAffineTransform transform = CGAffineTransformMakeRotation((90 + degrees) * M_PI/180);
yourView.transform = transform;
查看更多
We Are One
4楼-- · 2019-03-13 03:10

Simple solution:

view.transform = CGAffineTransformMakeRotation(degrees*M_PI/180);
查看更多
Bombasti
5楼-- · 2019-03-13 03:10

In Swift 3 :

let rotationDegrees =  180.0
let rotationAngle = CGFloat(rotationDegrees * M_PI / 180.0)
view.transform = CGAffineTransform(rotationAngle: rotationAngle)
查看更多
来,给爷笑一个
6楼-- · 2019-03-13 03:10

Latest Swift 3 syntax:

view.transform = view.transform.rotated(by: .pi)
查看更多
做个烂人
7楼-- · 2019-03-13 03:15

Same outcome as @Manny's answer using a different function.

 CGFloat degreesOfRotation = 180.0;
 view.transform = CGAffineTransformRotate(view.transform,
     degreesOfRotation * M_PI/180.0);
查看更多
登录 后发表回答