如何在iPhone SDK中旋转UIButton

4

alt text

如何在iPhone SDK中旋转UIButton......

5个回答

9
如果你有一个 IBOutlet 对象。
theButton.transform = CGAffineTransformMakeRotation(M_PI / -4);

如果您想在视图上创建运行时按钮。

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setFrame:CGRectMake(100, 100, 200, 44)];
    btn.transform=CGAffineTransformMakeRotation(M_PI / -4);
    [btn setTitle:@"RakeshBhatt" forState:UIControlStateNormal];
    [self.view addSubview:btn];
}

1
为什么不直接使用 M_PI 呢 :/ - Douwe Maan
@Dauwe Maan - 好的 - 我使用了M_PI。我同意使用M_PI更好。 - sagarkothari

4
theButton.transform = CGAffineTransformMakeRotation(-M_PI / 4);

(注:π/4 等于 45°。)

不行!请检查一下——这会导致错误的旋转,与示例图像不符。 - sagarkothari
@sugar:啊,UIKit的坐标是相反的方向。请查看更新。 - kennytm

2

最近我做了这个,我把5个按钮以不同的角度旋转。

以下是示例:

UIButton *typewritterButton = [UIButton buttonWithType:UIButtonTypeCustom];             

typewritterButton.frame = CGRectMake(15, 165, 130, 25);

typewritterButton.transform = CGAffineTransformMakeRotation((0.0174)*135);

[m_overlay addSubview:typewritterButton];

这一定适用于您。

使用此代码:CGAffineTransformMakeRotation((0.0174)*135);

只需更改值为135,即您想要旋转对象的角度。

如果有任何问题,请告诉我。


1
旋转至度数。
#define degreesToRadians(x) (M_PI * (x) / 180.0)

然后使用 / 90 是度数:
button.transform=CGAffineTransformMakeRotation(degreesToRadians(90));

1
Swift版本:

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接