我想支持除纵向之外的所有方向。我想做一些简单的事情,但一直找不到解决方案。
我的界面中有 6 个大按钮。再加上 2 个额外的小按钮。
当方向改变时,我想将所有 8 个按钮保持在相同的中心/位置,我只是想旋转 6 个大按钮,这样它们就会面向正确的方向。
我尝试设置
- (BOOL)shouldAutorotate
{
return NO;
}
并向自己发送通知,但我必须处理旧方向与新方向才能旋转到正确的位置。还有其他的可能吗?另外,我永远无法获得以前的方向,因为通知是在方向更改后发送的(UIDeviceOrientationDidChangeNotification)
最佳答案
这是我在 View 旋转时用来旋转按钮图像的方法:
- (BOOL)shouldAutorotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDeviceOrientationDidChangeNot:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)handleDeviceOrientationDidChangeNot:(NSNotification *)not {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
CGFloat angle = 0.0;
switch (orientation) {
case UIDeviceOrientationPortrait:
angle = 0.0;
break;
case UIDeviceOrientationLandscapeLeft:
angle = M_PI/2;
break;
case UIDeviceOrientationPortraitUpsideDown:
angle = M_PI;
break;
case UIDeviceOrientationLandscapeRight:
angle = -M_PI/2;
break;
default:
return;
break;
}
[UIView animateWithDuration:0.35 animations:^{
self.someButton.imageView.transform = CGAffineTransformMakeRotation(angle);
} completion:^(BOOL finished) {
//
}];
}
关于iphone - 防止旋转过程中界面发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17906116/