Created
October 9, 2014 10:47
-
-
Save orkenstein/55aaba40f4bcfe78fa36 to your computer and use it in GitHub Desktop.
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
// | |
// UIView+UIView_glow.m | |
// Hab It! | |
// | |
// Created by orkenstein on 30.09.14. | |
// Copyright (c) 2014 savefon.mobi. All rights reserved. | |
// | |
#import "UIView+glow.h" | |
#import <DProperty/DPPPropertyAttribute.h> | |
@interface GlowLayer : CALayer | |
@property (nonatomic, assign) CGFloat glowSize; | |
@property (nonatomic, strong) UIColor *glowColor; | |
@property (nonatomic, assign) CGFloat innerGlowSize; | |
@property (nonatomic, strong) UIColor *innerGlowColor; | |
@property (nonatomic, assign) UIView *parentView; | |
@end | |
@interface UIView (glow_private) | |
@property (nonatomic, strong) GlowLayer *glowLayer; | |
@property (nonatomic, assign) BOOL glowHiddenPrivate; | |
@end | |
@implementation UIView (glow_private) | |
@dynamic glowHiddenPrivate; | |
@dynamic glowLayer; | |
@end | |
@implementation UIView (glow) | |
@dynamic glowSize; | |
@dynamic glowColor; | |
@dynamic innerGlowSize; | |
@dynamic innerGlowColor; | |
- (void)setGlowHidden:(BOOL)glowHidden { | |
self.layer.masksToBounds = glowHidden; | |
self.clipsToBounds = glowHidden; | |
if (self.glowLayer == nil) { | |
self.glowLayer = [GlowLayer layer]; | |
self.glowLayer.frame = self.layer.bounds; | |
self.glowLayer.parentView = self; | |
self.glowLayer.masksToBounds = NO; | |
self.glowLayer.contentsGravity = kCAGravityCenter; | |
[self.layer addSublayer:self.glowLayer]; | |
} | |
self.glowLayer.glowSize = self.glowSize; | |
self.glowLayer.glowColor = self.glowColor; | |
self.glowLayer.innerGlowSize = self.innerGlowSize; | |
self.glowLayer.innerGlowColor = self.innerGlowColor; | |
self.glowLayer.hidden = glowHidden; | |
self.glowHiddenPrivate = glowHidden; | |
[self.glowLayer setNeedsDisplay]; | |
[self.glowLayer performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:0.0]; | |
} | |
- (BOOL)glowHidden { | |
return self.glowHiddenPrivate; | |
} | |
@end | |
@implementation GlowLayer | |
+ (instancetype)layer { | |
GlowLayer *layer = [super layer]; | |
layer.contentsScale = [[UIScreen mainScreen] scale]; | |
return layer; | |
} | |
- (void)drawInContext:(CGContextRef)ctx { | |
CGFloat delta = MAX(self.glowSize, self.innerGlowSize); | |
CGRect drawingRect = CGRectMake(-delta, -delta, CGRectGetWidth(self.parentView.frame) + 2 * delta, | |
CGRectGetHeight(self.parentView.frame) + 2 * delta); | |
CGPoint offsetPoint = CGPointMake(-drawingRect.origin.x, -drawingRect.origin.y); | |
self.frame = drawingRect; | |
[self setNeedsLayout]; | |
[self layoutIfNeeded]; | |
UIGraphicsBeginImageContextWithOptions(drawingRect.size, NO, 0.0f); | |
// [self.parentView drawViewHierarchyInRect:rect afterScreenUpdates:YES]; | |
[self.parentView.layer renderInContext:UIGraphicsGetCurrentContext()]; | |
UIImage *textImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
UIGraphicsPushContext(ctx); | |
CGContextSaveGState(ctx); | |
if (_glowSize > 0) { | |
CGContextSetShadow(ctx, CGSizeZero, _glowSize); | |
CGContextSetShadowWithColor(ctx, CGSizeZero, _glowSize, _glowColor.CGColor); | |
} | |
[textImage drawAtPoint:offsetPoint]; | |
CGContextRestoreGState(ctx); | |
if (_innerGlowSize > 0) { | |
UIGraphicsBeginImageContextWithOptions(drawingRect.size, NO, 0.0f); | |
CGContextRef ctx2 = UIGraphicsGetCurrentContext(); | |
CGContextSaveGState(ctx2); | |
CGContextSetFillColorWithColor(ctx2, [UIColor blackColor].CGColor); | |
CGContextFillRect(ctx2, drawingRect); | |
CGContextTranslateCTM(ctx2, 0.0, drawingRect.size.height); | |
CGContextScaleCTM(ctx2, 1.0, -1.0); | |
CGContextClipToMask(ctx2, drawingRect, textImage.CGImage); | |
CGContextClearRect(ctx2, drawingRect); | |
CGContextRestoreGState(ctx2); | |
UIImage *inverted = UIGraphicsGetImageFromCurrentImageContext(); | |
CGContextClearRect(ctx2, drawingRect); | |
CGContextSaveGState(ctx2); | |
CGContextSetFillColorWithColor(ctx2, _innerGlowColor.CGColor); | |
CGContextSetShadowWithColor(ctx2, CGSizeZero, _innerGlowSize, _innerGlowColor.CGColor); | |
[inverted drawAtPoint:CGPointZero]; | |
CGContextTranslateCTM(ctx2, 0.0, drawingRect.size.height); | |
CGContextScaleCTM(ctx2, 1.0, -1.0); | |
CGContextClipToMask(ctx2, drawingRect, inverted.CGImage); | |
CGContextClearRect(ctx2, drawingRect); | |
CGContextRestoreGState(ctx2); | |
UIImage *innerShadow = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
[innerShadow drawAtPoint:offsetPoint]; | |
} | |
// textImage = UIGraphicsGetImageFromCurrentImageContext(); | |
// self.contents = (id)textImage.CGImage; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment