Skip to content

Instantly share code, notes, and snippets.

@Inferis
Last active September 19, 2020 21:02
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Inferis/26ded6e1e8e625b3cd67 to your computer and use it in GitHub Desktop.
Save Inferis/26ded6e1e8e625b3cd67 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