2

我正在使用视图动画在视图之间切换,它的工作原理,但我遇到了界面方向的问题。

我在窗口上有两个视图。

  1. authenticationViewCont
  2. mainViewCont

两者都有一个按钮,当单击按钮时,authenticationViewCont我将其删除并显示mainViewCont,反之亦然。

一旦我 addSubviewauthenticationViewCont.view并将设备置于纵向模式然后将其删除,removeFromSuperview然后我将设备方向更改为横向,然后再次 addSubview authenticationViewCont。它首先以纵向显示动画并在动画后改变方向。

-(void)mainToAuthentication {
    CGRect originalFrame = authenticationViewCont.view.frame;
    CGRect modifiedFrame = originalFrame;
    modifiedFrame.origin.y = originalFrame.size.height;
    // made view out from screen
    authenticationViewCont.view.frame = modifiedFrame;
    // add sub view on top of other views
    [self.window addSubview:authenticationViewCont.view];
    // transiting view from bottom to center of screen
    [UIView animateWithDuration:0.5
        animations:^{ authenticationViewCont.view.frame = originalFrame; }
        completion:^(BOOL finished){ mainViewCont.view removeFromSuperview; }];
}

-(void)authenticationToMain {
    CGRect originalFrame = mainViewCont.view.frame;
    CGRect modifiedFrame = originalFrame;
    modifiedFrame.origin.y = -originalFrame.size.height;
    // made view out from screen
    mainViewCont.view.frame = modifiedFrame;
    // add sub view on top of other views
    [self.window addSubview:mainViewCont.view];
    // transiting view from top to center of screen
    [UIView animateWithDuration:0.5
        animations:^{ mainViewCont.view.frame = originalFrame; }
        completion:^(BOOL finished){ authenticationViewCont.view removeFromSuperview; }];
}

如何让它以当前界面方向显示,而不是它所在的旧界面方向removeFromSuperview

4

1 回答 1

0

我认为这里的问题是您最初将一个 viewController.view 添加到另一个 viewController.view 上,然后删除旧的。

窗口的问题只需要一个 rootViewController。所以窗口只会将旋转事件传递给第一个控制器。这意味着在调用 completionsBlock 之前,您的第二个 viewController 不会获得旋转事件。

我解决这个问题的方法是将这个切换代码放在窗口上的 rootViewController 中。然后,每当您进行切换时,您都可以传入 rootviewController 的当前旋转,并根据您传递的方向设置您的 authenticationViewController

于 2011-08-20T15:58:10.737 回答