0

我想支持除纵向之外的所有方向。我想做一些简单的事情,但我从来没有找到解决方案。

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

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

我试过设置

- (BOOL)shouldAutorotate
{
    return NO;
}

并向自己发送通知,但我必须处理旧方向与新方向才能旋转到正确的位置。还有其他可能吗?另外,我永远无法获得以前的方向,因为在方向更改后发送了通知(UIDeviceOrientationDidChangeNotification)

4

1 回答 1

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) {
        //
    }];
}
于 2013-07-28T11:45:49.163 回答