首页 > 其他 > 详细

横竖屏事件响应

时间:2015-01-05 20:04:24      阅读:242      评论:0      收藏:0      [点我收藏+]

最近搞横竖屏,获得一些心得,特记录下来。

做横竖屏最重要的是确定横竖屏响应的接口。目前我知道的有两种方式 :

1.使用通知。

    

- (void)viewDidLoad

      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_orientationDidChange:)name:UIDeviceOrientationDidChangeNotification object:nil];

}

 

- (void)dealloc {

 

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotificationobject:nil];

}

 

-(void)_orientationDidChange:(NSNotification*)notify

{

    [self _shouldRotateToOrientation:(UIDeviceOrientation)[UIApplication sharedApplication].statusBarOrientation];

}

-(void)_shouldRotateToOrientation:(UIDeviceOrientation)orientation {

 

if (orientation == UIDeviceOrientationPortrait ||orientation == UIDeviceOrientationPortraitUpsideDown) {

          // 竖屏

}

else {

         // 横屏

}

}

上述代码,一看就明白。

2.使用  viewWillLayoutSubviews

  测试发现横竖屏切换的时候,系统会响应一些函数,其中 viewWillLayoutSubviews就是之一。

 

- (void)viewWillLayoutSubviews

{

     [self _shouldRotateToOrientation:(UIDeviceOrientation)[UIApplication sharedApplication].statusBarOrientation];

}

通过上述一个函数就知道横竖屏切换的接口了。

注意:

viewWillLayoutSubviews只能用在ViewController里面,在view里面没有响应。

 

横竖屏事件响应

原文:http://www.cnblogs.com/huangh/p/4204239.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!