如何在 Xcode + IB 中旋转 UIbutton?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2455334/
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
How to rotate a UIbutton in Xcode + IB?
提问by Tattat
I am writing an iPhone programer, and I want to make a button with is rotate 180 degree, I try to use the multi-touch track pad to rotate a UIbutton, but it don't success, how can I do it? or I need to do it by code?
我正在编写一个 iPhone 编程器,我想制作一个旋转 180 度的按钮,我尝试使用多点触控板来旋转 UI 按钮,但它没有成功,我该怎么做?还是我需要通过代码来完成?
采纳答案by Barrie Reader
Well, here we go:
好吧,我们开始:
CABasicAnimation *halfTurn;
halfTurn = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
halfTurn.fromValue = [NSNumber numberWithFloat:0];
halfTurn.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
halfTurn.duration = 0.5;
halfTurn.repeatCount = 1;
[myButton addAnimation:halfTurn forKey:@"180"];
Hope that helps... Im typing from my PC though, not my Mac - So I hope that's right!
希望有帮助...不过我是从我的 PC 上打字,而不是我的 Mac - 所以我希望那是对的!
回答by Macmade
You can't do it from Interface Builder. You have to rotate it from your code, using the transform property of your UIButton, which is a CGAffineTransform struct. You can use the CGAffineTransformMakeRotation() to set it.
你不能从 Interface Builder 中做到这一点。您必须使用您的 UIButton 的 transform 属性(它是一个 CGAffineTransform 结构)从您的代码中旋转它。您可以使用 CGAffineTransformMakeRotation() 来设置它。
myButton.transform = CGAffineTransformMakeRotation( ( 180 * M_PI ) / 180 );
The first 180 in the code is the angle in degrees. The operation converts it to radians.
代码中的第一个 180 是以度为单位的角度。该操作将其转换为弧度。
回答by Gavin Miller
I was wanting to do this same thing - rotate a button 180 degrees when tapped - but do it using an animation. Neurofluxation's answerdoes the 180 degree rotation with an animation, but it isn't permanent. Macmade's answerdoes the 180 degree rotation, but doesn't do it with an animation. So if you're like me and would like to do a 180 degree rotation with animation use this code:
我想做同样的事情 - 点击时将按钮旋转 180 度 - 但使用动画来完成。Neurofluxation 的答案是用动画进行 180 度旋转,但它不是永久性的。Macmade 的答案是进行 180 度旋转,但没有使用动画进行。因此,如果您像我一样想要使用动画进行 180 度旋转,请使用以下代码:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.35];
// (180 * M_PI) / 180 == M_PI, so just use M_PI
myButton.transform = CGAffineTransformMakeRotation(M_PI);
[UIView commitAnimations];
As well, if you want to rotate back to the starting position (ie 0 degree rotation) then put the following in between the animation code like above:
同样,如果您想旋转回起始位置(即 0 度旋转),则在上面的动画代码之间添加以下内容:
myButton.transform = CGAffineTransformMakeRotation(0);
As to a use case for such a button, Evernote's show/hide keyboard button does a really slick 180 degree rotation which can be recreatedusing the above code.
对于此类按钮的用例,Evernote 的显示/隐藏键盘按钮进行了非常流畅的 180 度旋转,可以使用上述代码重新创建。