Skip to content

Instantly share code, notes, and snippets.

@zummenix
Created May 15, 2018 09:05
Show Gist options
  • Save zummenix/dfbe52b9ce390d44aff9fa68c4b0fa94 to your computer and use it in GitHub Desktop.
Save zummenix/dfbe52b9ce390d44aff9fa68c4b0fa94 to your computer and use it in GitHub Desktop.
Finds rotation difference between two interface orientations
/**
Finds rotation difference between two interface orientations.
For example, difference between Portrait and PortraitUpsideDown is 2 (rotate by 180 degrees),
defference between Portrait and LandscapeLeft is 1 (rotate by 90 degrees).
@param source source interface orientation
@param destination destination interface orientation
@return rotation difference between orientations in range [0, 3].
*/
NSInteger rotationDifference(UIInterfaceOrientation source, UIInterfaceOrientation destination) {
NSArray *matrix = @[
@(UIInterfaceOrientationPortrait),
@(UIInterfaceOrientationLandscapeLeft),
@(UIInterfaceOrientationPortraitUpsideDown),
@(UIInterfaceOrientationLandscapeRight),
];
NSInteger sourceIndex = [matrix indexOfObject:@(source)];
NSInteger destinationIndex = [matrix indexOfObject:@(destination)];
if (sourceIndex == NSNotFound || destinationIndex == NSNotFound) {
return 0;
} else {
return ABS(sourceIndex - destinationIndex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment