Skip to content

Instantly share code, notes, and snippets.

@NosovPavel
Forked from Inferis/orientation.m
Created April 27, 2017 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NosovPavel/65f59efc1738eb5fa2b46d4b49caf4a8 to your computer and use it in GitHub Desktop.
Save NosovPavel/65f59efc1738eb5fa2b46d4b49caf4a8 to your computer and use it in GitHub Desktop.
Calculate InterfaceOrientation from a transition coordinator transform
- (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