Last active
August 29, 2015 14:03
-
-
Save Shilo/228fb44222e0c73d9b8b to your computer and use it in GitHub Desktop.
Category for Sparrow to change the color of SPViewController auto rotation border.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SPViewController+AutoRotationBorderColor.h | |
// Barebone | |
// | |
// Created by Shilo White on 7/3/14. | |
// | |
// | |
#import <Sparrow/Sparrow.h> | |
@interface SPViewController (AutoRotationBorderColor) | |
@property (nonatomic, retain) UIColor *autoRotationBorderColor; | |
@property (nonatomic, assign) uint autoRotationBorderHexColor; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SPViewController+AutoRotationBorderColor.m | |
// Barebone | |
// | |
// Created by Shilo White on 7/3/14. | |
// | |
// | |
#import "SPViewController+AutoRotationBorderColor.h" | |
#import <objc/runtime.h> | |
@implementation SPViewController (AutoRotationBorderColor) | |
- (void)setAutoRotationBorderColor:(UIColor *)autoRotationBorderColor | |
{ | |
objc_setAssociatedObject(self, @selector(autoRotationBorderColor), autoRotationBorderColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
- (UIColor *)autoRotationBorderColor | |
{ | |
return objc_getAssociatedObject(self, @selector(autoRotationBorderColor)); | |
} | |
- (void)setAutoRotationBorderHexColor:(uint)autoRotationBorderHexColor | |
{ | |
self.autoRotationBorderColor = [UIColor colorWithRed:SP_COLOR_PART_RED(autoRotationBorderHexColor)/255.0f | |
green:SP_COLOR_PART_GREEN(autoRotationBorderHexColor)/255.0f | |
blue:SP_COLOR_PART_BLUE(autoRotationBorderHexColor)/255.0f | |
alpha:1.0f]; | |
} | |
- (uint)autoRotationBorderHexColor | |
{ | |
UIColor *autoRotationBorderColor = self.autoRotationBorderColor; | |
float red, green, blue; | |
if ([autoRotationBorderColor getRed:&red green:&green blue:&blue alpha:NULL]) | |
{ | |
uint redInt = (uint)(red * 255 + 0.5); | |
uint greenInt = (uint)(green * 255 + 0.5); | |
uint blueInt = (uint)(blue * 255 + 0.5); | |
return (redInt << 16) | (greenInt << 8) | blueInt; | |
} | |
return 0; | |
} | |
- (UIView *)borderView | |
{ | |
CGRect windowBounds = [Sparrow currentController].view.window.bounds; | |
for (UIView *view in self.view.superview.subviews) | |
{ | |
if (CGRectEqualToRect(view.frame, windowBounds)) | |
{ | |
CGSize viewBoundsSize = view.bounds.size; | |
NSArray *subviews = view.subviews; | |
if (subviews.count == 4) | |
{ | |
for (UIView *subview in subviews) | |
{ | |
CGRect frame = subview.frame; | |
if (!CGSizeEqualToSize(frame.size, viewBoundsSize)) continue; | |
if (frame.origin.x != -viewBoundsSize.width && | |
frame.origin.x != viewBoundsSize.width && | |
frame.origin.y != -viewBoundsSize.height && | |
frame.origin.y != viewBoundsSize.height) continue; | |
} | |
return view; | |
} | |
} | |
} | |
return nil; | |
} | |
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation | |
duration:(NSTimeInterval)duration | |
{ | |
// inform all display objects about the new game size | |
BOOL isPortrait = UIInterfaceOrientationIsPortrait(interfaceOrientation); | |
SPStage *stage = self.stage; | |
float newWidth = isPortrait ? MIN(stage.width, stage.height) : | |
MAX(stage.width, stage.height); | |
float newHeight = isPortrait ? MAX(stage.width, stage.height) : | |
MIN(stage.width, stage.height); | |
if (newWidth != stage.width) | |
{ | |
stage.width = newWidth; | |
stage.height = newHeight; | |
SPEvent *resizeEvent = [[SPResizeEvent alloc] initWithType:SPEventTypeResize | |
width:newWidth height:newHeight animationTime:duration]; | |
[stage broadcastEvent:resizeEvent]; | |
} | |
UIView *borderView = [self borderView]; | |
if (borderView) | |
{ | |
UIColor *autoRotationBorderColor = self.autoRotationBorderColor; | |
[UIView setAnimationsEnabled:NO]; | |
for (UIView *subview in borderView.subviews) | |
subview.backgroundColor = autoRotationBorderColor; | |
[UIView setAnimationsEnabled:YES]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment