事件willAnimateRotationToInterfaceOrientation不调用[复制]

2019-08-22 02:18发布

这个问题已经在这里有一个答案:

  • 旋转的UIView - willRotateToInterfaceOrientation不会被调用 2个回答

我有几个UIViewController S于应用。 但是,只有在其中的一个willAnimateRotationToInterfaceOrientation事件被调用。

如何使willAnimateRotationToInterfaceOrientation工作所有可见的看法?

Answer 1:

RootViewController

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    NSDictionary *userInfo = @{
        @"toInterfaceOrientation":@(toInterfaceOrientation),
        @"duration":@(duration)};
    [[NSNotificationCenter defaultCenter] postNotificationName:@"willAnimateRotationToInterfaceOrientation" object:nil userInfo:userInfo];
}

其他UIViewControllers

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willAnimateRotationToInterfaceOrientation:) name:@"willAnimateRotationToInterfaceOrientation" object:nil];
    }
    return self;
}

- (void)willAnimateRotationToInterfaceOrientation:(NSNotification *)notification {
    UIInterfaceOrientation toOrientation = (UIInterfaceOrientation)[notification.userInfo[@"toOrientation"] intValue];
    NSTimeInterval duration = (UIInterfaceOrientation)[notification.userInfo[@"duration"] floatValue];
    [self willAnimateRotationToInterfaceOrientation:toOrientation duration:duration];
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
    // ...
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}


文章来源: Event willAnimateRotationToInterfaceOrientation isn't calling [duplicate]