shouldAutorotateToInterfaceOrientation:一段时间后无法调用

时间:2011-06-01 12:17:45

标签: iphone objective-c uiviewcontroller

我有一个项目,在删除/添加大量view / viewControllers后旋转事件停止触发。它工作了一段时间然后随机添加到窗口的新视图永远不会得到shouldAutorotateToInterfaceOrientation:called。

状态栏也无法旋转,但视图/视图控制器功能正常(减去旋转)。此外,一次只有一个视图/ viewController添加到窗口中。我假设旧的viewController被注册为键,但添加到窗口的所有内容都是视图控制器的子类,并且除了活动分区之外,没有任何子类显示在仪器分配中。

在app委托或窗口上是否有办法找到哪个视图控制器是关键?

还有其他原因导致shouldAutorotateToInterfaceOrientation:无法被调用吗?

每个视图控制器正在实施:

- (BOOL)shouldAutorotateToInterfaceOrientation:

(UIInterfaceOrientation)interfaceOrientation {         返回YES;     }

- (void) willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation) 
                     interfaceOrientation duration: (NSTimeInterval) duration {
    //manual rotation code
}

以下是我在应用程序委托中使用的代码,用于更改根视图控制器和显示的视图。

[UIView transitionWithView:new.view 
                  duration:0.3
                   options:UIViewAnimationOptionCurveEaseOut
                animations:^{
                    //add new view
                    [self.window addSubview:new.view];

                    //play transition animation for old and new views
                    new.view.transform = CGAffineTransformConcat(new.view.transform, newEnd);
                    old.view.transform = CGAffineTransformConcat(old.view.transform, oldEnd);

                    //if old view is a "modal style" then keep it in front on new view
                    if([old conformsToProtocol:@protocol(PreloadableModalView)]) {
                        [self.window bringSubviewToFront:old.view];
                    }

                }
                completion:^(BOOL finished){
                    //remove old view
                    [old removeSubviews];
                    [old.view removeFromSuperview];
                }];

1 个答案:

答案 0 :(得分:0)

看起来问题的根源是删除了窗口在转换期间发送旋转事件的viewController导致窗口有时不将新的viewcontroller注册为新的根。

我只有一个UIView(有自己的UIViewController)添加到Window。在动画期间,我正在删除根视图控制器并在屏幕上添加一个新的控制器。修复是在动画之前切换视图控制器。

  1. 我创建了当前视图的屏幕截图

  2. 删除了旧视图控制器

  3. 将新视图控制器设置为rootviewcontroller并将视图添加到窗口

  4. 运行过渡动画

  5. oncomplete我删除图片

  6. 这是代码的主要部分:

    //create a image of old view and add to window
    UIGraphicsBeginImageContext(old.view.bounds.size);
    [[old.view layer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageView *screenshot = [[UIImageView alloc] initWithImage:snapshot];
    [self.window addSubview:screenshot];
    [screenshot release];
    
    //remove view and viewcontroller
    [old.view removeFromSuperview];
    
    //set new viewcontroller as root
    if(self.window.rootViewController)
        self.window.rootViewController = nil;
    self.window.rootViewController = new;
    
    //transition code goes here
    
    
    //animate
    [UIView transitionWithView:new.view 
                      duration:0.5
                       options:UIViewAnimationOptionCurveEaseOut
                    animations:^{
    
                        //add new view
                        [self.window addSubview:new.view];
    
                        //play transition animation
                        if(isOldModalDisplay) {
                            screenshot.transform = CGAffineTransformConcat(new.view.transform, newEnd);
                            [self.window bringSubviewToFront:screenshot];
                        } else {
                            new.view.transform = CGAffineTransformConcat(new.view.transform, newEnd);
                        }
                    }
                    completion:^(BOOL finished){
                        //remove screenshot
                        [screenshot removeFromSuperview];
                    }];