Skip to content

Instantly share code, notes, and snippets.

@jaminguy
Forked from Inferis/orientation.m
Last active August 29, 2015 14:20
Show Gist options
  • Save jaminguy/7c51259808b84d424cb2 to your computer and use it in GitHub Desktop.
Save jaminguy/7c51259808b84d424cb2 to your computer and use it in GitHub Desktop.
- (UIInterfaceOrientation)orientationByTransforming:(CGAffineTransform)transform fromOrientation:(UIInterfaceOrientation)c
{
CGFloat angle = atan2f(transform.b, transform.a);
NSInteger multiplier = (NSInteger)roundf(angle / M_PI_2);
UIInterfaceOrientation orientation = self.interfaceOrientation;
if (multiplier < 0) {
// clockwise rotation
while (multiplier++ < 0) {
switch (orientation) {
case UIInterfaceOrientationPortrait:
orientation = UIInterfaceOrientationLandscapeLeft;
break;
case UIInterfaceOrientationLandscapeLeft:
orientation = UIInterfaceOrientationPortraitUpsideDown;
break;
case UIInterfaceOrientationPortraitUpsideDown:
orientation = UIInterfaceOrientationLandscapeRight;
break;
case UIInterfaceOrientationLandscapeRight:
orientation = UIInterfaceOrientationPortrait;
break;
default:
break;
}
}
}
else if (multiplier > 0) {
// counter-clockwise rotation
while (multiplier-- > 0) {
switch (orientation) {
case UIInterfaceOrientationPortrait:
orientation = UIInterfaceOrientationLandscapeRight;
break;
case UIInterfaceOrientationLandscapeRight:
orientation = UIInterfaceOrientationPortraitUpsideDown;
break;
case UIInterfaceOrientationPortraitUpsideDown:
orientation = UIInterfaceOrientationLandscapeLeft;
break;
case UIInterfaceOrientationLandscapeLeft:
orientation = UIInterfaceOrientationPortrait;
break;
default:
break;
}
}
}
return (UIInterfaceOrientation)orientation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment