UIInterfaceOrientation和uidevice importOrientation的区别

博客分类:
UIDeviceOrientation & & &是机器硬件的当前旋转方向 & 这个你只能取值 不能设置
UIInterfaceOrientation & 是你程序界面的当前旋转方向 & 这个可以设置
判断设备现在的方向:
-&(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation&&
&&&&UIDevice&*device&=&[UIDevice&currentDevice]&;&&&
&&&&switch&(device.orientation)&{&&
&&&&&&&&case&UIDeviceOrientationFaceUp:&&
&&&&&&&&NSLog(@"螢幕朝上平躺");&&
&&&&&&&&&&&&break;&&
&&&&&&&&&&&&&&
&&&&&&&&case&UIDeviceOrientationFaceDown:&&
&&&&&&&&NSLog(@"螢幕朝下平躺");&&
&&&&&&&&&&&&break;&&
&&&&&&&&&&&&&&
&&&&&&&&&&
&&&&&&&&case&UIDeviceOrientationUnknown:&&
&&&&&&&&NSLog(@"未知方向");&&
&&&&&&&&&&&&break;&&
&&&&&&&&&&&&&&
&&&&&&&&case&UIDeviceOrientationLandscapeLeft:&&
&&&&&&&&NSLog(@"螢幕向左橫置");&&
&&&&&&&&&&&&break;&&
&&&&&&&&&&&&&&
&&&&&&&&case&UIDeviceOrientationLandscapeRight:&&
&&&&&&&&NSLog(@"螢幕向右橫置");&&
&&&&&&&&&&&&break;&&
&&&&&&&&&&&&&&
&&&&&&&&case&UIDeviceOrientationPortrait:&&
&&&&&&&&NSLog(@"螢幕直立");&&
&&&&&&&&&&&&break;&&
&&&&&&&&&&&&&&
&&&&&&&&case&UIDeviceOrientationPortraitUpsideDown:&&
&&&&&&&&NSLog(@"螢幕直立,上下顛倒");&&
&&&&&&&&&&&&break;&&
&&&&&&&&&&&&&&
&&&&&&&&default:&&
&&&&&&&&NSLog(@"無法辨識");&&
&&&&&&&&&&&&break;&&
&&&&return&(interfaceOrientation&==&UIInterfaceOrientationLandscapeLeft);&&&
-&(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation&&
&&&&UIDeviceOrientation&deviceOrientation&=&[UIDevice&currentDevice].&&
&&&&if&(UIDeviceOrientationIsLandscape(deviceOrientation))&NSLog(@"横向");&&&&&&
&&&&else&if(UIDeviceOrientationIsPortrait(deviceOrientation))&NSLog(@"纵向");&&
&&&&return&(interfaceOrientation&==&UIInterfaceOrientationLandscapeLeft);&&&
Portrait 表示 纵向,Landscape 表示 横向。
typedef&enum&{&&
&&&&UIDeviceOrientationUnknown,&&
&&&&UIDeviceOrientationPortrait,&&&&&&&&&&&&&&
&&&&UIDeviceOrientationPortraitUpsideDown,&&&&
&&&&UIDeviceOrientationLandscapeLeft,&&&&&&&&&
&&&&UIDeviceOrientationLandscapeRight,&&&&&&&&
&&&&UIDeviceOrientationFaceUp,&&&&&&&&&&&&&&&&
&&&&UIDeviceOrientationFaceDown&&&&&&&&&&&&&&&
}&UIDeviceO&&
typedef&enum&{&&
&&&&UIInterfaceOrientationPortrait&&&&&&&&&&&=&UIDeviceOrientationPortrait,&&
&&&&UIInterfaceOrientationPortraitUpsideDown&=&UIDeviceOrientationPortraitUpsideDown,&&
&&&&UIInterfaceOrientationLandscapeLeft&&&&&&=&UIDeviceOrientationLandscapeRight,&&
&&&&UIInterfaceOrientationLandscapeRight&&&&&=&UIDeviceOrientationLandscapeLeft&&
}&UIInterfaceO&&
#define&UIDeviceOrientationIsPortrait(orientation)&&((orientation)&==&UIDeviceOrientationPortrait&||&(orientation)&==&UIDeviceOrientationPortraitUpsideDown)&&
#define&UIDeviceOrientationIsLandscape(orientation)&((orientation)&==&UIDeviceOrientationLandscapeLeft&||&(orientation)&==&UIDeviceOrientationLandscapeRight)&&
上面是重要的源代码,已经解释的非常清楚。UIDeviceOrientationIsPortrait(orientation) 跟&&((orientation) == UIDeviceOrientationPortrait || (orientation) == UIDeviceOrientationPortraitUpsideDown) 完全是一个意思。
阅读(...) 评论()Handling UIDeviceOrientationFaceUp & UIDeviceOrientationFaceDown
If your iPad (or iPhone) application uses UIDeviceOrientationDidChangeNotification to detect device rotation, there might be a couple cases you forget to think about. If your device is lying down flat (or upside down), there is still an implied “felt” orientation, based on how the iPad was last held before it went flat. How will iOS let you know about this? Here is what i found out when writing :
After you sign up for UIDeviceOrientationDidChangeNotification, if your device is flat, you will receive 3 notifications. The first one will be with a value if UIDeviceOrientationFaceUp (or UIDeviceOrientationFaceDown), matching the actual current physical location of the device. Right afterwards, you get another notification, and this one will specify one of UIDeviceOrientationPortrait, UIDeviceOrientationPortraitUpsideDown,
UIDeviceOrientationLandscapeLeft,
or UIDeviceOrientationLandscapeRight – depending on what the UI was showing before your app started (and indicating how the user will think of the device as being rotated). This is followed right up with a third notification that brings you back to either UIDeviceOrientationFaceUp or UIDeviceOrientationFaceDown.
Essentially, the device is pretending the user really quickly shook it back to an upright position, and back down.
The best way to handle this that i found in my app was to never ever use the actual reported orientation do fo any drawing or calculation, but instead cache the last “felt” orientation, within an handler as follows:
- (void)orientationChanged:(NSNotification *)notification
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait ||
orientation == UIDeviceOrientationPortraitUpsideDown ||
orientation == UIDeviceOrientationLandscapeLeft ||
orientation == UIDeviceOrientationLandscapeRight)
if (!currentOrientation)
currentOrientation =
[self prepareScreen];
currentOrientation =
[[UIApplication sharedApplication] setStatusBarOrientation:currentOrientation animated:YES];
All my code will use currentOrientation, so it will always have a proper orientation value. Also any screen initialization that depends on having a proper orientation (in Dwarfland Photos that is the initial drawing of the startup logo, once Default.png disappears)
i do in prepareScreen, which is not called until i obtained the proper “felt” rotation.
Recent Posts
Select Month
July 2015 &(3)
June 2015 &(1)
May 2015 &(2)
April 2015 &(1)
March 2015 &(1)
January 2015 &(2)
December 2014 &(2)
September 2014 &(3)
August 2014 &(2)
July 2014 &(2)
June 2014 &(5)
April 2014 &(3)
March 2014 &(3)
February 2014 &(1)
January 2014 &(1)
December 2013 &(1)
November 2013 &(2)
October 2013 &(8)
September 2013 &(4)
August 2013 &(2)
July 2013 &(5)
June 2013 &(7)
May 2013 &(3)
April 2013 &(5)
March 2013 &(4)
February 2013 &(7)
January 2013 &(9)
December 2012 &(6)
November 2012 &(5)
October 2012 &(6)
September 2012 &(10)
August 2012 &(3)
July 2012 &(15)
June 2012 &(5)
May 2012 &(13)
April 2012 &(6)
March 2012 &(12)
February 2012 &(10)
January 2012 &(15)
December 2011 &(9)
November 2011 &(7)
October 2011 &(12)
September 2011 &(14)
August 2011 &(7)
July 2011 &(9)
June 2011 &(12)
May 2011 &(8)
April 2011 &(4)
March 2011 &(6)
February 2011 &(5)
January 2011 &(5)
December 2010 &(5)
November 2010 &(11)
October 2010 &(7)
September 2010 &(14)
August 2010 &(20)
July 2010 &(2)
June 2010 &(8)
May 2010 &(2)
April 2010 &(5)
March 2010 &(8)
February 2010 &(7)
January 2010 &(12)
November 2009 &(3)
October 2009 &(1)
September 2009 &(2)
August 2009 &(3)
July 2009 &(3)
June 2009 &(2)
May 2009 &(9)
April 2009 &(13)
March 2009 &(6)
February 2009 &(5)
January 2009 &(3)
December 2008 &(7)
November 2008 &(2)
October 2008 &(3)
September 2008 &(6)
August 2008 &(3)
July 2008 &(3)
June 2008 &(3)
May 2008 &(5)
April 2008 &(5)
March 2008 &(1)
February 2008 &(5)
January 2008 &(5)
December 2007 &(8)
November 2007 &(8)
October 2007 &(8)
September 2007 &(8)
August 2007 &(4)
July 2007 &(1)
June 2007 &(11)
May 2007 &(12)
April 2007 &(7)
January 2007 &(1)
December 2006 &(3)
November 2006 &(1)
October 2006 &(2)
August 2006 &(1)
July 2006 &(1)
June 2006 &(4)
April 2006 &(2)
March 2006 &(1)
February 2006 &(2)
January 2006 &(3)
December 2005 &(4)
September 2005 &(6)
August 2005 &(4)
July 2005 &(5)
June 2005 &(11)
May 2005 &(2)
April 2005 &(4)
March 2005 &(5)
February 2005 &(1)
January 2005 &(3)
November 2004 &(3)
October 2004 &(3)
September 2004 &(3)
August 2004 &(3)
July 2004 &(1)
June 2004 &(7)
May 2004 &(9)
January 2004 &(1)
Contributorsios设备旋转走的代理(代码附加输出口集合) - 不积跬步 无以至千里 不积小流 无以成江海 - ITeye技术网站
博客分类:
//被调用。这个方法是发生在翻转开始之前。一般用来禁用某些控件或者停止某些正在进行的活动,比如停止视频播放。
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
NSLog(@"willRotateToInterfaceOrientation %d",toInterfaceOrientation);
//这个方法发生在翻转的过程中,一般用来定制翻转后各个控件的位置、大小等。可以用另外两个方法来代替:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)
interfaceOrientation duration:(NSTimeInterval)duration {
NSLog(@"willAnimateRotationToInterfaceOrientation %d",interfaceOrientation);
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
self.view = self.
self.view.transform = CGAffineTransformI
self.view.transform =
CGAffineTransformMakeRotation(degreesToRadians(0));
self.view.bounds = CGRectMake(0.0, 0.0, 320.0, 460.0);
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
self.view = self.
self.view.transform = CGAffineTransformI
self.view.transform =
CGAffineTransformMakeRotation(degreesToRadians(-90));
self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 300.0);
else if (interfaceOrientation ==
UIInterfaceOrientationLandscapeRight) {
self.view = self.
self.view.transform = CGAffineTransformI
self.view.transform =
CGAffineTransformMakeRotation(degreesToRadians(90));
self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 300.0);
//视图旋转动画前一半发生之前自动调用
- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
NSLog(@"willAnimateFirstHalfOfRotationToInterfaceOrientation %d",toInterfaceOrientation);
//视图旋转动画前一半发生之后自动调用
- (void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
NSLog(@"didAnimateFirstHalfOfRotationToInterfaceOrientation %d",toInterfaceOrientation);
}// The rotating header and footer views are offscreen.
//视图旋转动画后一半发生之前自动调用
- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration{
NSLog(@"willAnimateSecondHalfOfRotationFromInterfaceOrientation %d",fromInterfaceOrientation);
//这个方法发生在整个翻转完成之后。一般用来重新启用某些控件或者继续翻转之前被暂停的活动,比如继续视频播放
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
NSLog(@"didRotateFromInterfaceOrientation %d",fromInterfaceOrientation);
// 《=ios5
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
- (BOOL)shouldAutorotate
return YES;
-(NSUInteger)supportedInterfaceOrientations
return UIInterfaceOrientationMaskAllButUpsideD
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 可以用另外两个方法来代替:willAnimateFirstHalfOfRotationToInterfaceOrientation:duration: 和 willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:。
但是当willAnimateRotationToInterfaceOrientation走时,willAnimateFirstHalfOfRotationToInterfaceOrientation:duration,willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration和willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration是不走的,也就是二选一!但用后者三个方法系统提示过时了。
------------------------ios6的略微改变---------------
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation //已经相当弱化
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
//新增Api,与info.plist设备支持旋转方向神似(区别在于default.png)且注意与UIInterfaceOrientation不同的是这的NSUInteger返回值为UIInterfaceOrientationMaskPortrait|...等
//iOS6新增Api来控制旋转,需要注意的是顶层才是有效的
- (BOOL)shouldAutorotate
return NO;
-(NSUInteger)supportedInterfaceOrientations
return UIInterfaceOrientationMaskP
//习惯使用的presentModalViewController navigation controller在旋转上已经出现了大问题,可用category解决
@implementation UINavigationController (autorotate)
- (NSUInteger)supportedInterfaceOrientations{
NSArray *arr = self.viewC
if ([arr count] == 0) {
return UIInterfaceOrientationMaskP
id vc = [arr objectAtIndex:0];
if ([vc isKindOfClass:[TestNoRotation class]]) {
return UIInterfaceOrientationMaskP
if ([vc isKindOfClass:[TestAutoRotation class]]) {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeR
return UIInterfaceOrientationMaskP
在ios6.0中shouldAutorotateToInterfaceOrientation:几乎不在起作用了,ios使用shouldAutorotate来控制旋转效果。
我测试ios6.0真正起作用的旋转有三种:
1.采用info.plist的UISupportedInterfaceOrientations来控制方向
2.是直接在UIWindow中加视图,这种方法可以脱离info.plist的控制,shouldAutorotate来自定义方向。
3.使用rootViewController添加shouldAutorotate方法,但是受info.plist的限制。
以上的方法都限制于顶层视图控制,如果要在子视图控制中添加旋转效果,则需要在顶层视图控制器中开启shouldAutorotate方法,在子视图控制器就可以使用
- (NSUInteger)supportedInterfaceOrientations{}
代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.mainViewController = [[[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil] autorelease];
self.window.rootViewController = self.mainViewC
self.mainViewController.window = self.
[self.window makeKeyAndVisible];
直接添加视图来控制
self.testVC = [[[testViewController alloc] initWithNibName:@"testViewController" bundle:nil] autorelease];
//[self.window addSubview:self.testVC.view];
return YES;
顶层视图控制器开启旋转
//MainViewController中开启旋转
- (BOOL)shouldAutorotate
return YES;
-(NSUInteger)supportedInterfaceOrientations
return UIInterfaceOrientationMaskL
添加一个子视图
- (IBAction)showInfo:(id)sender
testViewController *vc =
[[testViewController alloc] initWithNibName:@"testViewController" bundle:nil];
//添加子视图
[self presentViewController:vc animated:YES completion:nil];
子视图控制器直接使用
//可以不写
//- (BOOL)shouldAutorotate
return YES;
// testViewController中直接使用
-(NSUInteger)supportedInterfaceOrientations
return UIInterfaceOrientationMaskP
-----------------------------------切糕分割线----------------------------------
假如如下方法禁止旋转:
- (BOOL)shouldAutorotate
return NO;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return NO;
可以增加监听来选装[notificationCenter addObserver:selfselector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotificationobject:nil];
在自定义deviceOrientationDidChange方法中:
获取自身屏幕方法使用self.interfaceOrientation或[[UIApplication sharedApplication] statusBarOrientation],不要使用[[UIDevice currentDevice] orientation](第一次使用的时候,总是返回0)。
----------------------------------不旋转原因的分割线---------------
转自.cn/s/blog_6deh.html
UIViewController没有随着设备一起旋转的原因
对于iPhone app,UIViewController类提供了基本的视图管理模式。当设备改变方向的时候view controller的视图会自动随之旋转的。如果视图和子视图的autoresizing属性设置是对的,这时候视图又没有随着设备一起旋转,可能是以下的原因:
1.view controller没有完成代理方法
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceO
也要实现了shouldAutorotateToInterfaceOrientation方法,同时shouldAutorotateToInterfaceOrientation方法要返回YES来支持所有的旋转方向
2.view controller的UIView属性嵌入在UIWindow中,并非是一个附加的view controller
你可能会发现shouldAutorotateToInterfaceOrientation方法只会在view controller启动的时候被调用,不会因为设置的旋转而在被调用。这是因为view controller束缚着他们所管理的视图,view controller是用来处理时间的响应链的一部分。view controller是从UIResponder继承而来,同时他被插入到他所管理的视图和他的superview之间。因此,通常的做法是在你的app中有一个主view controller来作为响应链的一部分。通常来说会添加一个主view controller,例如UINavigationController, UITabBarController或者UIViewController到UIWindow上。
[myWindow addSubview:primaryViewController.view];
如果你添加了另外一个view controller的UIView属性到UIWindow(anotherController和主view controller在同一个等级上)
[myWindow addSubview:anotherController.view];
anotherController将不会接受旋转事件,只有第一个view controller(primaryViewController)会接受旋转事件。
3.你添加了view controller的UIView属性到UIWindow作为subview,但是过早的release它。
UIWindow会retain视图,而不是view controller。你不能过早的release他。在UIApplicationDelegate子类中定义他为retain属性。
4.在UITabBarController或者UINavigationController中的子view controller没有对同一方向的支持。
为了确保所有的子view controller旋转正确,你的每一个view controller,每一个tab或者额navigation都要完成shouldAutorotateToInterfaceOrientation,而且必须支持对于同一方向的旋转,也就是说对于同一方向的位置要返回为YES。
5.重写-(id)init:或者 -(id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle 方法的时候没有调用super。
对于对象的初始化,在你的view controller的init或者initWithNibName方法中必须要调用super。
下载次数: 8
浏览: 1281768 次
来自: China
对于dispatch_queue_t 对象来说,我们应该这么写 ...
func dispatch_barrier_async(_ q ...
使用下面两行代码替代 estimatedHeightForRo ...
#define SYNTHESIZE_CATEGORY_STR ...

我要回帖

更多关于 android uidevice 的文章

 

随机推荐