【问题标题】:How to rotate a UISwitch in Xcode?如何在 Xcode 中旋转 UISwitch?
【发布时间】:2014-08-07 18:51:16
【问题描述】:

我的应用程序中有一个 UISwitch。默认情况下,它是水平的。我可以旋转开关使其垂直吗?我无意将其提交给 App Store,因此 Apple 的 HIG 在我的情况下无关紧要。

【问题讨论】:

    标签: ios xcode view storyboard uiswitch


    【解决方案1】:

    当然,尝试更改开关层属性的 Transform 属性。应该看起来像这样。

    #define DEGREES_TO_RADIANS(__ANGLE) ((__ANGLE) / 180.0 * M_PI)
    
    UISwitch *aSwitch = [[UISwitch alloc] init];
    aSwitch.layer.transform = CATransform3DMakeRotation(DEGREES_TO_RADIANS(90), 0, 0, 1);
    aSwitch.frame = CGRectMake(50, 50, aSwitch.frame.size.width, aSwitch.frame.size.height);
    [self.view addSubview:aSwitch];
    

    您也可以使用 CGAffineTransform 进行编辑,因此您无需访问图层属性。

    所以不是

    aSwitch.layer.transform = CATransform3DMakeRotation(DEGREES_TO_RADIANS(90), 0, 0, 1);
    

    你会有

    self.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(90));
    

    【讨论】:

    • 我怎样才能访问开关层属性?谢谢。
    • 确保您已将 QuartzCore.framework 添加到您的项目并使用 #import 导入它
    【解决方案2】:

    假设您的 UISwitch 被称为“switchControl”并通过 Interface Builder 连接,只需编写:

    self.switchControl.transform = CGAffineTransformMakeRotation(M_PI_2); // 90 degrees
    

    对弧度使用度数,反之亦然:

    /** Degrees to Radian **/
    #define degreesToRadians( degrees ) ( ( degrees ) / 180.0 * M_PI )
    
    /** Radians to Degrees **/
    #define radiansToDegrees( radians ) ( ( radians ) * ( 180.0 / M_PI ) )
    
    self.switchControl.transform = CGAffineTransformMakeRotation(degreesToRadians(90)); // 90 degrees
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 2023-04-08
      • 2011-11-13
      • 2021-09-18
      • 1970-01-01
      • 2012-12-27
      • 2019-03-22
      相关资源
      最近更新 更多