-
Notifications
You must be signed in to change notification settings - Fork 6
/
RCTOrientationController.m
166 lines (136 loc) · 4.59 KB
/
RCTOrientationController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// RCTOrientationListener.m
//
// Created by Julien Sierra on 29/10/15.
//
#import "RCTOrientationController.h"
#import "RCTBridge.h"
@implementation RCTOrientationController
@synthesize bridge = _bridge;
- (instancetype)init
{
if ((self = [super init])) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (NSString*) getOrientationStr: (int) orientationNumber
{
NSString *orientationStr;
switch (orientationNumber) {
case 0:
orientationStr = @"PORTRAIT";
break;
case 1:
orientationStr = @"LANDSCAPE LEFT";
break;
case 2:
orientationStr = @"PORTRAIT UPSIDE DOWN";
break;
case 3:
orientationStr = @"LANDSCAPE RIGHT";
break;
default:
orientationStr = @"UNKNOWN";
break;
}
return orientationStr;
}
- (void)orientationDidChange:(NSNotification *)notification
{
[_bridge.eventDispatcher sendDeviceEventWithName:@"orientationDidChange" body:[self getData]];
}
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(getOrientation:(RCTResponseSenderBlock)callback)
{
callback([self getData]);
}
-(NSArray*)getData{
int deviceOrientation = [self getDeviceOrientationAsNumber];
int applicationOrientation = [self getApplicationOrientationAsNumber];
UIDevice *currentDevice = [UIDevice currentDevice];
NSString *deviceStr = [currentDevice model];
NSArray *orientationArray = @[[self getOrientationStr : deviceOrientation], [self getOrientationStr : applicationOrientation], deviceStr, [self getDimensions]];
return orientationArray;
}
-(NSDictionary*) getDimensions {
CGRect sizeRect = [[UIScreen mainScreen] bounds];
NSArray *keys = [NSArray arrayWithObjects:@"width", @"height", nil];
NSArray *objects = [NSArray arrayWithObjects:[NSNumber numberWithFloat:sizeRect.size.width], [NSNumber numberWithFloat:sizeRect.size.height], nil];
NSDictionary *size = [NSDictionary dictionaryWithObjects:objects
forKeys:keys];
return size;
}
-(int)getDeviceOrientationAsNumber{
UIDevice *currentDevice = [UIDevice currentDevice];
UIDeviceOrientation orientation = [currentDevice orientation];
switch (orientation) {
case UIDeviceOrientationPortrait:
return 0;
break;
case UIDeviceOrientationPortraitUpsideDown:
return 2;
break;
case UIDeviceOrientationLandscapeLeft:
return 1;
break;
case UIDeviceOrientationLandscapeRight:
return 3;
break;
default:
return 0;
break;
}
}
-(int)getApplicationOrientationAsNumber{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
switch (orientation) {
case UIInterfaceOrientationPortrait:
return 0;
break;
case UIInterfaceOrientationLandscapeLeft:
return 3;
break;
case UIInterfaceOrientationPortraitUpsideDown:
return 2;
break;
case UIInterfaceOrientationLandscapeRight:
return 1;
break;
default:
return 0;
break;
}
}
RCT_EXPORT_METHOD(rotate:(int)rotation)
{
if(rotation>3||rotation<1)rotation = 0;
NSNumber *value;
int orientation = ([self getApplicationOrientationAsNumber] + rotation)%4;
switch (orientation) {
case 1:
value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
break;
case 2:
value = [NSNumber numberWithInt:UIInterfaceOrientationPortraitUpsideDown];
break;
case 3:
value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
break;
case 0:
value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
break;
default:
break;
}
dispatch_async(dispatch_get_main_queue(), ^{
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[UIViewController attemptRotationToDeviceOrientation];
});
}
@end