比如說我有一個(gè)需求,要求想要旋轉(zhuǎn)的界面就旋轉(zhuǎn),想要不旋轉(zhuǎn)的界面就不旋轉(zhuǎn),請(qǐng)問你怎么實(shí)現(xiàn)?
關(guān)于網(wǎng)上資料
其實(shí)網(wǎng)上的資料很多坑(說得我這簡(jiǎn)書好像不是網(wǎng)上資料一樣-。-)有很多提供的方法本身都是沒錯(cuò)的,但是都是復(fù)制來復(fù)制去(走右鍵不走心),所以缺少了一個(gè)關(guān)鍵的點(diǎn)比如說:方法應(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)“??!原來所有界面都會(huì)調(diào)用這兩個(gè)方法來判斷橫屏啊!好神奇!”那么,我們已經(jīng)成功了一半。如果想要隨意控制一個(gè)頁面的旋轉(zhuǎn),需要來一個(gè)屬性來判斷,而且要所有控制器都能修改和取得,小的不才就只提供一個(gè)簡(jiǎn)單粗暴的思路,大大們自己看著搞:
我在AppDelegate.h里面添加了一個(gè)屬性(用BOOL的話,取值判斷時(shí)會(huì)為nil,大大請(qǐng)告訴我為什么) ///是否可以轉(zhuǎn)屏 @property(nonatomic,copy)NSString* isRotationAllowed; ///然后根據(jù)這個(gè)屬性來判斷每一個(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ì)說怎么用呢?請(qǐng)記?。?/p>
在需要的地方修改我添加的這個(gè)屬性。。。就可以了,比如說一個(gè)控制器的ViewDidLoad里面,改成YES,那么在旋轉(zhuǎn)手機(jī)的時(shí)候,就會(huì)去判斷這個(gè)值然后可以旋轉(zhuǎn),改成NO則反之。
說說通知
///屏幕旋轉(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)前屏幕的方向來實(shí)現(xiàn)想要的方法比如UIDeviceOrientationPortrait之類
}
手動(dòng)轉(zhuǎn)屏
打個(gè)比方,比如一個(gè)按鈕的點(diǎn)擊事件是改變屏幕朝向,可以在點(diǎn)擊事件里面調(diào)用下面的方法來實(shí)現(xiàn)(這里是通過找到系統(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)了。。。有什么高明的建議或者交流歡迎留言,謝謝。