【问题标题】:Is there a way to catch an WillRotateToInterfaceOrientation event from an UIView?有没有办法从 UIView 捕获 WillRotateToInterfaceOrientation 事件?
【发布时间】:2010-11-24 16:29:27
【问题描述】:

每个 UIViewController 都有一个名为 willRotateToInterface 的方法。

是否也可以在 UIView 中执行此操作?

这是否符合模型视图控制器的想法?

我能想到的唯一方法是将事件从 UIViewController 发送到 UIView。

当前方向是否有全局变量?

【问题讨论】:

    标签: iphone objective-c uiview uiviewcontroller


    【解决方案1】:

    关注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】:

      您可以订阅全局通知并在设备旋转时接听电话,但它不会为您做任何事情..

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

      【讨论】:

        【解决方案3】:

        如果您只想在方向更改时将视图调整为新的大小/布局,您应该重写其layoutSubviews 方法。

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

        【讨论】:

        • 如果在 layoutSubviews 调用中所做的更改是可动画的,这将是一个完美的解决方案。
        猜你喜欢
        • 1970-01-01
        • 2021-05-20
        • 2010-09-13
        • 2016-09-12
        • 2019-04-30
        • 2017-12-23
        • 1970-01-01
        • 1970-01-01
        • 2011-08-12
        相关资源
        最近更新 更多