PCSMotionManager.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // PCSMotionManager.m
  3. // LenzCameraNativeModuleForRN
  4. //
  5. // Created by 王昭威 on 2023/1/27.
  6. //
  7. #import "PCSMotionManager.h"
  8. #import <CoreMotion/CoreMotion.h>
  9. NSNotificationName const PCS_NotificationNameOrientationDidChange = @"OrientationDidChangeHandler";
  10. @interface PCSMotionManager ()
  11. @property(nonatomic) CMAcceleration userAcceleration;
  12. @property (nonatomic, copy) CMAccelerometerData* accelerometerData;
  13. @property (nonatomic, assign) UIDeviceOrientation currentDeviceOrientation;
  14. @property (nonatomic, strong) CMMotionManager* motionManager;
  15. @property (nonatomic, strong) NSOperationQueue* samplingQueue;
  16. @end
  17. @implementation PCSMotionManager
  18. + (instancetype)shared{
  19. static dispatch_once_t token;
  20. static PCSMotionManager* instance = nil;
  21. dispatch_once(&token, ^{
  22. instance = [[self alloc] init];
  23. });
  24. return instance;
  25. }
  26. - (instancetype)init{
  27. self = [super init];
  28. if(self){
  29. _motionManager = [[CMMotionManager alloc] init];
  30. _motionManager.accelerometerUpdateInterval = 0.2;
  31. _samplingQueue = [NSOperationQueue mainQueue];
  32. _samplingQueue.maxConcurrentOperationCount = 1;
  33. }
  34. return self;
  35. }
  36. - (AVCaptureVideoOrientation)videoOrientation:(nullable UIDeviceOrientation*)currentDeviceOrientation{
  37. CMMotionManager* motionMgr = self.motionManager;
  38. UIDeviceOrientation motionOrientation = UIDeviceOrientationPortrait;
  39. if(motionMgr.deviceMotion != nil){
  40. CMAcceleration acc = motionMgr.deviceMotion.gravity;
  41. if(fabs(acc.y) < fabs(acc.x)){
  42. motionOrientation = acc.x > 0 ? UIDeviceOrientationLandscapeRight : UIDeviceOrientationLandscapeLeft;
  43. }
  44. else{
  45. motionOrientation = acc.y > 0 ? UIDeviceOrientationPortraitUpsideDown : UIDeviceOrientationPortrait;
  46. }
  47. }
  48. if(currentDeviceOrientation != nil)
  49. *currentDeviceOrientation = motionOrientation;
  50. AVCaptureVideoOrientation videoOrientation = AVCaptureVideoOrientationPortrait;
  51. NSDictionary* table = @{
  52. @(UIDeviceOrientationPortraitUpsideDown): @(AVCaptureVideoOrientationPortraitUpsideDown),
  53. @(UIDeviceOrientationPortrait): @(AVCaptureVideoOrientationPortrait),
  54. @(UIDeviceOrientationLandscapeLeft): @(AVCaptureVideoOrientationLandscapeRight),
  55. @(UIDeviceOrientationLandscapeRight): @(AVCaptureVideoOrientationLandscapeLeft),
  56. };
  57. if(table[@(motionOrientation)] != nil){
  58. videoOrientation = (AVCaptureVideoOrientation)[table[@(motionOrientation)] integerValue];
  59. }
  60. return videoOrientation;
  61. }
  62. - (void)startAccelerometerUpdates{
  63. // if(self.motionManager.deviceMotionAvailable && !self.motionManager.deviceMotionActive){
  64. // [self startDeviceMotionUpdates];
  65. // }
  66. // if(self.motionManager.accelerometerAvailable){
  67. // [self.motionManager startAccelerometerUpdatesToQueue:self.samplingQueue withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
  68. //
  69. // if(error != nil){
  70. // NSLog(@"%@", error);
  71. // return;
  72. // }
  73. //
  74. // self.userAcceleration = self.motionManager.deviceMotion.userAcceleration;
  75. // double pitch = self.motionManager.deviceMotion.attitude.pitch;
  76. // double x = self.userAcceleration.x;
  77. // double y = self.userAcceleration.y;
  78. // double z = self.userAcceleration.z;
  79. //// NSLog(@"x:%lf| y:%lf| z:%lf| pitch:%lf",x,y,z,pitch);
  80. //
  81. // self.accelerometerData = accelerometerData;
  82. // UIDeviceOrientation currentOrientation = UIDeviceOrientationUnknown;
  83. // [self videoOrientation:&currentOrientation];
  84. // if(self.currentDeviceOrientation != currentOrientation){
  85. // UIDeviceOrientation old = self.currentDeviceOrientation;
  86. // self.currentDeviceOrientation = currentOrientation;
  87. // [[NSNotificationCenter defaultCenter] postNotificationName:PCS_NotificationNameOrientationDidChange object:@{@"new": @(currentOrientation), @"old": @(old)}];
  88. // }
  89. // }];
  90. // }
  91. }
  92. - (id)forwardingTargetForSelector:(SEL)aSelector{
  93. if(![self respondsToSelector:aSelector] && [self.motionManager respondsToSelector:aSelector]){
  94. return self.motionManager;
  95. }
  96. return nil;
  97. }
  98. - (void)stopAccelerometerUpdates {
  99. }
  100. @end