2011-10-03 41 views
0

我想推遲自動旋轉用戶界面,直到設備定位在一個方向幾秒鐘,而不是讓用戶瘋狂,並且每當他們傾斜設備錯誤地離軸幾度。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]; 
} 

全面實施,其中包括重入否定如下:

​​
+0

正如我現在編輯的問題中提到的,我發現了一個臨時解決方案,可能不會隨應用商店一起飛,所以如果有人有更好的方法,請讓我知道。 – unsynchronized

回答

0

要使用公共API做到這一點,你可能會擁有大約自轉給忘了,做你自己的看法手動轉換基於過濾(不只是延遲!)加速度計輸入。

+0

決定完全刪除此功能,因爲如果問題成爲問題,用戶可以鎖定旋轉。蘋果可能會決定在內部將這種功能集成到操作系統中,這可能是它所屬的地方,而不是每個應用程序的基礎。 – unsynchronized

0

我沒有測試過這一點,它可能不會在所有的工作,但你可以試試這個:

然後開始了self.rotate = NO;

- (void)shouldRotateTo:(UIInteraceOrientation *)interfaceOrientation { 
    self.rotate = YES; 
    // or test interfaceOrientation and assign accordingly. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    bool rot = self.rotate; 
    self.rotate = NO 
    [self performSelector:selector(shouldRotateTo:) withObject:[NSNumber numberWithInt:interfaceOrientation] afterDelay:2.0]; 
    return rot; 
} 

參數interfaceOrientation是一個枚舉(UIInteraceOrientation),所以把它包在NSNumber傳球時。