代码之家  ›  专栏  ›  技术社区  ›  jantimon

是否有方法从UIView捕获WillRotateToInterfaceOrientation事件?

  •  6
  • jantimon  · 技术社区  · 17 年前

    在UIView中也可以做到这一点吗?

    这与模型视图控制器的想法相匹配吗?

    3 回复  |  直到 17 年前
        1
  •  17
  •   Rob Napier    17 年前

    观察 UIDeviceOrientationDidChangeNotification :

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
    
    ...
    
    - (void)orientationDidChange:(NSNotification *)note
    {
        NSLog(@"new orientation = %d", [[UIDevice currentDevice] orientation]);
    }
    

    UIDevice Class Reference

    我应该注意到,你没法补充 -beginGeneratingDeviceOrientationNotifications 当您希望发送这些通知时,请致电 -endGeneratingDeviceOrientationNotifications 当你想让他们停下来的时候。生成这些会对电池产生影响,因此您应该只在屏幕上显示视图时才这样做。 UIViewController 这一切都是为你做的,所以如果你有一个视图控制器,让它来做这项工作是值得的。

        2
  •  3
  •   Kornel    17 年前

    如果你只想在方向改变时将视图调整到新的大小/布局,你应该覆盖它 layoutSubviews 方法。

    每当视图的大小发生变化时,它都会被调用,这通常发生在视图旋转时。

        3
  •  2
  •   Daniel    17 年前

    您可以订阅全局通知,并在设备轮换时接到电话,但它不会为您做任何事情。。

    [NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(didRotate:)
    name:@"UIDeviceOrientationDidChangeNotification" object:nil];