gpt4 book ai didi

iphone - 如何在设备旋转时旋转和调整 UIView 的大小

转载 作者:行者123 更新时间:2023-11-28 20:14:02 26 4
gpt4 key购买 nike

我有一个 UINavigationController 应用程序,用户可以在其中对多个导航 View 进行多个选择。然后在最终 View 中,我允许用户查看他们从先前 View 中的选择定义的信息。此信息显示在 NavigationController 堆栈的最终 View 的 subview 中。

示例:

UINavigationController
- Several Views including (final view)
Final View
- several Subviews

当加载最终 View 时,我创建了一个执行 didRotate 方法的 deviceOrientation 监听器。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];

然后我调用一个名为 currentView 的方法,该方法用于确定当前可见的 subview 。也就是说,每当我有类似 [finalview.view addSubView:firstView.view] 的东西时就会调用它

[self copyCurrentView:@"firstView"];


- (void)copyCurrentView:(NSString *)currentView {

myCurrentView = currentView;

}

现在当设备旋转时会触发此方法

-(void)didRotate:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

if (orientation == UIDeviceOrientationLandscapeLeft)
{
if ([myCurrentView isEqualToString:@"firstView"]) {

[self.navigationController.view insertSubview:firstView.view aboveSubview:self.navigationController.navigationBar];
[self.navigationController setNavigationBarHidden:YES animated:YES];

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
[firstView.view setBounds:CGRectMake(0, 0, 480.0, 320.0)];
[firstView.view setCenter:CGPointMake(320/2, 480/2)];
[firstView.view setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
}
//other if statments here


}
else if (orientation == UIDeviceOrientationLandscapeRight)
{
// do the same here
}
else if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown)
{
NSLog(@"@port");

if ([myCurrentView isEqualToString:@"firstView"]) {
[self.view insertSubview:firstView.view belowSubview:actionTabBar];
[self.navigationController setNavigationBarHidden:NO animated:YES];



[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
[firstView.view setBounds:CGRectMake(0.0, infoBarHeight, firstView.view.bounds.size.width, firstView.view.bounds.size.height)];
// [firstView.view setCenter:CGPointMake(480/2, 320/2)];
[firstView.view setTransform:CGAffineTransformMakeRotation(M_PI * 2)];
}
}
}

现在这看起来有点乱七八糟,而且可能是一种非常随机的方式。但是根据我在网上找到的所有内容,我无法弄清楚如何获得良好的效果。

目前,当我向左旋转设备时,它会完美地全屏显示.. 完全符合我的要求然而,当我再次旋转到纵向时,即使我使用了正确的尺寸等,它也永远不会回到原来的位置。

如果有人可以帮助我为当前可见的 subview 设置旋转模型 View ,将不胜感激。

我一直犹豫要不要问这个问题,因为很难以理解的方式解释这个问题。但我已尽我所能解释我所做的以及我需要的帮助。希望有人能帮助我。

问候

最佳答案

这就是最终对我有用的方法,这是在进行全屏旋转,希望对您有所帮助:

- (void)viewDidLoad
{
[super viewDidLoad];
isShowingLandscapeView = NO;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
// Do any additional setup after loading the view.
}

- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
CGRect rect = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);;


switch (deviceOrientation) {
case 1:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
self.view.transform = CGAffineTransformMakeRotation(0);
self.view.bounds = [[UIScreen mainScreen] bounds];
[UIView commitAnimations];
break;
case 2:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortraitUpsideDown animated:NO];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
self.view.transform = CGAffineTransformMakeRotation(-M_PI);
self.view.bounds = [[UIScreen mainScreen] bounds];
[UIView commitAnimations];
break;
case 3:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
//rect = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
self.view.bounds = rect;
[UIView commitAnimations];
break;
case 4:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
//rect = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);
self.view.transform = CGAffineTransformMakeRotation(-M_PI_2);
self.view.bounds = rect;
[UIView commitAnimations];
break;

default:
break;
}
}

关于iphone - 如何在设备旋转时旋转和调整 UIView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18863739/

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