強制橫屏:
方法一:
關(guān)于強制橫屏看了很多文章,首先第一個方法是invocation,這個方法可以實現(xiàn)橫屏效果,但是后來看了博客都不推薦使用這種方法,而且屏幕旋轉(zhuǎn)的時候,會出現(xiàn)空白的狀態(tài)
這個方法有個問題,現(xiàn)在旋轉(zhuǎn)的時候,頁面會出現(xiàn)先豎屏在橫屏的狀態(tài),不知道哪里出了問題?
源碼:
+ (void)interfaceOrientation:(UIInterfaceOrientation)orientation {
? ? 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;
? ? ? ? // 從2開始是因為0 1 兩個參數(shù)已經(jīng)被selector和target占用
? ? ? ? [invocation setArgument:&val atIndex:2];
? ? ? ? [invocation invoke];
? ? }
}
方法二:
因為頁面的跳轉(zhuǎn)方式全都使用了push方法,所以
-?(BOOL)shouldAutorotate ? ;
-?(UIInterfaceOrientationMask)supportedInterfaceOrientations ? ;
-?(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation ? ;
不能執(zhí)行(當然我也不確定是不是我操作的問題)
項目的需求:是在視頻頁面橫屏操作,然后從視頻頁面push到其他頁面,其他頁面在push到簽字頁面,簽字頁面也需要橫屏操作
在網(wǎng)上找到的方法:
第一步:
在appdelegate中聲明一個屬性@property (nonatomic, assign) BOOL allowRotation;
然后在appdelegate中實現(xiàn)方法
源碼:
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window {
? ? if (self.allowRotation == YES) {
? ? ? ? return UIInterfaceOrientationMaskLandscapeRight;
? ? }else{
? ? ? ? return UIInterfaceOrientationMaskPortrait;
? ? }
}
第二步:
在viewcontroller頁面中實現(xiàn)方法,實現(xiàn)旋轉(zhuǎn)屏幕
源碼:
- (void)setNewOrientation:(BOOL)fullscreen{
? ? if (fullscreen) {
? ? ? ? NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
? ? ? ? [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
? ? ? ? NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
? ? ? ? [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
? ? }else{
? ? ? ? NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
? ? ? ? [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
? ? ? ? NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
? ? ? ? [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
? ? }
}
第三步:
在需要旋轉(zhuǎn)屏幕頁面初始化的時候,調(diào)用橫屏方法
? ? ? ?AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
? ? ? ? appDelegate.allowRotation = YES;//(以上2行代碼,可以理解為打開橫屏開關(guān))
? ? ? ? [self setNewOrientation:YES];//調(diào)用轉(zhuǎn)屏代碼
在頁面返回按鈕的操作中,將屏幕返回豎屏狀態(tài)
? ? ? ?AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
? ? ? ? appDelegate.allowRotation = NO;//(以上2行代碼,可以理解為打開橫屏開關(guān))
? ? ? ? [self setNewOrientation:NO];//調(diào)用轉(zhuǎn)屏代碼
方法三:
使用view的動畫效果實現(xiàn)屏幕旋轉(zhuǎn),但是整個系統(tǒng)還依然是豎屏狀態(tài), 最后將此方法修改了.這里記錄下view的動畫效果
源碼:
? ?self.originFrame = self.view.frame;
? ? CGFloat height = [[UIScreen mainScreen] bounds].size.width;
? ? CGFloat width = [[UIScreen mainScreen] bounds].size.height;
? ? CGRect frame = CGRectMake((height - width) / 2, (width - height) / 2, width, height);
? ? self.frame = frame;
? ? [UIView animateWithDuration:0.3f animations:^{
? ? ? ? self.frame = frame;
? ? ? ? [self.view setTransform:CGAffineTransformMakeRotation(M_PI_2)];
? ? } completion:^(BOOL finished) {
? ? }];