ios键盘横屏_iOS 强制横屏、部分横屏等功能实践

本文介绍了如何在iOS应用中实现部分页面横屏显示,包括整体竖屏但部分页面可以横屏,以及点击全屏按钮实现横屏播放视频的功能。详细讲解了在目标设置、AppDelegate以及ViewController中的实现步骤和关键代码。
摘要由CSDN通过智能技术生成

需求如下:

1.app整体只能竖屏,部分页面才可以横屏

2.app整体只能竖屏,部分页面也是竖屏,但是点击某个按钮可以使当前页面变为横屏,如全屏视频播放键。

需求1解决方法:

1.在targets - general中设置设备支持方向如下,确保设备各个方法均支持

屏幕快照 2016-12-08 下午6.41.26.png

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中设置设备支持方向如下,确保整体都是竖屏

屏幕快照 2016-12-08 下午6.30.25.png

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中设置设备支持方向如下,确保设备各个方法均支持 (弃用  可不设置)

屏幕快照 2016-12-08 下午6.41.26.png

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

}

作者:CoderYZY

链接:https://www.jianshu.com/p/95cdf3262bc6

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值