旋转固定位置的UILabel

时间:2012-02-19 04:57:29

标签: iphone objective-c ios ipad autolayout

编辑以询问Autolayout在这种情况下是否有帮助?

如何让我的UILabel在屏幕上保持固定位置但仍然可以旋转? shouldAutorotateToInterfaceOrientation返回YES并且标签正好旋转,但无论我如何在Xcode中设置弹簧,弹簧标签的位置都会发生变化。

第一张图片展示了我如何在故事板中创建布局,其中包含四个单独的UILabel。 第二张图片展示了景观是如何出现的 - 而不是我想要的。 第三张图显示了我希望如何看待风景,每个数字的旋转动画都以数字本身为中心。

This is how I've created the layout in storyboard, with four individual UILabels. This is how landscape appears -- not what I want. Here is what I would like landscape to look, with the rotation animation of each number centered on the number itself.

4 个答案:

答案 0 :(得分:3)

vrk在他的回答的正确轨道上,但shouldAutorotateToInterfaceOrientation:不是布置标签的正确方法。当您收到shouldAutorotateToInterfaceOrientation:时,尚未针对新方向更新任何内容。

您应该在willAnimateRotationToInterfaceOrientation:duration:中列出您的观点。正如the documentation所述:

  

调用此方法时,interfaceOrientation属性已设置为新方向,并且视图的边界已更改。因此,您可以在此方法中执行视图所需的任何其他布局。

您甚至可以在此方法中将您的观看动画到新的位置。

答案 1 :(得分:2)

您可以在标签上执行旋转(transformatin)。在uiviewcontroller中的didrotate来自interfaceorientation方法。

uilabel.transform = CGAffineTransformMakeRotation(M_PI / -4);像这样。在所有四个方向上执行此操作。除了设备方向之外,有一件事是从appdelegate获取状态栏方向。它将帮助您与视图方向保持同步。

答案 2 :(得分:1)

您需要在shouldAutorotateToInterfaceOrientation方法中手动设置标签的帧位置值

答案 3 :(得分:1)

创建两个UILabel,一个是水平的,另一个是垂直的。更改方向时隐藏并显示所需的标签。 为此,您必须放入viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

并实施

- (void) didRotate:(NSNotification *)notification{

       UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

       if (orientation == UIDeviceOrientationLandscapeLeft)
       {

          // show Horizontal Label
       }
       ....
}
相关问题