事件willAnimateRotationToInterfaceOrientation不会调用

时间:2013-04-11 19:21:40

标签: iphone ios uiview uiviewcontroller rotation

我在应用程序上有几个UIViewController个。但只有其中一个willAnimateRotationToInterfaceOrientation事件正在调用。

如何让willAnimateRotationToInterfaceOrientation适用于所有可见视图?

1 个答案:

答案 0 :(得分:0)

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];
}