屏幕方向.png
檢測屏幕旋轉(zhuǎn):
視圖控制器本身能檢測到屏幕的旋轉(zhuǎn)多柑,如果要處理屏幕旋轉(zhuǎn)祭往,需要重寫幾個方法:
supportedInterfaceOrientations(設(shè)置設(shè)備支持旋轉(zhuǎn)的方向,如果不添加澎剥,視圖控制器將無法檢測屏幕的旋轉(zhuǎn))。
willRotateToInterfaceOrientation:duration:(暫停音樂、關(guān)閉視圖交互等)圾亏。
willAnimateRotationToInterfaceOrientation:duration:(添加自定義動畫等)。
didRotateFromInterfaceOrientation:(播放音樂封拧、打開視圖交互等)志鹃。
視圖控制器中的方法:
//旋轉(zhuǎn)的時候暫停?樂、關(guān)閉視圖交互等
//此方法已廢棄
//- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
// [self.getbackView.textField resignFirstResponder];
//}
//Notifies the container that the size of its view is about to change
//視圖控制器里的視圖的大小發(fā)生改變的時候哮缺,視圖控制器會收到這個方法的通知(即這個方法會在此時自動執(zhí)行)
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
[self.getbackView.textField resignFirstResponder];
}
//Returns all of the interface orientations that the view controller supports.
//返回所有視圖控制器支持的方向(注意home鍵在上的情況默認(rèn)不處理)
//設(shè)置屏幕旋轉(zhuǎn)時支持的方法
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAll;
//在這個方法的API中可找到以下枚舉值弄跌,這里不詳細(xì)介紹
//UIInterfaceOrientationMaskAll 橫屏和豎屏
//UIInterfaceOrientationMaskLandscape 橫屏
}
視圖的處理:
****注意視圖控制器會自動調(diào)整view的大小以適應(yīng)屏幕旋轉(zhuǎn),bounds被修改尝苇, 觸發(fā)view的layoutSubviews方法铛只。
view重寫layoutSubviews方法,根據(jù)設(shè)備方向糠溜,重新布局淳玩。
[UIApplication shareApplication].statusBarOrientation提供設(shè)備當(dāng)前方向(獲取應(yīng)用程序的狀態(tài)欄的方向)。
視圖中的方法:
//只要視圖本身的bounds發(fā)生變化非竿,此方法就會執(zhí)行
- (void)layoutSubviews {
//獲取屏幕方向 UIInterfaceOrientation(枚舉類型)
UIInterfaceOrientation orientaion = [UIApplication sharedApplication].statusBarOrientation;
//判斷是否橫向
if (orientaion == UIInterfaceOrientationLandscapeLeft || orientaion ==UIInterfaceOrientationLandscapeRight) {
self.getbackButton.frame = CGRectMake(300, 150, 100, 40);
}else {
self.getbackButton.frame = CGRectMake(150, 150, 100, 40);
}
}