日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS强制切换横屏、竖屏

發布時間:2024/7/19 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS强制切换横屏、竖屏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

切換橫豎屏最直接的方式是調用device的setOrientation方法。但是從sdk3.0以后,這個方法轉為似有API,如果要上AppStore的話,要慎用!

?

? ??if?([[UIDevice?currentDevice]?respondsToSelector:@selector(setOrientation:)]) {

? ? ? ? [[UIDevice?currentDevice]?performSelector:@selector(setOrientation:)

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??withObject:(id)UIInterfaceOrientationLandscapeRight];

? ? }

第二種方式是手動的設置界面元素的旋轉,包括狀態欄、導航欄和視圖。以下代碼為從豎屏設置為橫屏,坐標系是以豎屏的為基準,所以會出現負數的坐標值。

?

? ??//設置狀態欄旋轉

? ? [[UIApplication?sharedApplication]?setStatusBarOrientation:UIDeviceOrientationLandscapeRight?animated:YES];

? ??CGFloat?duration = [UIApplication?sharedApplication].statusBarOrientationAnimationDuration;

? ??//設置旋轉動畫

? ? [UIView?beginAnimations:nil?context:nil];

? ? [UIView?setAnimationDuration:duration]; ? ?

? ??//設置導航欄旋轉

? ??self.navigationController.navigationBar.frame?=?CGRectMake(-204,?224,?480,?32);

? ??self.navigationController.navigationBar.transform?=?CGAffineTransformMakeRotation(M_PI*1.5);

? ??//設置視圖旋轉

? ??self.view.bounds?=?CGRectMake(0, -54,?self.view.frame.size.width,?self.view.frame.size.height);

? ??self.view.transform?=?CGAffineTransformMakeRotation(M_PI*1.5);

? ? [UIView?commitAnimations];

?

來源:?<http://blog.csdn.net/dickenslian/article/details/7407221>

?

*********************************************************************************************************************

?

一直遇到這個問題,今天終于找到了解決方法.

在我們的項目中經常遇到橫豎屏切換,而又有某個特定的界面必須是特定的顯示方式(橫屏或豎屏).這就需要如下的處理了.

強制轉成橫屏:

?

if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {

? ? ? ? ? ? SEL selector = NSSelectorFromString(@"setOrientation:");

? ? ? ? ? ? NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];

? ? ? ? ? ? [invocation setSelector:selector];

? ? ? ? ? ? [invocation setTarget:[UIDevice currentDevice]];

? ? ? ? ? ? int val = UIInterfaceOrientationLandscapeRight;

? ? ? ? ? ? [invocation setArgument:&val atIndex:2];

? ? ? ? ? ? [invocation invoke];

? ? ? ? }

?

以上代碼支持ARC喲.

方法二: 通過判斷狀態欄來設置視圖的transform屬性。

?

?

- (void)deviceOrientationDidChange: (NSNotification *)notification

{

? ? UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];

? ? CGFloat startRotation = [[self valueForKeyPath:@"layer.transform.rotation.z"] floatValue];

? ? CGAffineTransform rotation;

? ? switch (interfaceOrientation) {

? ? ? ? case UIInterfaceOrientationLandscapeLeft:

? ? ? ? ? ? rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 270.0 / 180.0);

? ? ? ? ? ? break;

? ? ? ? case UIInterfaceOrientationLandscapeRight:

? ? ? ? ? ? rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 90.0 / 180.0);

? ? ? ? ? ? break;

? ? ? ? case UIInterfaceOrientationPortraitUpsideDown:

? ? ? ? ? ? rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 180.0 / 180.0);

? ? ? ? ? ? break;

? ? ? ? default:

? ? ? ? ? ? rotation = CGAffineTransformMakeRotation(-startRotation + 0.0);

? ? ? ? ? ? break;

? ? }

? ??view.transform?= rotation;

}

轉載于:https://www.cnblogs.com/zxykit/p/5274887.html

總結

以上是生活随笔為你收集整理的iOS强制切换横屏、竖屏的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。