在改变方向时旋转UIButton

时间:2016-04-29 11:41:49

标签: ios objective-c uibutton uideviceorientation

我有圆形按钮,包含以下图像

现在,在旋转设备时,我希望以图像保持方向的方式旋转按钮。

我怎样才能做到这一点,

我不想改变按钮的框架,我希望它保持原状我只想转换按钮。

2 个答案:

答案 0 :(得分:1)

如果您有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 :(得分:0)

要旋转图像视图,请尝试以下

// Set the anchor point and center so the view swings from the upper right
imageView.layer.anchorPoint = CGPointMake(1.0, 0.0);
imageView.center = CGPointMake(CGRectGetWidth(self.view.bounds), 0.0);

// Rotate 90 degrees to hide it off screen
CGAffineTransform rotationTransform = CGAffineTransformIdentity;
rotationTransform = CGAffineTransformRotate(rotationTransform, DegreesToRadians(90));
swingView.transform = rotationTransform;