gpt4 book ai didi

iPhone以编程方式设置界面方向

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:04:18 25 4
gpt4 key购买 nike

我正在尝试以编程方式更改单个 UIViewController 的方向。用户应在设置中选择 View 是否为纵向。该值将保存在 NSUserDefaults 中,并且在特定的 ViewController 中应该更改方向。所以如果 Portrait 在 interfaceOrientation 应该在 UIInterfaceorientationUpsideDown......

我使用了以下方法,但它只会在用户将设备转到横向/纵向时改变方向...否则不会触发任何内容。

- (BOOL)shouldAutoRotateToInterfactOrientation:(UIInterfaceOrientation)interfaceOrientation

此外,我尝试了下一个(也使用方法名称“setOrientation”):

[[UIDevice currentDevice] orientation: UIDeviceOrientationPortraitUpsideDown];

但是同样,什么也没有发生,只是出现错误,因为 xcode 说没有这个名称的方法。

但是应该有另一种方法来以编程方式更改它......第一个方法也只是一个在设备位置之后处理 actionListener 的方法,但是应该有一种方法可以直接调用这个 actionListener ....我只是查看了 UIApplication 文件,但没有有用的方法....

最佳答案

你应该使用像这样的代码:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(rotationChanged:)
name:@"UIDeviceOrientationDidChangeNotification"
object:nil];

然后,根据方向改变 View :

-(void)rotationChanged:(NSNotification *)notification{
NSInteger orientation = [[UIDevice currentDevice] orientation];
UIWindow *_window = [[[UIApplication sharedApplication] delegate] window];
switch (orientation) {
case 1:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];

[_window setTransform:CGAffineTransformMakeRotation (0)];
[_window setFrame:CGRectMake(0, 0, 320, 480)];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];

[UIView commitAnimations];
break;
case 2:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];

[_window setTransform:CGAffineTransformMakeRotation (M_PI)];
[_window setFrame:CGRectMake(0, 0, 320, 480)];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];

[UIView commitAnimations];
break;
case 3:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];

[_window setTransform:CGAffineTransformMakeRotation (M_PI / 2)];
[_window setFrame:CGRectMake(0, 0, 320, 480)];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];

[UIView commitAnimations];
break;
case 4:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];

[_window setTransform:CGAffineTransformMakeRotation (- M_PI / 2)];
[_window setFrame:CGRectMake(0, 0, 320, 480)];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];

[UIView commitAnimations];
break;
default:
break;
}
}

希望对你有帮助:P

关于iPhone以编程方式设置界面方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11109662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com