YBPopupMenuDeviceOrientationManager.m 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //
  2. // YBPopupMenuDeviceOrientationManager.m
  3. // YBPopupMenuDemo
  4. //
  5. // Created by liyuanbo on 2020/1/19.
  6. // Copyright © 2020 LYB. All rights reserved.
  7. //
  8. #import "YBPopupMenuDeviceOrientationManager.h"
  9. @implementation YBPopupMenuDeviceOrientationManager
  10. @synthesize autoRotateWhenDeviceOrientationChanged = _autoRotateWhenDeviceOrientationChanged;
  11. @synthesize deviceOrientDidChangeHandle = _deviceOrientDidChangeHandle;
  12. + (id<YBPopupMenuDeviceOrientationManager>)manager
  13. {
  14. YBPopupMenuDeviceOrientationManager * manager = [[YBPopupMenuDeviceOrientationManager alloc] init];
  15. manager.autoRotateWhenDeviceOrientationChanged = YES;
  16. return manager;
  17. }
  18. - (void)startMonitorDeviceOrientation
  19. {
  20. if (!self.autoRotateWhenDeviceOrientationChanged) return;
  21. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onDeviceOrientationDidChangedNotification:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
  22. }
  23. - (void)endMonitorDeviceOrientation
  24. {
  25. if (!self.autoRotateWhenDeviceOrientationChanged) return;
  26. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
  27. }
  28. #pragma mark - notify
  29. - (void)onDeviceOrientationDidChangedNotification:(NSNotification *)notify
  30. {
  31. if (!self.autoRotateWhenDeviceOrientationChanged) return;
  32. UIInterfaceOrientation orientation = [notify.userInfo[UIApplicationStatusBarOrientationUserInfoKey] integerValue];
  33. if (_deviceOrientDidChangeHandle) {
  34. _deviceOrientDidChangeHandle(orientation);
  35. }
  36. }
  37. @end