比如說(shuō)我有一個(gè)需求枫绅,要求想要旋轉(zhuǎn)的界面就旋轉(zhuǎn)荐绝,想要不旋轉(zhuǎn)的界面就不旋轉(zhuǎn)漫拭,請(qǐng)問(wèn)你怎么實(shí)現(xiàn)改览?
關(guān)于網(wǎng)上資料
其實(shí)網(wǎng)上的資料很多坑(說(shuō)得我這簡(jiǎn)書好像不是網(wǎng)上資料一樣-下翎。-)有很多提供的方法本身都是沒(méi)錯(cuò)的,但是都是復(fù)制來(lái)復(fù)制去(走右鍵不走心)宝当,所以缺少了一個(gè)關(guān)鍵的點(diǎn)比如說(shuō):方法應(yīng)該寫在哪里视事,什么情況下會(huì)調(diào)用
解決方法
手敲解決辦法啊,各位大大不要復(fù)制少了今妄,免得又成為被人嫌棄的網(wǎng)上資料郑口。
想要屏幕能夠旋轉(zhuǎn)鸳碧,首先要在你的 project - General - Deployment Info - Device Orientation 里面將需要的方向打鉤(最基本的要會(huì)吧= =)
控制屏幕旋轉(zhuǎn)的關(guān)鍵地方就是你自己設(shè)置的根控制器
[UIApplication sharedApplication].delegate 的window的rootViewController-
想要控制屏幕旋轉(zhuǎn)盾鳞,就在你設(shè)置的rootViewController里面加上如下代碼
///是否支持自動(dòng)轉(zhuǎn)屏 -(BOOL)shouldAutorotate { return YES;///很明顯如果返回YES就可以,返回NO就不行 } ///如果上面方法返回YES則會(huì)根據(jù)這個(gè)方法判斷支持的方向 -(UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAll;///這個(gè)是所有方向 }
-
打上斷點(diǎn)瞻离,你會(huì)發(fā)現(xiàn)“疤诮觥!原來(lái)所有界面都會(huì)調(diào)用這兩個(gè)方法來(lái)判斷橫屏疤桌推励!好神奇!”那么肉迫,我們已經(jīng)成功了一半验辞。如果想要隨意控制一個(gè)頁(yè)面的旋轉(zhuǎn),需要來(lái)一個(gè)屬性來(lái)判斷喊衫,而且要所有控制器都能修改和取得跌造,小的不才就只提供一個(gè)簡(jiǎn)單粗暴的思路,大大們自己看著搞:
我在AppDelegate.h里面添加了一個(gè)屬性(用BOOL的話族购,取值判斷時(shí)會(huì)為nil壳贪,大大請(qǐng)告訴我為什么) ///是否可以轉(zhuǎn)屏 @property(nonatomic,copy)NSString* isRotationAllowed; ///然后根據(jù)這個(gè)屬性來(lái)判斷每一個(gè)界面是否支持旋轉(zhuǎn) -(BOOL)shouldAutorotate { AppDelegate* delegate = [UIApplication sharedApplication].delegate; if ([delegate.isRotationAllowed isEqualToString:@"YES"]) { return YES; } return NO; } ///設(shè)置旋轉(zhuǎn)方向,這里可以自由發(fā)揮啊寝杖,不要局限于我的代碼 -(UIInterfaceOrientationMask)supportedInterfaceOrientations { AppDelegate* delegate = [UIApplication sharedApplication].delegate; if ([delegate.isRotationAllowed isEqualToString:@"YES"] && [delegate.isPortraitAllowed isEqualToString:@"NO"]) { return UIInterfaceOrientationMaskLandscape; } return UIInterfaceOrientationMaskPortrait; }
看到這里违施,你會(huì)不會(huì)說(shuō)怎么用呢?請(qǐng)記咨弧:
在需要的地方修改我添加的這個(gè)屬性磕蒲。。只盹。就可以了辣往,比如說(shuō)一個(gè)控制器的ViewDidLoad里面,改成YES鹿霸,那么在旋轉(zhuǎn)手機(jī)的時(shí)候排吴,就會(huì)去判斷這個(gè)值然后可以旋轉(zhuǎn),改成NO則反之懦鼠。
說(shuō)說(shuō)通知
///屏幕旋轉(zhuǎn)通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleDeviceOrientationDidChange:)
name:UIDeviceOrientationDidChangeNotification
object:nil
];
///屏幕旋轉(zhuǎn)調(diào)用(自定義)
-(void)handleDeviceOrientationDidChange:(NSDictionary*)notification
{
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
///根據(jù)orientation也就是當(dāng)前屏幕的方向來(lái)實(shí)現(xiàn)想要的方法比如UIDeviceOrientationPortrait之類
}
手動(dòng)轉(zhuǎn)屏
打個(gè)比方钻哩,比如一個(gè)按鈕的點(diǎn)擊事件是改變屏幕朝向屹堰,可以在點(diǎn)擊事件里面調(diào)用下面的方法來(lái)實(shí)現(xiàn)(這里是通過(guò)找到系統(tǒng)UIDevice類的設(shè)置屏幕方向方法,用currentDevice調(diào)用)
- (void)setInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
{
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setTarget:[UIDevice currentDevice]];
[invocation setSelector:selector];
[invocation setArgument:&orientation atIndex:2];
[invocation invoke];
}
}
結(jié)束
到這里基本上就能夠任性地使用你的屏幕旋轉(zhuǎn)了街氢。扯键。。有什么高明的建議或者交流歡迎留言珊肃,謝謝荣刑。