2012-02-19 92 views
2

编辑问Autolayout是否有助于这种情况?固定位置旋转UILabel

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

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

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.

+0

的它在做什么帖子的图片和你想要它做什么。 – 2012-02-19 05:03:04

+0

很确定你将不得不手动(旋转)。你不能使用struts/spring来获得你想要的效果。 – gamozzii 2012-02-19 06:11:09

回答

3

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

您应该在willAnimateRotationToInterfaceOrientation:duration:中布置您的观点。作为the documentation状态:

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

你甚至可以动画您的意见,在此方法中的新位置。

1

需要在shouldAutorotateToInterfaceOrientation方法

2

,你可以执行上的标签(transformatin)旋转手动设置标签的框架位置值。在uiviewcontroller中通过interfaceorientation方法做到这一点。

uilabel.transform = CGAffineTransformMakeRotation(M_PI/-4);喜欢这个。在全部四个方向上执行此操作。有一件事情是从appdelegate的状态栏方向而不是设备方向。它将帮助您保持与视图方向同步。

1

创建两个UILabels,一个水平和其他垂直。改变方向时,隐藏并显示所需的标签。 要做到这一点,你必须把在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 
     } 
     .... 
}