【发布时间】:2011-08-16 03:32:34
【问题描述】:
我有一个 UIViewController,它有一个图像视图和一个工具栏。我希望工具栏旋转,但图像视图保持原样。这可能吗?
【问题讨论】:
-
是什么触发了轮换?设备方向、设备指南针等?
标签: iphone uiviewcontroller rotation autorotate
我有一个 UIViewController,它有一个图像视图和一个工具栏。我希望工具栏旋转,但图像视图保持原样。这可能吗?
【问题讨论】:
标签: iphone uiviewcontroller rotation autorotate
是的,这是可能的,但需要手动处理旋转事件。
在viewDidLoad中,添加
// store the current orientation
currentOrientation = UIInterfaceOrientationPortrait;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(receivedRotate:) name: UIDeviceOrientationDidChangeNotification object: nil];
if(currentOrientation != self.interfaceOrientation) {
[self deviceInterfaceOrientationChanged:self.interfaceOrientation];
}
并且不要忘记在移除控制器时取消注册事件。 然后添加一个旋转的方法:
// This method is called by NSNotificationCenter when the device is rotated.
-(void) receivedRotate: (NSNotification*) notification
{
NSLog(@"receivedRotate");
UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
if(interfaceOrientation != UIDeviceOrientationUnknown) {
[self deviceInterfaceOrientationChanged:interfaceOrientation];
} else {
NSLog(@"Unknown device orientation");
}
}
最后是旋转方法
- (void)deviceInterfaceOrientationChanged:(UIInterfaceOrientation)interfaceOrientation {
if(interfaceOrientation == currentOrientation) {
NSLog(@"Do not rotate to current orientation: %i", interfaceOrientation);
} else if(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
NSLog(@"Do not rotate to UIInterfaceOrientationPortraitUpsideDown");
} else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(@"Do not rotate to UIInterfaceOrientationLandscapeLeft");
} else {
if(!isRotating)
{
isRotating = YES;
if(currentOrientation == UIInterfaceOrientationPortrait && interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
NSLog(@"Rotate to landscape");
// rotate to right top corner
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
// do your rotation here
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDoneShowCaption:finished:context:)];
[UIView commitAnimations];
} else if(currentOrientation == UIInterfaceOrientationLandscapeRight && interfaceOrientation == UIInterfaceOrientationPortrait) {
// etc
}
isRotating = NO;
} else {
NSLog(@"We are already rotating..");
}
}
currentOrientation = interfaceOrientation;
}
请注意,我不允许在某些方向上旋转,您可能会这样做。
此外,您需要使您的组件可调整大小/能够旋转。
编辑考虑改用block-based动画并在完成块中设置isRotation = NO。
【讨论】: