iPhone、iPad、苹果设备方向判断方法

  在做项目的时候,我们经常要判断设备的方向,然后选择不同的图片。

  现将判断设备方向的方法列举如下:

    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)) 
即可判定设备方向,将具体处理办法放入{}语句内。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值