旋转UIButton并保持它的大小



我正在尝试旋转按钮,形状为正方形(65 x 65)。在我旋转它们之后,按钮会改变它们的大小。有什么办法,使旋转,并保持大小?

代码:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
self.stealthButton.transform = CGAffineTransformMakeRotation(radians);
self.recButton.transform = CGAffineTransformMakeRotation(radians);
self.toggleButton.transform = CGAffineTransformMakeRotation(radians);
[UIView commitAnimations];

试着这样做也许会对你有帮助,

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    fullRotation.fromValue = [NSNumber numberWithFloat:0];
    fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
    fullRotation.duration = 1;
    fullRotation.repeatCount = 1;
    [button.layer addAnimation:fullRotation forKey:@"360"];

在你的项目中添加下面的框架。

#import <QuartzCore/QuartzCore.h>
#import "QuartzCore/CALayer.h"

它们不会改变大小。他们改变镜框的尺寸。

添加:

CGFrame oldFrame = self.stealthButton.frame;
//Do that for all the buttons
//Do your rotation
self.stealthButton.frame = oldFrame;

BUT:这样做实际上会缩小按钮的大小。从用户的角度来看,它会显得小得多。

确保将内容视图设置为居中

self.stealthButton.contentMode = UIViewContentModeCenter;
self.recButton.contentMode = UIViewContentModeCenter;
self.toggleButton.contentMode = UIViewContentModeCenter;

最新更新