旋转 UIViewController 以抵消 UIInterfaceOrientation 的变化
我对此进行了很多搜索,但找不到任何可以帮助我的东西。
我有一个 UIViewController 包含在另一个 UIViewController 中。当父 UIViewController 旋转时,比如从 Portrait 旋转到 LandscapeLeft,我想让它看起来好像子 UIViewController 没有旋转。也就是说。我希望孩子对天空有相同的方向,无论父母的方向如何。如果它有一个在纵向中直立的 UIButton,我希望按钮的右侧在 UIInterfaceOrientationLandscapeLeft 中“向上”。
这可能吗?目前,我正在做这样非常恶心的事情:
-(void) rotate:(UIInterfaceOrientation)fromOrientation: toOr:(UIInterfaceOrientation)toOrientation
{
if(((fromOrientation == UIInterfaceOrientationPortrait) && (toOrientation == UIInterfaceOrientationLandscapeRight))
|| ((fromOrientation == UIInterfaceOrientationPortraitUpsideDown) && (toOrientation == UIInterfaceOrientationLandscapeLeft)))
{
}
if(((fromOrientation == UIInterfaceOrientationLandscapeRight) && (toOrientation == UIInterfaceOrientationPortraitUpsideDown))
|| ((fromOrientation == UIInterfaceOrientationLandscapeLeft) && (toOrientation == UIInterfaceOrientationPortrait)))
{
}
if(((fromOrientation == UIInterfaceOrientationPortrait) && (toOrientation == UIInterfaceOrientationLandscapeLeft))
|| ((fromOrientation == UIInterfaceOrientationPortraitUpsideDown) && (toOrientation == UIInterfaceOrientationLandscapeRight)))
{
}
if(((fromOrientation == UIInterfaceOrientationLandscapeLeft) && (toOrientation == UIInterfaceOrientationPortraitUpsideDown))
|| ((fromOrientation == UIInterfaceOrientationLandscapeRight) && (toOrientation == UIInterfaceOrientationPortrait)))
{
}
if(((fromOrientation == UIInterfaceOrientationPortrait) && (toOrientation == UIInterfaceOrientationPortraitUpsideDown))
|| ((fromOrientation == UIInterfaceOrientationPortraitUpsideDown) && (toOrientation == UIInterfaceOrientationPortrait)))
{
}
if(((fromOrientation == UIInterfaceOrientationLandscapeLeft) && (toOrientation == UIInterfaceOrientationLandscapeRight))
|| ((fromOrientation == UIInterfaceOrientationLandscapeRight) && (toOrientation == UIInterfaceOrientationLandscapeLeft)))
{
}
}
这看起来完全是对代码的浪费。此外,我计划使用 CGAffineTransform (如此处引用的: http://www.crystalminds.nl/ ?p=1102),但我很困惑是否应该更改视图的尺寸以匹配旋转后的尺寸。
这里最大的噩梦是您必须跟踪全局“方向”变量。如果不这样做,幻觉就会消失,ViewController 就会旋转成任何东西。
我真的需要一些帮助,谢谢!
I've been searching a lot on this, and can't find anything to help me.
I have a UIViewController contained within another UIViewController. When the parent UIViewController rotates, say from Portrait to LandscapeLeft, I want to make it look as though the child didn't rotate. That is to say. I want the child to have the same orientation to the sky regardless of the parent's orientation. If it has a UIButton that's upright in Portrait, I want the right-side of the button to be "up" in UIInterfaceOrientationLandscapeLeft.
Is this possible? Currently, I'm doing really gross stuff like this:
-(void) rotate:(UIInterfaceOrientation)fromOrientation: toOr:(UIInterfaceOrientation)toOrientation
{
if(((fromOrientation == UIInterfaceOrientationPortrait) && (toOrientation == UIInterfaceOrientationLandscapeRight))
|| ((fromOrientation == UIInterfaceOrientationPortraitUpsideDown) && (toOrientation == UIInterfaceOrientationLandscapeLeft)))
{
}
if(((fromOrientation == UIInterfaceOrientationLandscapeRight) && (toOrientation == UIInterfaceOrientationPortraitUpsideDown))
|| ((fromOrientation == UIInterfaceOrientationLandscapeLeft) && (toOrientation == UIInterfaceOrientationPortrait)))
{
}
if(((fromOrientation == UIInterfaceOrientationPortrait) && (toOrientation == UIInterfaceOrientationLandscapeLeft))
|| ((fromOrientation == UIInterfaceOrientationPortraitUpsideDown) && (toOrientation == UIInterfaceOrientationLandscapeRight)))
{
}
if(((fromOrientation == UIInterfaceOrientationLandscapeLeft) && (toOrientation == UIInterfaceOrientationPortraitUpsideDown))
|| ((fromOrientation == UIInterfaceOrientationLandscapeRight) && (toOrientation == UIInterfaceOrientationPortrait)))
{
}
if(((fromOrientation == UIInterfaceOrientationPortrait) && (toOrientation == UIInterfaceOrientationPortraitUpsideDown))
|| ((fromOrientation == UIInterfaceOrientationPortraitUpsideDown) && (toOrientation == UIInterfaceOrientationPortrait)))
{
}
if(((fromOrientation == UIInterfaceOrientationLandscapeLeft) && (toOrientation == UIInterfaceOrientationLandscapeRight))
|| ((fromOrientation == UIInterfaceOrientationLandscapeRight) && (toOrientation == UIInterfaceOrientationLandscapeLeft)))
{
}
}
which seems like a perfectly good waste of code. Furthermore, I was planning on using CGAffineTransform (like cited here: http://www.crystalminds.nl/?p=1102) but I'm confused about whether I should change the view's dimensions to match what they will be after the rotation.
The big nightmare here is that you have to keep track of a global "orientation" variable. If you don't, the illusion is lost and the ViewController is rotated into whatever.
I could really use some help on this, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您能做的最好的事情就是根据您的界面方向更改您的子视图框架的框架。你可以这样做:
希望这有帮助。
The best thing you can do is to change the frames of ur subview frames according to ur interface orientations. You can do it like:
Hope this helps.
我意识到了一些事情..假设我们的项目有多层 ViewController (就像您将其他视图控制器的子视图添加到您的视图控制器中一样)
willRotateToInterfaceOrientation:duration 方法将不会为 ViewController 的第二层调用...
所以我什么所做的是,在我从最顶层初始化我的第二层视图控制器之后,然后当在最顶层调用 willRotateToInterfaceOrientation:duration 方法时,我也会为第二层视图控制器调用 willRotateToInterfaceOrientation:duration
i realised something.. let's say our project have multiple layer of ViewController (as in if you add subview of other view controller to your view controller)
willRotateToInterfaceOrientation:duration method will not be called for the 2nd layer of ViewController...
so what i did was, after i initialise my 2nd layer view controller from my top most layer, then when willRotateToInterfaceOrientation:duration method is called on the top-most layer, i will call willRotateToInterfaceOrientation:duration for 2nd layer view controller as well