需求如下:
1.app整體只能豎屏,部分頁面才可以橫屏
2.app整體只能豎屏,部分頁面也是豎屏,但是點(diǎn)擊某個(gè)按鈕可以使當(dāng)前頁面變?yōu)闄M屏,如全屏視頻播放鍵。
需求1解決方法:
1.在targets - general中設(shè)置設(shè)備支持方向如下,確保設(shè)備各個(gè)方法均支持
屏幕快照 2016-12-08 下午6.41.26.png
2.在appdelegate的.h文件聲明屬性來標(biāo)記當(dāng)前設(shè)備方向
@property (assign , nonatomic)UIInterfaceOrientation interfaceOrientation;
在appdelegate的.m文件中:
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window {
if(self.interfaceOrientation == UIInterfaceOrientationUnknown) {
// 直播間用
return UIInterfaceOrientationMaskAllButUpsideDown;
} else if(self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
//交易頁面橫屏用
return UIInterfaceOrientationMaskLandscapeRight;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
3.在需要橫屏的ViewController的ViewWillApper
方法里面:
Appdelegate *delegate =p.p1[UIApplication sharedApplication].delegate;
delegate.canRotate = YES;
需求2解決方法:
方法1:
1.在targets - general中設(shè)置設(shè)備支持方向如下,確保整體都是豎屏
屏幕快照 2016-12-08 下午6.30.25.png
2.在橫屏(如button點(diǎn)擊)事件處,代碼如下
- (void)fullScreenBtnAction {
if (_isNormalOrientation) { // 如果當(dāng)前是默認(rèn)的豎屏
//注:當(dāng)前self是UIView對(duì)象,如果是VC,則為self.view.....
self.frame = CGRectMake(0, 0, kScreenSize.height, kScreenSize.width);
CGRect frame = [UIScreen mainScreen].applicationFrame;
// transfrom會(huì)以當(dāng)前center為錨點(diǎn)旋轉(zhuǎn),所以旋轉(zhuǎn)后位置有偏移,需要處理
CGPoint center = CGPointMake(frame.origin.x + ceil(frame.size.width/2), frame.origin.y + ceil(frame.size.height/2));
self.center = center;
//取狀態(tài)欄旋轉(zhuǎn)時(shí)間
// CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 0.3];
self.transform = CGAffineTransformMakeRotation(M_PI_2);
[UIView commitAnimations];
} else {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 0.3];
self.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
//_originRect是進(jìn)入橫屏前self的frame
self.frame = _originRect;
}
_isNormalOrientation = !_isNormalOrientation; //更改狀態(tài)
}
***方法2:
1.在targets - general中設(shè)置設(shè)備支持方向如下,確保設(shè)備各個(gè)方法均支持 (棄用 可不設(shè)置)
屏幕快照 2016-12-08 下午6.41.26.png
2.在appdelegate的.h文件聲明屬性interfaceOrientation標(biāo)記方向
@property (assign , nonatomic)UIInterfaceOrientation interfaceOrientation;
在appdelegate的.m文件中實(shí)現(xiàn)設(shè)備方向方法:
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window {
if(self.interfaceOrientation == UIInterfaceOrientationUnknown) { // 直播間用
return UIInterfaceOrientationMaskAllButUpsideDown;
} else if(self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) { //交易頁面橫屏用
return UIInterfaceOrientationMaskLandscapeRight;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
- 橫豎屏轉(zhuǎn)換:
//轉(zhuǎn)換到豎屏
- (void)rotateToPortraitScreenWithInvocation {
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = UIDeviceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
delegate.interfaceOrientation = UIInterfaceOrientationPortrait;
}
//轉(zhuǎn)換到橫屏模式
- (void)rotateToLandscapWithIncovation {
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
delegate.interfaceOrientation = UIInterfaceOrientationUnknown;
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];
}
補(bǔ)充后來自己又折騰的幾個(gè)Demo:
https://github.com/3KK3/AppdelegateMethodHorizDemo/tree/master