需求如下:
1.app整体只能竖屏,部分页面才可以横屏
2.app整体只能竖屏,部分页面也是竖屏,但是点击某个按钮可以使当前页面变为横屏,如全屏视频播放键。
需求1解决方法:
1.在targets - general中设置设备支持方向如下,确保设备各个方法均支持
2.在appdelegate的.h文件声明属性来标记当前设备方向
@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中设置设备支持方向如下,确保整体都是竖屏
2.在横屏(如button点击)事件处,代码如下
- (void)fullScreenBtnAction {
if (_isNormalOrientation) { // 如果当前是默认的竖屏
//注:当前self是UIView对象,如果是VC,则为self.view.....
self.frame = CGRectMake(0, 0, kScreenSize.height, kScreenSize.width);
CGRect frame = [UIScreen mainScreen].applicationFrame;
// transfrom会以当前center为锚点旋转,所以旋转后位置有偏移,需要处理
CGPoint center = CGPointMake(frame.origin.x + ceil(frame.size.width/2), frame.origin.y + ceil(frame.size.height/2));
self.center = center;
//取状态栏旋转时间
// 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是进入横屏前self的frame
self.frame = _originRect;
}
_isNormalOrientation = !_isNormalOrientation; //更改状态
}
***方法2:
1.在targets - general中设置设备支持方向如下,确保设备各个方法均支持 (弃用 可不设置)
2.在appdelegate的.h文件声明属性interfaceOrientation标记方向
@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;
}
}
- 横竖屏转换:
//转换到竖屏
- (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;
}
//转换到横屏模式
- (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];
}
补充后来自己又折腾的几个Demo:
https://github.com/3KK3/AppdelegateMethodHorizDemo/tree/master