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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

iOS强制切换横屏、竖屏

發(fā)布時(shí)間:2024/7/19 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS强制切换横屏、竖屏 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

?

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

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

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

? ? }

第二種方式是手動(dòng)的設(shè)置界面元素的旋轉(zhuǎn),包括狀態(tài)欄、導(dǎo)航欄和視圖。以下代碼為從豎屏設(shè)置為橫屏,坐標(biāo)系是以豎屏的為基準(zhǔn),所以會(huì)出現(xiàn)負(fù)數(shù)的坐標(biāo)值。

?

? ??//設(shè)置狀態(tài)欄旋轉(zhuǎn)

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

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

? ??//設(shè)置旋轉(zhuǎn)動(dòng)畫(huà)

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

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

? ??//設(shè)置導(dǎo)航欄旋轉(zhuǎn)

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

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

? ??//設(shè)置視圖旋轉(zhuǎn)

? ??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];

?

來(lái)源:?<http://blog.csdn.net/dickenslian/article/details/7407221>

?

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

?

一直遇到這個(gè)問(wèn)題,今天終于找到了解決方法.

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

強(qiáng)制轉(zhuǎn)成橫屏:

?

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喲.

方法二: 通過(guò)判斷狀態(tài)欄來(lái)設(shè)置視圖的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;

}

轉(zhuǎn)載于:https://www.cnblogs.com/zxykit/p/5274887.html

總結(jié)

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

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。