【问题标题】:Orientation don't change in second time第二次方向不变
【发布时间】:2015-02-03 21:16:07
【问题描述】:

我在下面有这段代码,它识别设备何时改变他的方向,如果方向是横向的左或右,他又回到纵向:

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

    [[NSNotificationCenter defaultCenter]
     addObserver:self selector:@selector(orientationChanged:)
     name:UIDeviceOrientationDidChangeNotification
     object:[UIDevice currentDevice]];


- (void) orientationChanged:(NSNotification *)note{

    UIDevice * device = note.object;

    if(device.orientation == UIDeviceOrientationLandscapeRight){


        [[UIDevice currentDevice] setValue:
         [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                                    forKey:@"orientation"];
    }

    if(device.orientation == UIDeviceOrientationLandscapeLeft){


        [[UIDevice currentDevice] setValue:
         [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                                    forKey:@"orientation"];
    }

}

当我运行我的项目并将设备的方向更改为横向或横向时,此代码会识别并将方向更改为纵向,但如果我再次将设备旋转到横向或横向,则代码不起作用我的设备一直处于横向模式,为什么?以及如何解决这个问题?

【问题讨论】:

    标签: ios objective-c uiinterfaceorientation uidevice


    【解决方案1】:

    我不知道我是否正确,但请尝试从 viewWillAppear 触发您的通知

     -(void)viewWillDisappear:(BOOL)animated
    {
      [super viewWillAppear] ;
       [[NSNotificationCenter defaultCenter]
     addObserver:self selector:@selector(orientationChanged:)
     name:UIDeviceOrientationDidChangeNotification
     object:[UIDevice currentDevice]];
    
    }
    

    【讨论】:

      【解决方案2】:

      最好在viewWillAppear 中注册通知并在viewWillDisappear 方法中取消注册。这样只有在您的视图呈现时您才会收到通知。

      像这样实现:

      -(void)viewWillAppear:(BOOL)animated
      {
        [super viewWillAppear] ;
        [[NSNotificationCenter defaultCenter]
           addObserver:self selector:@selector(orientationChanged:)
           name:UIDeviceOrientationDidChangeNotification
           object:[UIDevice currentDevice]];
      }
      
      -(void)viewWillDisappear:(BOOL)animated
      {
        // Removing observer for lead created notification
        [[NSNotificationCenter defaultCenter] removeObserver:self];
      }
      

      如果UIDeviceOrientationDidChangeNotification 不能解决您的问题,请尝试如下 StatusBarOrientationNotification。

      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
      
      - (void)statusBarOrientationChange:(NSNotification *)note {
          UIInterfaceOrientation orientation = [note.userInfo[UIApplicationStatusBarOrientationUserInfoKey] integerValue];
      
          // handle changes
      }
      

      【讨论】:

        【解决方案3】:

        您应该使用supportedInterfaceOrientationsshouldAutorotate 等标准方法简单地锁定控制器的旋转,而不是弄乱私有API 并被AppStore 拒绝。

        【讨论】:

          猜你喜欢
          • 2012-03-23
          • 1970-01-01
          • 2017-03-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多