加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 由于Orientation / Shake,UINavigationController加载

发布时间:2020-12-14 19:44:56 所属栏目:百科 来源:网络整理
导读:背景:App有一个回家的功能.主视图仅支持纵向. 如果你比平时更加??晃动,你所看到的视图开始旋转(这很好),但随后它会检测到一个震动,并将popViewControlller发送到主视图.当它这样做时它加载导航控制器就好了,但是(主页内容)下的视图被加载到栏后面并被拉伸(
背景:App有一个回家的功能.主视图仅支持纵向.
如果你比平时更加??晃动,你所看到的视图开始旋转(这很好),但随后它会检测到一个震动,并将popViewControlller发送到主视图.当它这样做时它加载导航控制器就好了,但是(主页内容)下的视图被加载到栏后面并被拉伸(它基本上是在导航栏下方加载,因此它被拉伸)

后退按钮处理这个从风景到肖像很好(因为它不是中间过渡)

我应该如何处理这个方向更改(从摇动),以便我可以弹回到根视图控制器,而无需在导航栏下加载视图?

编辑:发生的事情是内容认为它有整个视图加载,所以它自己伸展整个屏幕,没有意识到它上面的导航栏.我可以看出,因为图像加载已拉长

增加了50美元的赏金.

编辑这里我是如何检测摇晃和弹出的

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{

    if ( event.subtype == UIEventSubtypeMotionShake )
    {

            UINavigationController *navController = self.navigationController;

            [[self retain] autorelease];
            HomeViewController *home = [[HomeViewController alloc]init];
            [navController popViewControllerAnimated:YES];

            home.title =@"Home View Controller";
            [home release];     
        }

    if ( [super respondsToSelector:@selector(motionEnded:withEvent:)] )
        [super motionEnded:motion withEvent:event];
}

这是我的App代表:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    navController = [[UINavigationController alloc]init];
    [self.window addSubview:navController.view];

    HomeViewController *home = [[HomeViewController alloc]init];
    [[self home] setFrame:[[UIScreen mainScreen] applicationFrame]];

我会在这里加一个模型.

普通视图:

摇晃/流行后的拉伸视图:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

解决方法

我对你的代码感到有些困惑,所以我真的建议从一开始就开始.正如Lukya所说,没有理由重新创建HomeViewController.我也对“[[自我保留]自动释放]感到困惑;”位.除非你在其他地方做错事,否则这不应该是必要的.

所以我会从这开始…在应用程序中:didFinishLaunchingWithOptions:做这样的事情:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    {
        HomeViewController *home = [[[HomeViewController alloc] init] autorelease];
        UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:home] autorelease];
        [self.window addSubview:navController.view];
    }

该窗口将保留您的导航控制器,导航控制器将保留您的HomeViewController.

然后在motionEnded:withEvent:做类似的事情:

    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
    {
        if (event.subtype == UIEventSubtypeMotionShake)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }

那应该是它.

如果这不起作用,那么你可以提供任何其他信息吗?例如,HomeViewController是否在shouldAutorotateToInterfaceOrientation中实现(并返回YES):?如果是这样,你可以返回no,因为你的第一行显示“Home view Only支持肖像”,它不会旋转吗?

编辑:willRotateToInterfaceOrientation的示例:duration:和didRotateFromInterfaceOrientation:以及.

在你正在检测抖动的控制器的标题中添加一个布尔值:


    BOOL isRotating;

在您的实现文件中添加我们想要覆盖的两个UIViewController方法 – 类似于:


    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
        [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
        isRotating = YES;
    }

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
        [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
        isRotating = NO;
    }

现在,为您的事件处理程序执行以下操作:

    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
    {
        if (event.subtype == UIEventSubtypeMotionShake && !isRotating)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读