IOS6 屏幕旋转 以及 旋转后的对应方法

想要在IOS6下支持屏幕旋转,首先有一下两点要求:

1、在 Info.plist 中需要有Supported interface orientations支持(默认支持三个方向的旋转)

2、 在添加页面是,采取的是ViewController形式,而不是view

例如: AppDelegate中,[self.windows setRootViewController  viewController ]

而不是:     [self.windows.view  addSubview: viewController.view]


接下来就可以设置屏幕支持了,ios默认只支持竖屏,不支持转屛

1、在viewControll.m 文件中添加函数,表明支持旋转屏幕

- (BOOL)shouldAutorotate
{
    return YES;
}
这个函数的意思是,是否支持旋转屏幕,YES ,支持

2、 添加函数,表明支持具体哪几个屏幕

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}
return的就是支持屏幕的类型,在IOS6之前,一般是返回UIInterfaceOrientation 类型,上次左右,简单明了

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
};

但是IOS6定义了新的类型UIInterfaceOrientationMask,定义如下:

typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
};
这个类型,不仅包括上下左右,还包括他们的不同合集,比如左右,上下左右,上左右等,我在函数中的返回值 UIInterfaceOrientationMaskAll 就是指,所有的旋转都支持

如此,旋转的设置便定义好了。

那么,如何准对旋转,来执行相应的方法呢?

我们便需要用到

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
在函数里,可以根据对应的UIInterfaceOrientation 来应对,举例如下:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
        NSLog(@"left");
        self.view.backgroundColor = [UIColor greenColor];
    }
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
        self.view.backgroundColor = [UIColor whiteColor];
        NSLog(@"center");
    }
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        self.view.backgroundColor = [UIColor yellowColor];
        NSLog(@"right");
    }
    
}

这个Demo就是根据屏幕的旋转来变换不同的色,很简单,不解释。

源代码如下:屏幕旋转源代码



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值