2015-08-27 25 views
2

我想複製本機相機應用程序的確切行爲(包括其導航到畫廊和回)在設備旋轉時,UI控件旋轉的地方,而不是整個屏幕旋轉。我能夠通過鎖定在肖像模式屏幕和手動搬運設備旋轉的通知這樣的複製旋轉行爲:相機應用程序如屏幕旋轉和導航

- (BOOL)shouldAutorotate { 
    return NO; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { 
    return UIInterfaceOrientationPortrait; 
} 

- (void)orientationDidChange:(NSNotification*)note { 
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; 

    UIInterfaceOrientation newOrientation = UIInterfaceOrientationPortrait; 
    switch (orientation) { 
    case UIDeviceOrientationPortraitUpsideDown: 
     newOrientation = UIInterfaceOrientationPortraitUpsideDown; 
     break; 

    case UIDeviceOrientationLandscapeLeft: 
     newOrientation = UIInterfaceOrientationLandscapeLeft; 
     break; 

    case UIDeviceOrientationLandscapeRight: 
     newOrientation = UIInterfaceOrientationLandscapeRight; 
     break; 

    case UIDeviceOrientationPortrait: 
     newOrientation = UIInterfaceOrientationPortrait; 
     break; 

    default: 
     newOrientation = self.currentOrientation; 
     break; 
    } 

    if (newOrientation == self.currentOrientation) { 
    return; 
    } 
    self.currentOrientation = newOrientation; 
    [self rotateInterfaceToOrientation:self.currentOrientation]; 
} 

- (void)rotateInterfaceToOrientation:(UIInterfaceOrientation)orientation { 
    double rotationAngle = 0; 
    switch (orientation) { 
    case UIInterfaceOrientationPortraitUpsideDown: rotationAngle = M_PI; break; 
    case UIInterfaceOrientationLandscapeLeft: rotationAngle = M_PI_2; break; 
    case UIInterfaceOrientationLandscapeRight: rotationAngle = -M_PI_2; break; 
    default: rotationAngle = 0; break; 
    } 

    CGFloat angle = (float)rotationAngle; 
    self.defaultTransform = CGAffineTransformMakeRotation(angle); 

    ... manual animation by setting transform 
} 

這種運作良好,準確地表現爲我所需要的。

我的問題是根據應用程序的屏幕仍然是肖像的事實。

整個應用程序支持縱向和橫向模式。當我導航到不同的屏幕並返回時,由於它從橫向視圖轉換爲縱向視圖,轉換已中斷。就在過渡動畫開始之前,前一個風景視圖將佈局更改爲肖像(雖然它被異常拉伸)。來自模擬器的視頻:http://gfycat.com/DeafeningGaseousBrant。轉換開始時您可以看到佈局更改。它在設備上更加明顯,因爲您可以一直看到屏幕。可能值得一提的是,我使用自定義過渡管理器在瀏覽時使屏幕正確朝向(這可能解釋爲什麼視圖會像移動一樣移動,但它對有問題的行爲沒有影響)。

此外,當我用鍵盤或UIAlertView顯示提示時,它們的方向是錯誤的。再次模擬器:http://gfycat.com/SelfreliantPointedEwe

是否有任何方式從視圖控制器指定該視圖當前是縱向還是橫向?或者有沒有辦法讓屏幕旋轉手動,而不使用自動佈局調整大小/佈局?

回答

1

通過旋轉和變形視圖來應用旋轉。如果您應用反轉並對您的視圖進行整形,它將顯示爲不旋轉。

通過重塑,我的意思是交換寬度和高度。

當旋轉到橫向時,應用旋轉視圖( - )90度並手動交換邊界寬度和高度的變換。您的視圖將不再出現旋轉,但界面方向不會受到影響。同樣以相反的角度旋轉任何子視圖,並移動它們以匹配重塑的邊界。

+0

我在其他問題閱讀該解決方案,但我不敢相信這是正確的方法。當使用自動佈局時,無需基於設備方向對約束進行復雜編輯,就不可能達到想要的效果。 其中一件事是,當您將設備旋轉到左側橫向時,觸發按鈕位於右側。當你將它旋轉到正確的風景時,它位於左側。這僅僅是自動佈局無法完成​​的。必須有一些其他的魔法發生。 – Montas

1

在當設備旋轉時,UI控件到位旋轉全屏幕旋轉

其實不是,這不是發生了什麼。應用程序確實是旋轉 - 但元素旋轉,並且它們使用animateAlongside,因此它們的旋轉與應用程序的旋轉同步。你不能說應用程序正在旋轉,因爲沒有狀態欄來表明這一點。

+3

那麼,Camera.app的實際做法有點複雜,因爲它們在旋轉鎖定時也旋轉了東西。 –

+0

@FilipRadelic我沒有注意到。這實際上非常棒。所以看起來他們確實不會將屏幕旋轉到風景。但是如何在從圖庫轉換到圖庫時使導航工作正常? – Montas

+0

@Montas我並不是暗示他們不旋轉屏幕。當旋轉鎖定關閉時(您可以確認因爲控制中心和通知中心正常工作),他們肯定會這樣做,只是旋轉鎖定會讓它變得更加複雜。 –

0

看起來從預覽打開的Apple庫不是導航打開的視圖控制器,這是模式打開的視圖控制器,它甚至從預覽打開。只有當你觸摸按鈕「所有照片」,只有在這一刻加載初始照片應用程序,與真正的導航欄... (我檢查了在iOS 9)