1、手动旋转屏幕函数
- (void)setOrientation: (UIInterfaceOrientation)orientation {
if([[UIDevice currentDevice] respondsToSelector:
@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(
@"setOrientation:");
NSInvocation*invocation = [NSInvocationinvocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget: [UIDevice currentDevice]];
intval = orientation;
[invocation setArgument:&val atIndex:
2];
[invocation invoke];
}
}
在AppDelegate.h中定义一个变量:
BOOL allowRotation;
再定义一个宏:
然后在AppDelegate.m文件中 实现允许屏幕的方向函数
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if(self.allowRotation) {
returnUIInterfaceOrientationMaskAll;
}
returnUIInterfaceOrientationMaskPortrait;
}
2、自动旋转屏幕
1)在控制器中实现允许屏幕旋转的方法
- (BOOL)shouldAutorotate{
returnApplicationDelegate.allowRotation;
}
2)当想要旋转屏幕的时候,就去修改ApplicationDelegate.allowRotation的值,默认是NO,是不支持旋转的,所以满足我们的需求当我们从首页进入播放界面的时候在页面将要出现的时候 设置为YES在页面将要消失的时候 设置为NO:
- (void)viewDidDisappear:(BOOL)animated{
[superviewDidDisappear:animated];
ApplicationDelegate.allowRotation==NO;
}
- (void)viewDidAppear:(BOOL)animated{
[superviewDidAppear:animated];
ApplicationDelegate.allowRotation==YES;
}
3)控制器下添加一个通知,用来监听手机方向是否改变
[[NSNotificationCenterdefaultCenter] addObserver:self selector:
@selector(changeFrames) name:UIDeviceOrientationDidChangeNotification object:nil];
转载请注明原文地址: https://ju.6miu.com/read-39817.html