【问题标题】:Rotate UIButton on change of orientation改变方向时旋转 UIButton
【发布时间】:2016-08-24 13:31:14
【问题描述】:

我有一个圆形按钮,由下图组成

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

我怎样才能做到这一点,

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

【问题讨论】:

  • 你试过改变图像的方向吗?
  • 我不想这样做,我不想为不同的方向保留四个不同的图像。

标签: ios objective-c uibutton uideviceorientation


【解决方案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];
}

【讨论】:

    【解决方案2】:

    要旋转图像视图试试这个

    // 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;
    

    【讨论】:

    • 很抱歉我没有 UIImageView 我有一个带图片的 UIButton!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2015-04-09
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    相关资源
    最近更新 更多