iOS實(shí)現(xiàn)手動和自動屏幕旋轉(zhuǎn)
本文實(shí)例為大家分享了iOS實(shí)現(xiàn)手動和自動屏幕旋轉(zhuǎn)的具體代碼,供大家參考,具體內(nèi)容如下
首先iPhone中屏幕分為狀態(tài)欄方向和設(shè)備方向
typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
? ? UIDeviceOrientationUnknown,
? ? UIDeviceOrientationPortrait, ? ? ? ? ? ?// Device oriented vertically, home button on the bottom
? ? UIDeviceOrientationPortraitUpsideDown, ?// Device oriented vertically, home button on the top
? ? UIDeviceOrientationLandscapeLeft, ? ? ? // Device oriented horizontally, home button on the right
? ? UIDeviceOrientationLandscapeRight, ? ? ?// Device oriented horizontally, home button on the left
? ? UIDeviceOrientationFaceUp, ? ? ? ? ? ? ?// Device oriented flat, face up
? ? UIDeviceOrientationFaceDown ? ? ? ? ? ? // Device oriented flat, face down
};
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
? ? UIInterfaceOrientationUnknown ? ? ? ? ? ?= UIDeviceOrientationUnknown,
? ? UIInterfaceOrientationPortrait ? ? ? ? ? = UIDeviceOrientationPortrait,
? ? UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
? ? UIInterfaceOrientationLandscapeLeft ? ? ?= UIDeviceOrientationLandscapeRight,
? ? UIInterfaceOrientationLandscapeRight ? ? = UIDeviceOrientationLandscapeLeft
};系統(tǒng)提供兩個地方來設(shè)置設(shè)備的方向,取兩個地方的交集是最后的設(shè)備所支持的方向
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window; -(NSUInteger)supportedInterfaceOrientations;
這里需要注意的是返回的時下面的枚舉
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),
};在轉(zhuǎn)動屏幕的時候會觸發(fā)下面方法
-(BOOL)shouldAutorotate;
在該方法返回真,自動調(diào)用上面的兩個方法得到方向。
修改狀態(tài)欄方向的方法
1、使用私有API setOrientation;
2、修改狀態(tài)欄的方向,并通過設(shè)置View的transform來達(dá)到偽旋轉(zhuǎn)的結(jié)果,但是設(shè)備方向并沒有改變
3、主動出發(fā)系統(tǒng)支持的方法,就相當(dāng)于讓這個vc在重新出來的時候系統(tǒng)判斷所支持的方向的機(jī)制重新走一遍。
- (void)awakeSupportInterOrtation:(UIViewController *)showVC completion:(void(^)(void))block
{
? ? UIViewController *vc = [[UIViewController alloc] init];
? ? void(^completion)() = ^() {
? ? ? ? [showVC dismissViewControllerAnimated:NO completion:^{
? ? ? ? ? ? if (block)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? block();
? ? ? ? ? ? }
? ? ? ? }];
? ? };
? ? // This check is needed if you need to support iOS version older than 7.0
? ? BOOL canUseTransitionCoordinator = [showVC respondsToSelector:@selector(transitionCoordinator)];
? ? if (canUseTransitionCoordinator)
? ? {
? ? ? ? [showVC presentViewController:vc animated:NO completion:nil];
? ? ? ? [showVC.transitionCoordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
? ? ? ? ? ? completion();
? ? ? ? }];
? ? }
? ? else
? ? {
? ? ? ? [showVC presentViewController:vc animated:NO completion:completion];
? ? }
}
-(NSUInteger)supportedInterfaceOrientations
{
? ? ? ? return UIInterfaceOrientationMaskPortrait;
}
-(BOOL)shouldAutorotate
{
? ? return YES;
}在需要轉(zhuǎn)為豎屏的時候調(diào)用一個方法,在后面兩個方法中如上實(shí)現(xiàn),第二個方法中返回的是你最終要轉(zhuǎn)向的方向。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS開發(fā)之UIPickerView實(shí)現(xiàn)城市選擇器的步驟詳解
這篇文章給大家介紹iOS利用控件UIPickerView實(shí)現(xiàn)城市選擇器的效果,選擇城市這一功能相信在大家日常開發(fā)的時候經(jīng)常遇見,下面就來看看詳細(xì)的實(shí)現(xiàn)過程,有需要的可以參考借鑒。2016-09-09
iOS開發(fā)中使用Picker View實(shí)現(xiàn)一個點(diǎn)菜應(yīng)用的UI示例
這篇文章主要介紹了iOS開發(fā)中使用Picker View實(shí)現(xiàn)一個點(diǎn)菜應(yīng)用的UI示例,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01
iOS實(shí)現(xiàn)屏幕亮度和閃光燈控制的實(shí)例代碼
本篇文章主要介紹了iOS實(shí)現(xiàn)屏幕亮度和閃光燈控制的實(shí)例代碼,具有一定的參考價值,有興趣的可以了解一下2017-06-06
iOS App使用GCD導(dǎo)致的卡頓現(xiàn)象及解決方法
這篇文章主要給大家介紹了關(guān)于iOS App使用GCD導(dǎo)致的卡頓現(xiàn)象及解決方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07

