界面旋轉(zhuǎn)準備
- 在AppDelegate.h中添加屬性
//是否旋轉(zhuǎn)
@property (nonatomic, assign) BOOL isRotation;
- 在AppDelegate.m中添加方法
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.isRotation) {
//可以針對不同的界面創(chuàng)建不同的值孝治,進行返回
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskPortrait;
}
- 進入正題审磁,找到需要旋轉(zhuǎn)的界面
- (IBAction)btnClick:(id)sender {
NSLog(@"旋轉(zhuǎn)");
//支持旋轉(zhuǎn)
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
app.isRotation = YES;
[self interfaceOrientation:UIInterfaceOrientationLandscapeLeft];
}
- (void)interfaceOrientation:(UIInterfaceOrientation)orientation
{
//強制轉(zhuǎn)換
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 = orientation;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
//關閉旋轉(zhuǎn)(恢復原狀)
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
app.isRotation = NO;
[self interfaceOrientation:UIInterfaceOrientationPortrait];
}
需要注意的是這個值可以在你真的需要旋轉(zhuǎn)的時候進行賦值,比如在視頻播放的時候全屏點擊的時候進行處理