旋转固定位置的UILabel

编辑询问Autolayout是否有助于这种情况?

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

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

这就是我在故事板中创建布局的方式,它有四个单独的UILabel。这就是景观的出现 - 而不是我想要的。这是我想看的风景,每个数字的旋转动画都以数字本身为中心。

vrk是在正确的轨道上与他的答案,但shouldAutorotateToInterfaceOrientation:不是正确的方法来布局你的标签。 当您收到shouldAutorotateToInterfaceOrientation: ,尚未更新任何新方向。

您应该在willAnimateRotationToInterfaceOrientation:duration:布置您的视图willAnimateRotationToInterfaceOrientation:duration: . 正如文件所述:

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

您甚至可以在此方法中将视图设置为新位置的动画

你可以在标签上进行旋转(transformatin)。 在uiviewcontroller中的didrotatefrom interfaceorientation方法中执行此操作。

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

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

创建两个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 } .... }