2011-11-13 14 views
0

目前我正在爲iPad繪圖應用程序工作。我需要重新定位和旋轉應用程序中的工具欄,當它放入不同的方向時,同時將繪圖區域保留在同一個位置。手動旋轉方法與側開關斷開

我發現這樣做的方法here。它使用NSNotificationCenter監視輪換更改。這將調用一個自定義的didRotate:方法,該方法將根據UIDeviceOrientation旋轉並重新定位我的工具欄。

這部分工作正常。但是,只要iPad上的側面開關用於鎖定方向,工具欄就會重新定位到該位置。

例如:如果我開始景觀離開應用程序並將其旋轉爲縱向,工具欄將重新定位到屏幕的底部。但是,只要我使用滑動開關,它就會移動到屏幕左側,以便橫向放置。

我用這個方法都低於。

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil]; 
} 

- (void)didRotate:(NSNotification *)notification { 
    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation]; 

    UIInterfaceOrientation interfaceOrientation; 

    bool orientationFound = YES; 

    if (deviceOrientation == UIDeviceOrientationPortrait) { 
     interfaceOrientation = UIInterfaceOrientationPortrait; 
    } else if (deviceOrientation == UIDeviceOrientationPortraitUpsideDown) { 
     interfaceOrientation = UIInterfaceOrientationPortraitUpsideDown; 
    } else if (deviceOrientation == UIDeviceOrientationLandscapeLeft) { 
     interfaceOrientation = UIInterfaceOrientationLandscapeRight; 
    } else if (deviceOrientation == UIDeviceOrientationLandscapeRight) { 
     interfaceOrientation = UIInterfaceOrientationLandscapeLeft; 
    } else { 
     orientationFound = NO; 
    } 



    if (orientationFound) { 
     [self.toolbar changeToOrientation:interfaceOrientation withDuration:.25]; 
     [self.tutorialOverlay changeToOrientation:interfaceOrientation]; 
    } 
} 

- (void)changeToOrientation:(UIInterfaceOrientation)orientation withDuration:(float)duration { 
    float angle; 
    CGPoint origin; 

    if (orientation == UIInterfaceOrientationPortrait) { 
     angle = portraitAngle; 
     origin = self.portraitOrigin; 
    } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) { 
     angle = portraitUpsideDownAngle; 
     origin = self.portraitUpsideDownOrigin; 
    } else if (orientation == UIInterfaceOrientationLandscapeLeft) { 
     angle = landscapeLeftAngle; 
     origin = self.landscapeLeftOrigin; 
    } else if (orientation == UIInterfaceOrientationLandscapeRight) { 
     angle = landscapeRightAngle; 
     origin = self.landscapeRightOrigin; 
    } 

    [UIView animateWithDuration:duration animations:^{ 
     self.transform = CGAffineTransformMakeRotation(angle); 

     CGRect rect = self.frame; 

     rect.origin = origin; 

     self.frame = rect; 
    }]; 
} 

回答

1

我強烈建議不要使用這種基於通知的方法。請注意,通知的名稱是UIDeviceOrientationDidChangeNotification;設備的方向與界面方向不一樣,導致很多這樣的小問題。 (該裝置取向,例如,包括UIDeviceOrientationFaceDown,這是從未與接口取向相關聯。)

我建議讓你的視圖控制器自動旋轉本身;這會爲你放置工具欄等。然後您可以覆蓋- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration將繪圖區域返回到到您想要維護的方向。 (你可以在上面的changeToOrientation方法中使用類似的代碼,但是對於繪製區域,而不需要創建自己的動畫塊。而且,角度都將被否定,因爲您正在撤消更改由視圖控制器)。

+0

我已經嘗試過此方法,並且在使用基於通知的方法時遇到了更多問題。例如,繪圖區域的邊界和框架屬性不匹配。它也造成了一些不尋常的繪製異常。 – robhasacamera

+0

你是對的;在視圖上存在變換時框架是未定義的,因此如果要變換它,則必須依賴邊界。 –

1

我剛纔已經回答了類似的問題here。 基本上只允許界面旋轉,但旋轉你不想在willRotateToInterfaceOrientation:duration:「旋轉」的視圖。