react-native-camera/ios/RCTSensorOrientationChecker.m

102 lines
3.1 KiB
Mathematica
Raw Normal View History

//
// RCTSensorOrientationChecker.m
// RCTCamera
//
// Created by Radu Popovici on 24/03/16.
//
//
#import "RCTSensorOrientationChecker.h"
#import <CoreMotion/CoreMotion.h>
2016-03-24 08:12:57 +00:00
@interface RCTSensorOrientationChecker ()
@property (strong, nonatomic) CMMotionManager * motionManager;
2016-03-24 08:12:57 +00:00
@property (assign, nonatomic) UIInterfaceOrientation orientation;
@property (strong, nonatomic) RCTSensorCallback orientationCallback;
@end
@implementation RCTSensorOrientationChecker
2016-03-24 08:12:57 +00:00
- (instancetype)init
{
self = [super init];
if (self) {
// Initialization code
self.motionManager = [[CMMotionManager alloc] init];
self.motionManager.accelerometerUpdateInterval = 0.2;
self.motionManager.gyroUpdateInterval = 0.2;
self.orientationCallback = nil;
}
return self;
}
- (void)resume
{
__weak __typeof(self) weakSelf = self;
[self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue new]
withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
if (!error) {
2016-03-24 08:12:57 +00:00
self.orientation = [weakSelf getOrientationBy:accelerometerData.acceleration];
}
2016-03-24 08:12:57 +00:00
if (self.orientationCallback) {
self.orientationCallback(self.orientation);
}
}];
2016-03-24 08:12:57 +00:00
}
- (void)pause
{
[self.motionManager stopAccelerometerUpdates];
}
- (void)getDeviceOrientationWithBlock:(RCTSensorCallback)callback
{
__weak __typeof(self) weakSelf = self;
self.orientationCallback = ^(UIInterfaceOrientation orientation) {
if (callback) {
callback(orientation);
}
weakSelf.orientationCallback = nil;
[weakSelf pause];
};
[self resume];
}
- (UIInterfaceOrientation)getOrientationBy:(CMAcceleration)acceleration
{
if(acceleration.x >= 0.75) {
return UIInterfaceOrientationLandscapeLeft;
}
if(acceleration.x <= -0.75) {
return UIInterfaceOrientationLandscapeRight;
}
if(acceleration.y >= -0.75) {
return UIInterfaceOrientationPortrait;
}
if(acceleration.y >= 0.75) {
return UIInterfaceOrientationPortraitUpsideDown;
}
return [[UIApplication sharedApplication] statusBarOrientation];
}
- (AVCaptureVideoOrientation)convertToAVCaptureVideoOrientation:(UIInterfaceOrientation)orientation
{
switch (orientation) {
case UIInterfaceOrientationPortrait:
return AVCaptureVideoOrientationPortrait;
case UIInterfaceOrientationPortraitUpsideDown:
return AVCaptureVideoOrientationPortraitUpsideDown;
case UIInterfaceOrientationLandscapeLeft:
return AVCaptureVideoOrientationLandscapeLeft;
case UIInterfaceOrientationLandscapeRight:
return AVCaptureVideoOrientationLandscapeRight;
default:
return 0; // unknown
}
}
@end