我需要像 Youtube 全屏那样修复纵向或横向左方向。 当用户单击按钮时,它会更改为纵向或横向左。 并被修复。 用户只能通过按钮进行控制。
接受设备方向 纵向、横向 一般为左 这是我的代码
应用委托(delegate)
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(restrictRotation)
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskLandscapeLeft;
}
View Controller
-(void) restrictRotation:(BOOL) restriction
{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate->restrictRotation = restriction;
}
- (IBAction)rotateOrientationAction:(id)sender
{
[self restrictRotation:NO];
if(isPortrait){
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
self.myNewScrollViewHeight.constant = self.view.frame.size.height - self.naviBar.frame.size.height - self.horizMenu.frame.size.height;
self.portraitMenuView.hidden = YES;
}else{
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
self.myNewScrollViewHeight.constant = self.view.frame.size.height * 0.5;
self.portraitMenuView.hidden = NO;
}
isPortrait = !isPortrait;
[self restrictRotation:YES];
}
如果我单击该按钮,它会更改为向左横向,但不会再次更改为纵向。
谢谢
最佳答案
您的条件不满足restrictRotation
,请检查以下代码以获得所需结果。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
isPortrait = YES;
}
-(void) restrictRotation:(BOOL) restriction
{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = restriction;
}
- (IBAction)rotateOrientationAction:(id)sender
{
if (isPortrait) {
[self restrictRotation:NO];
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
} else {
[self restrictRotation:YES];
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
}
isPortrait = !isPortrait;
}
更新:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
isPortrait = YES;
}
- (IBAction)rotateOrientationAction:(id)sender
{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = NO;
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
if (isPortrait)
{
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
}
else
{
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
}
isPortrait = !isPortrait;
}
关于ios - 如何确定方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45005909/