0

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

例子:

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

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

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

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

然后我调用一个名为 currentView 的方法,该方法用于确定当前可见的子视图。即当我有类似 [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)];
      }
    }
}

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

目前,当我向左旋转设备时,它会完美地全屏显示..就像我想要的一样,但是当我再次旋转到纵向时,即使我使用正确的尺寸等,它也永远不会回到原始位置。

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

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

问候

4

1 回答 1

3

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

- (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;
    }
}
于 2013-09-18T06:21:20.337 回答