我正在使用 AVFoundation 框架来管理相机。当我处于横向模式时,我希望将拍摄按钮保持在屏幕右侧,如下图所示:
知道怎么做吗?
最佳答案
这是我的解决方案。
1) 禁用自动旋转,(我假设你不想旋转整个 View )
2) 将您的 View Controller 注册为 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;
...
}];
}
希望对您有所帮助。
关于ios - 相机处于横向模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23626934/