在做项目的时候,我们经常要判断设备的方向,然后选择不同的图片。
现将判断设备方向的方法列举如下:
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];//获取方向的方法
方向枚举如下:
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown, //对应数值为0
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait, //对应数值为1
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, //对应数值为2
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight, //对应数值为3
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft //对应数值为4
};
最常用的,一般的竖直方向,和按钮向右。因为玩游戏的话,基本是这两个方向。
但是记录数值,实在是太麻烦,如果我们直接能够判断具体方向的话,要比数值好一点。
if(UIInterfaceOrientationIsPortrait(orientation)) //判断设备方向是否是竖直,包括按钮朝下和按钮朝上两种情况。
#define UIInterfaceOrientationIsPortrait(orientation)
((orientation) == UIInterfaceOrientationPortrait ||
(orientation) == UIInterfaceOrientationPortraitUpsideDown)
else if (UIInterfaceOrientationIsLandscape(orientation)) //此时设备的横着的
#define UIInterfaceOrientationIsLandscape(orientation)
((orientation) == UIInterfaceOrientationLandscapeLeft ||
(orientation) == UIInterfaceOrientationLandscapeRight)
直接调用
if(UIInterfaceOrientationIsPortrait(orientation))
和
else if (UIInterfaceOrientationIsLandscape(orientation))
即可判定设备方向,将具体处理办法放入{}语句内。