forked from mattconnolly/uidevice-extension
-
Notifications
You must be signed in to change notification settings - Fork 4
/
UIDevice-Orientation.m
131 lines (110 loc) · 3.74 KB
/
UIDevice-Orientation.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
/*
Erica Sadun, http://ericasadun.com
iPhone Developer's Cookbook, 6.0 Edition
BSD License, Use at your own risk
*/
// Thanks jweinberg, Emanuele Vulcano, rincewind42, Jonah Williams
#import "UIDevice-Orientation.h"
@implementation UIDevice (Orientation)
const CGFloat to_angle_upside_down = ( CGFloat )M_PI;
const CGFloat to_angle_landscape_left = ( CGFloat )-( M_PI/2.0f );
const CGFloat to_angle_landscape_right = ( CGFloat )( M_PI/2.0f );
const CGFloat from_angle = ( CGFloat )( M_PI * 2.0f );
#pragma mark current angle
CGFloat device_angle;
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
double xx = acceleration.x;
double yy = -acceleration.y;
device_angle = ( CGFloat )( M_PI / 2.0f - atan2(yy, xx) );
if (device_angle > M_PI)
device_angle -= 2 * M_PI;
CFRunLoopStop(CFRunLoopGetCurrent());
}
- (CGFloat) orientationAngle
{
#if TARGET_IPHONE_SIMULATOR
switch (self.orientation)
{
case UIDeviceOrientationPortrait:
return 0.0f;
case UIDeviceOrientationPortraitUpsideDown:
return to_angle_upside_down;
case UIDeviceOrientationLandscapeLeft:
return to_angle_landscape_left;
case UIDeviceOrientationLandscapeRight:
return to_angle_landscape_right;
default:
return 0.0f;
}
#else
// supercede current delegate
id priorDelegate = [UIAccelerometer sharedAccelerometer].delegate;
[UIAccelerometer sharedAccelerometer].delegate = self;
// Wait for a reading
CFRunLoopRun();
// restore delgate
[UIAccelerometer sharedAccelerometer].delegate = priorDelegate;
return device_angle;
#endif
}
// Limited to the four portrait/landscape options
- (UIDeviceOrientation) acceleratorBasedOrientation
{
CGFloat baseAngle = self.orientationAngle;
if ((baseAngle > -M_PI_4) && (baseAngle < M_PI_4))
return UIDeviceOrientationPortrait;
if ((baseAngle < -M_PI_4) && (baseAngle > -3 * M_PI_4))
return UIDeviceOrientationLandscapeLeft;
if ((baseAngle > M_PI_4) && (baseAngle < 3 * M_PI_4))
return UIDeviceOrientationLandscapeRight;
return UIDeviceOrientationPortraitUpsideDown;
}
#pragma mark relative orientation
// Thanks Jonah Williams
- (CGFloat) orientationAngleRelativeToOrientation:(UIDeviceOrientation) someOrientation
{
CGFloat dOrientation = 0.0f;
switch (someOrientation)
{
case UIDeviceOrientationPortraitUpsideDown: {dOrientation = to_angle_upside_down; break;}
case UIDeviceOrientationLandscapeLeft: {dOrientation = to_angle_landscape_left; break;}
case UIDeviceOrientationLandscapeRight: {dOrientation = to_angle_landscape_right; break;}
default: break;
}
CGFloat adjustedAngle = fmodf(self.orientationAngle - dOrientation, from_angle );
if (adjustedAngle > (M_PI + 0.01f))
adjustedAngle = ( adjustedAngle - from_angle );
return adjustedAngle;
}
#pragma mark basic orientation
- (BOOL) isLandscape
{
return UIDeviceOrientationIsLandscape(self.orientation);
}
- (BOOL) isPortrait
{
return UIDeviceOrientationIsPortrait(self.orientation);
}
// Transform to real world-readable string for arbitrary orientation
+ (NSString *) orientationString: (UIDeviceOrientation) orientation
{
switch (orientation)
{
case UIDeviceOrientationUnknown: return @"Unknown";
case UIDeviceOrientationPortrait: return @"Portrait";
case UIDeviceOrientationPortraitUpsideDown: return @"Portrait Upside Down";
case UIDeviceOrientationLandscapeLeft: return @"Landscape Left";
case UIDeviceOrientationLandscapeRight: return @"Landscape Right";
case UIDeviceOrientationFaceUp: return @"Face Up";
case UIDeviceOrientationFaceDown: return @"Face Down";
default: break;
}
return nil;
}
// String for current orientaiton
- (NSString *) orientationString
{
return [UIDevice orientationString:self.orientation];
}
@end