我想推遲自動旋轉用戶界面,直到設備定位在一個方向幾秒鐘,而不是讓用戶瘋狂,並且每當他們傾斜設備錯誤地離軸幾度。UIViewController shouldAutorotateToInterfaceOrientation - 想要添加滯後
最接近我能得到這個(這絕不是我想要的東西,因爲它鎖定UI)是:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
[NSThread sleepForTimeInterval:2.0];
return YES;
}
我希望做的是使用這樣的 - 它的工作原理原則上,通過檢查控制檯日誌,但我需要已註釋掉的相應代碼行。
-(void) deferredAutorotateToInterfaceOrientation:(NSTimer *) timer {
autoRotationTimer = nil;
UIInterfaceOrientation interfaceOrientation = (UIInterfaceOrientation)[timer.userInfo integerValue];
NSLog(@"switching to new orientation %d now",interfaceOrientation);
// replace this with code to induce manual orientation switch here.
//[self forceAutoRotateToInterfaceOrientation:interfaceOrientation];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
[autoRotationTimer invalidate];
autoRotationTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self
selector:@selector(deferredAutorotateToInterfaceOrientation:) userInfo:[NSNumber numberWithInt:(int)interfaceOrientation ] repeats:NO];
NSLog(@"denying autorotate, deffering switch to orientation %d by 2 seconds",interfaceOrientation);
return NO;
}
我知道有很多,有時方法可以做到的事情,因此,如果這種做法是不是最有效的,有人可以建議另一種方式來做到這一點,我洗耳恭聽。我的主要標準是我想延遲自轉的開始,同時保持一個敏感的用戶界面,如果他們確實只是向左傾斜一點,因爲他們在一個公共汽車剛剛繞過一個角落等。
編輯:I找到了一個解決方案,可能不是應用程序商店友好的,但我離完成幾個星期,有人可能在此期間回答這個問題。這個作品調用了一個沒有記錄的方法。 (UIPrintInfoOrientation)類型轉換隻是爲了抑制編譯器警告,並不影響傳遞的值。
-(void) forceUIOrientationInterfaceOrientation:(UIDeviceOrientation) interfaceMode {
[(id)[UIDevice currentDevice] setOrientation:(UIPrintInfoOrientation) interfaceMode];
}
全面實施,其中包括重入否定如下:
正如我現在編輯的問題中提到的,我發現了一個臨時解決方案,可能不會隨應用商店一起飛,所以如果有人有更好的方法,請讓我知道。 – unsynchronized