[英]iOS6 Device Rotation To Be Restricted
对于我的应用程序,我想让设备旋转但上下颠倒。 一切正常。 但是,我想阻止应用专门从以下位置旋转
左景观->右景观 -反之亦然
如果有人好奇,那是因为旋转使我的布局混乱,因为它们每个都从一个公共点旋转
我认为适用于iOS 5的代码如下:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
NSLog(@"Rotating");
if((lastOrient == 3 && toInterfaceOrientation == 4) || (lastOrient == 4 && toInterfaceOrientation == 3)){
lastOrient = toInterfaceOrientation;
return NO;
}
lastOrient = toInterfaceOrientation;
return YES;
}
其中3 =左景观,4 =右景观
有关如何在iOS6上执行此操作的任何建议? 还是完全不同的解决方案?
在ios6中不应该使用shouldAutorotateToInterfaceOrientation。 用这个:
- (BOOL)shouldAutorotate {
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (lastOrientation==UIInterfaceOrientationPortrait && orientation == UIInterfaceOrientationPortrait) {
return NO;
}
return YES;
}
尚未测试此代码。 您可以在这些职位上获得更多信息: iOS 6中 shouldAutorotateToInterfaceOrientation 不起作用在iOS 6 中不应该调用AutoAutoateateToInterfaceOrientation
好吧,我在这里回答了我自己的问题:
好消息是,绝对有办法做到这一点! 因此,这里是基础知识:
在iOS6中,通常由appDelegate处理应用程序是否可以旋转。 然后,当设备收到旋转信号时,它将向您的视图询问其支持的方向。 这是我实现代码的地方。 实际上,shouldAutorotate()在解决方案中不起作用。
因此,我创建了一个变量来跟踪最后的方向,并在其中进行更改
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
这样我可以比较方向
-(NSUInteger)supportedInterfaceOrientations
{
NSLog(@"Last Orient = %d", lastOrient);
NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
if (lastOrient != 3 && lastOrient != 4) {
NSLog(@"All good, rotate anywhere");
return UIInterfaceOrientationMaskAllButUpsideDown;
}
else if(lastOrient == 3){
orientations |= UIInterfaceOrientationMaskLandscapeRight;
NSLog(@"Can only rotate right");
}
else if(lastOrient == 4){
orientations |= UIInterfaceOrientationMaskLandscapeLeft;
NSLog(@"Can only rotate left");
}
return orientations;
}
似乎为我工作。 有点hack,但是它可以完成所需的工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.