UIDeviceOrientation
UIInterfaceOrientation
判断设备现在的方向:
- -
(BOOL)shouldAutorotateToInterf aceOrientation:(UIInterfaceOrientation)interfaceOrientation - {
-
//宣告一個UIDevice指標,並取得目前Device的狀況 -
UIDevice *device = [UIDevice currentDevice] ; -
-
//取得當前Device的方向,來當作判斷敘述。(Device的方向型態為Integer) -
switch (device.orientation) { -
case UIDeviceOrientationFaceU p: -
NSLog(@"螢幕朝上平躺"); -
break; -
-
case UIDeviceOrientationFaceD own: -
NSLog(@"螢幕朝下平躺"); -
break; -
-
//系統無法判斷目前Device的方向,有可能是斜置 -
case UIDeviceOrientationUnkno wn: -
NSLog(@"未知方向"); -
break; -
-
case UIDeviceOrientationLands capeLeft: -
NSLog(@"螢幕向左橫置"); -
break; -
-
case UIDeviceOrientationLands capeRight: -
NSLog(@"螢幕向右橫置"); -
break; -
-
case UIDeviceOrientationPortr ait: -
NSLog(@"螢幕直立"); -
break; -
-
case UIDeviceOrientationPortr aitUpsideDown: -
NSLog(@"螢幕直立,上下顛倒"); -
break; -
-
default: -
NSLog(@"無法辨識"); -
break; -
} -
-
// Return YES for supported orientations -
return (interfaceOrientation == UIInterfaceOrientationLa ndscapeLeft); // 只支持向左横向, YES 表示支持所有方向 - }
或者
- -
(BOOL)shouldAutorotateToInterf aceOrientation:(UIInterfaceOrientation)interfaceOrientation - {
-
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation; -
if (UIDeviceOrientationIsLan dscape(deviceOrientation)) NSLog(@"横向"); -
else if(UIDeviceOrientationIsPor trait(deviceOrientation)) NSLog(@"纵向"); -
-
// // Return YES for supported orientations -
return (interfaceOrientation == UIInterfaceOrientationLa ndscapeLeft); // 只支持向左横向, YES 表示支持所有方向 - }
Portrait 表示 纵向,Landscape 表示 横向。
- typedef
enum { -
UIDeviceOrientationUnkno wn, -
UIDeviceOrientationPortr ait, // Device oriented vertically, home button on the bottom -
UIDeviceOrientationPortr aitUpsideDown, // Device oriented vertically, home button on the top -
UIDeviceOrientationLands capeLeft, // Device oriented horizontally, home button on the right -
UIDeviceOrientationLands capeRight, // Device oriented horizontally, home button on the left -
UIDeviceOrientationFaceU p, // Device oriented flat, face up -
UIDeviceOrientationFaceD own // Device oriented flat, face down - }
UIDeviceOrientation;
- typedef
enum { -
UIInterfaceOrientationPo rtrait = UIDeviceOrientationPortr ait, -
UIInterfaceOrientationPo rtraitUpsideDown = UIDeviceOrientationPortr aitUpsideDown, -
UIInterfaceOrientationLa ndscapeLeft = UIDeviceOrientationLands capeRight, -
UIInterfaceOrientationLa ndscapeRight = UIDeviceOrientationLands capeLeft - }
UIInterfaceOrientation;
- #define
UIDeviceOrientationIsPor trait(orientation) ((orientation) == UIDeviceOrientationPortr ait || (orientation) == UIDeviceOrientationPortr aitUpsideDown) - #define
UIDeviceOrientationIsLan dscape(orientation) ((orientation) == UIDeviceOrientationLands capeLeft || (orientation) == UIDeviceOrientationLands capeRight)
上面是重要的源代码,已经解释的非常清楚。UIDeviceOrientationIsPor
转自:huifeidexin_1