防止界面在旋转期间发生变化

时间:2013-07-28 07:47:00

标签: iphone ios objective-c orientation

我想支持所有方向,但肖像。我想做一些简单但我从来没有找到解决方案。

我的界面中有6个大按钮。还有2个额外的小按钮。

当方向改变时,我想将所有8个按钮保持在同一个中心/位置,我只想旋转6个大按钮,这样它们就会朝向正确的方向。

我尝试过设置

- (BOOL)shouldAutorotate
{
    return NO;
}

并向我发送通知,但我必须处理旧方向与新方向,以便旋转到正确的位置。还有其他可能性吗?此外,由于在方向更改后发送通知(UIDeviceOrientationDidChangeNotification)

,我无法获得先前的方向

1 个答案:

答案 0 :(得分:1)

这是我在视图旋转时用来旋转按钮图像的方法:

- (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) {
        //
    }];
}