目前我正在爲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;
}];
}
我已經嘗試過此方法,並且在使用基於通知的方法時遇到了更多問題。例如,繪圖區域的邊界和框架屬性不匹配。它也造成了一些不尋常的繪製異常。 – robhasacamera
你是對的;在視圖上存在變換時框架是未定義的,因此如果要變換它,則必須依賴邊界。 –