iOS開發,視頻播放,項目不支持橫屏,如何監測手機是否橫屏?並旋轉視頻播放器view?


1.如何鎖屏或者不支持橫屏的時候檢測手機方向?

- (void)applicationDidBecomeActive:(UIApplication *)application

{

[_motionManager startDeviceMotionUpdates];

}

- (void)applicationWillResignActive:(UIApplication *)application

{

[_motionManager stopDeviceMotionUpdates];

}

- (UIDeviceOrientation)realDeviceOrientation

{

CMDeviceMotion *deviceMotion = _motionManager.deviceMotion;

double x = deviceMotion.gravity.x;

double y = deviceMotion.gravity.y;

if (fabs(y) &>= fabs(x))

{

if (y &>= 0)

return UIDeviceOrientationPortraitUpsideDown;

else

return UIDeviceOrientationPortrait;

}

else

{

if (x &>= 0)

return UIDeviceOrientationLandscapeRight;

else

return UIDeviceOrientationLandscapeLeft;

}

}

2.如何手動旋轉設備?

Objective-C:

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];

Swift:

let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")


推薦閱讀:

如何在 Windows 下進行 iOS 開發?
iOS開發 APP必須使用HTTPS怎麼搞?
iOS 高級工程師是怎麼進階的?
一個 iOS Universal 的 App 代碼結構要怎麼樣寫才算是一個好的代碼?
什麼是 Cocoa,和 Objective-C 有什麼關係?開發 iOS 應用這兩者都要學習嗎?

TAG:多媒體播放器軟體 | iOS開發 |