在iPad应用中,如果没有特殊情况,需要让应用支持所有屏幕方向。在iPad中使用ZBar扫描二维码时,需要在屏幕旋时调整摄像头的方向,而ZBarReaderView并不会在屏幕旋转时自动调整方向,所以需要我们以某种方式进行控制。

如果使用的是ZBarReaderViewController,那么这件事情相对比较简单,使用以下方式进行设置即可支持所有屏幕方向。

ZBarReaderViewController *reader = [[ZBarReaderViewController alloc]init];

reader.supportedOrientationsMask = ZBarOrientationMaskAll;

我在项目中使用的是ZBarReaderView,在网上找的一些方案都不能很好的解决问题,最终用以下方式解决了屏幕旋问题,并且非常简洁。

ZBarReaderView中摄像头的默认方向为UIInterfaceOrientationPortrait,因此需要在初始化时调用该方法,以保证ZBarReaderView初始化时处于正确的方向。代码如下:

ZBarReaderView *readerView = [[ZBarReaderView alloc]init];

[readerView 

    willRotateToInterfaceOrientation:

        [[UIApplication sharedApplication] statusBarOrientation] 

    duration:

        [[UIApplication sharedApplication]statusBarOrientationAnimationDuration]];

此外,添加以下部分以保证ZBarReaderView可以正确处理屏幕旋,代码如下:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

{

    return YES;

}


//旋屏幕

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

    duration:(NSTimeInterval)duration

{

    [readerView willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

}更多​