相机在横向模式下

时间:2014-05-13 09:03:35

标签: ios camera avfoundation landscape-portrait

我正在使用AVFoundation框架来管理相机。当我处于横向模式时,我想将拍摄按钮保持在屏幕右侧,如下图所示:

enter image description here

知道该怎么做吗?

1 个答案:

答案 0 :(得分:1)

这是我的解决方案。

1)禁用自动旋转,(我假设您不想旋转整个视图)

2)将您的视图控制器注册为UIDeviceOrientationDidChangeNotification的观察者

- (void)viewDidLoad {
    ...
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    ...
}

并且还要确保在取消分配观察者对象之前调用removeObserver。

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}

3)处理方向变化

- (void)deviceOrientationDidChange {
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    switch (orientation) {
        case UIDeviceOrientationPortrait:
        {
            [self rotateInterfaceWithDegrees:0.0];
        }
            break;
        case UIDeviceOrientationPortraitUpsideDown:
        {
            [self rotateInterfaceWithDegrees:180.0];
        }
            break;
        case UIDeviceOrientationLandscapeLeft:
        {
            [self rotateInterfaceWithDegrees:90.0];
        }
            break;
        case UIDeviceOrientationLandscapeRight:
        {
            [self rotateInterfaceWithDegrees:270.0];
        }
            break;

        default:
            break;
    }
}

4)进行旋转变换并应用于按钮

- (void)rotateInterfaceWithDegrees:(NSUInteger)degrees {
    CGAffineTransform transform = CGAffineTransformMakeRotation(degrees*M_PI/180.0);
    [UIView animateWithDuration:0.3   // with animation
                     animations:^{    // optional
                         yourFirstButton.transform = transform;
                         yourSecondButton.transform = transform;
                         ...
                     }];
}

希望它有所帮助。