[英]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.