转载:http://42.96.197.72/ios-zbarreaderview-interface-orientation/
在iPad应用中,如果没有特殊情况,需要让应用支持所有屏幕方向。在iPad中使用ZBar扫描二维码时,需要在屏幕旋转时调整摄像头的方向,而ZBarReaderView并不会在屏幕旋转时自动调整方向,所以需要我们以某种方式进行控制。
如果使用的是ZBarReaderViewController,那么这件事情相对比较简单,使用以下方式进行设置即可支持所有屏幕方向。
ZBarReaderViewController
*reader
=
[
[ZBarReaderViewController alloc
]init
];
reader.supportedOrientationsMask = ZBarOrientationMaskAll;
reader.supportedOrientationsMask = ZBarOrientationMaskAll;
我在项目中使用的是ZBarReaderView,在网上找的一些方案都不能很好的解决问题,最终用以下方式解决了屏幕旋转问题,并且非常简洁。
ZBarReaderView中摄像头的默认方向为UIInterfaceOrientationPortrait,因此需要在初始化时调用该方法,以保证ZBarReaderView初始化时处于正确的方向。代码如下:
ZBarReaderView
*readerView
=
[
[ZBarReaderView alloc
]init
];
[readerView
willRotateToInterfaceOrientation :
[ [UIApplication sharedApplication ] statusBarOrientation ]
duration :
[ [UIApplication sharedApplication ]statusBarOrientationAnimationDuration ] ];
[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 ];
}
{
return YES;
}
//旋转屏幕
- ( void )willAnimateRotationToInterfaceOrientation : (UIInterfaceOrientation )toInterfaceOrientation
duration : (NSTimeInterval )duration
{
[readerView willRotateToInterfaceOrientation :toInterfaceOrientation duration :duration ];
}