I'm using AVFoundation framework to manage the camera. When I'm in landscape mode, I would like to keep my shoot button on the right side of the screen like this picture :

enter image description here

Any idea how to do it?

有帮助吗?

解决方案

here is my solution.

1) disable autorotation, (I assume you don't want to rotate entire view)

2) register your view controller as an observer for UIDeviceOrientationDidChangeNotification

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

and also be sure to invoke removeObserver before your observer object is deallocated.

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

3) handle orientation changes

- (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) make rotation transform and apply on your buttons

- (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;
                         ...
                     }];
}

Hope it helps.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top