实现点击按钮切换横竖屏的功能,设备锁屏无影响。效果如图所示:
方法一:通过setOrientation:方法
首先,要保证支持横竖屏,你需要在这里进行设置
支持横竖屏.png
其次,对需要旋转的界面进行处理:需要支持屏幕旋转,可对支持的方向进行控制
-(BOOL)shouldAutorotate{ //需要支持旋转屏幕
return YES; //默认YES
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{ //控制支持的方向
return _btn.selected?UIInterfaceOrientationMaskLandscapeRight:UIInterfaceOrientationMaskPortrait; //不希望屏幕自动旋转,通过这里控制,有时会失效。
//return UIInterfaceOrientationMaskAllButUpsideDown; //默认和Target中的设置相同
}
然后,就是通过利用NSInvocation调用对象的消息
/**
切换横竖屏
@param sender button
*/
- (IBAction)btnAction:(UIButton *)sender {
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 = UIDeviceOrientationPortrait;//竖屏
UIDeviceOrientation duration = [[UIDevice currentDevice]orientation];
if (UIDeviceOrientationPortrait == duration) {
val = UIInterfaceOrientationLandscapeLeft;//横屏
}
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
补充:上面有提到,不希望屏幕自动旋转,控制时有时会失效,比如你点击了按钮之后,旋转了手机,再次点击按钮。解决办法,添加监听设备方向的通知,如果是手动触发旋转屏,则返回;如果是设备旋转触发旋转屏,则重置屏幕方向。
//添加监听设备方向的通知
_inteface = UIInterfaceOrientationMaskPortrait;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:)name:UIDeviceOrientationDidChangeNotification object:nil];
//监听设备方向的通知方法
- (void)orientChange:(NSNotification *)noti {
if (_isManualRotate) {
_isManualRotate = NO;
return;
}
if (_inteface == UIInterfaceOrientationMaskPortrait) {
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 = UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke]; }
}else {
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];
}
}
}
方法二:通过设置视图的transform
首先:关闭自动旋转,否则状态栏不旋转。值得庆幸的是,界面是否支持旋转不影响效果
-(BOOL)shouldAutorotate{ //必须关闭旋转屏幕
return NO; //默认YES
}
然后:调用代码旋转状态栏和视图
CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
[UIView animateWithDuration:duration animations:^{
[[UIApplication sharedApplication] setStatusBarOrientation:_btn.selected ? UIInterfaceOrientationLandscapeRight : UIInterfaceOrientationPortrait];
self.view.transform = _btn.selected ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformIdentity;
self.view.frame = CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width);
}];
期待你的评论建议O(∩_∩)O~
网友评论