Skip to content

Commit

Permalink
Add clipsShadow property
Browse files Browse the repository at this point in the history
  • Loading branch information
peyton committed Feb 25, 2012
1 parent 8ce02bc commit a9af1bc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
6 changes: 6 additions & 0 deletions MOOMaskedIconView.h
Expand Up @@ -83,6 +83,7 @@ typedef enum {

UIColor *_shadowColor;
CGSize _shadowOffset;
BOOL _clipsShadow;
UIColor *_innerShadowColor;
CGSize _innerShadowOffset;

Expand Down Expand Up @@ -210,6 +211,11 @@ typedef enum {
*/
@property (nonatomic, assign) CGSize shadowOffset;

/*
* Set to YES if your icon is translucent and you don't want the shadow showing through.
*/
@property (nonatomic, assign) BOOL clipsShadow;

/*
* Specifies inner shadow color.
*/
Expand Down
63 changes: 31 additions & 32 deletions MOOMaskedIconView.m
Expand Up @@ -55,6 +55,7 @@ @implementation MOOMaskedIconView

@synthesize shadowColor = _shadowColor;
@synthesize shadowOffset = _shadowOffset;
@synthesize clipsShadow = _clipsShadow;
@synthesize innerShadowColor = _innerShadowColor;
@synthesize innerShadowOffset = _innerShadowOffset;

Expand Down Expand Up @@ -179,6 +180,10 @@ - (void)dealloc;
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGImageRef invertedMask = NULL;

if (!cblas_sdsdot) // Check for Accelerate framework. See TN2064: https://developer.apple.com/library/mac/#technotes/tn2064/_index.html
NSLog(@"Shadows are unavailable. No Accelerate framework.");

// Flip coordinates so images don't draw upside down
CGContextTranslateCTM(context, 0.0f, CGRectGetHeight(rect));
Expand All @@ -194,6 +199,15 @@ - (void)drawRect:(CGRect)rect
CGRect shadowRect = imageRect;
shadowRect.origin = CGPointMake((self.shadowOffset.width < 0.0f) ? -self.shadowOffset.width : 0.0f, (self.shadowOffset.height < 0.0f) ? -self.shadowOffset.height : 0.0f);
CGContextClipToMask(context, shadowRect, self.mask);

// Clip to inverted mask to prevent icon from being filled
if (self.clipsShadow)
{
if (!invertedMask)
invertedMask = CGImageCreateInvertedMaskWithMask(self.mask);
CGContextClipToMask(context, imageRect, invertedMask);
}

CGContextFillRect(context, shadowRect);
CGContextRestoreGState(context);
}
Expand All @@ -202,7 +216,7 @@ - (void)drawRect:(CGRect)rect
CGContextClipToMask(context, imageRect, self.mask);

// Fill icon
CGContextSaveGState(context);
CGContextSaveGState(context); // Save state before filling

if (self.gradient && !(self.highlighted && self.highlightedColor))
{
Expand All @@ -221,41 +235,27 @@ - (void)drawRect:(CGRect)rect

CGContextFillRect(context, imageRect);
}

CGContextRestoreGState(context);

// Perform additional drawing if specified
if (self.drawingBlock != NULL)
{
CGContextSaveGState(context);
self.drawingBlock(context);
CGContextRestoreGState(context);
}
CGContextRestoreGState(context); // Restore state after filling

// Draw inner shadow
if (!CGSizeEqualToSize(self.innerShadowOffset, CGSizeZero))
{
if (cblas_sdsdot) // Check for Accelerate framework. See TN2064: https://developer.apple.com/library/mac/#technotes/tn2064/_index.html
{
CGContextSaveGState(context);

// First invert the mask to be able to draw outside it after clipping
CGImageRef invertedImage = CGImageCreateInvertedMaskWithMask(self.mask);

// Clip to inverted mask translated by innerShadowOffset
CGAffineTransform innerShadowOffsetTransform = CGAffineTransformMakeTranslation(self.innerShadowOffset.width, -self.innerShadowOffset.height);
CGContextClipToMask(context, CGRectApplyAffineTransform(imageRect, innerShadowOffsetTransform), invertedImage);
CGImageRelease(invertedImage);

// Fill inner shadow color
[self.innerShadowColor set];
CGContextFillRect(context, imageRect);
CGContextRestoreGState(context);
} else {
NSLog(@"Inner shadows are unavailable. Include the Accelerate framework in your project.");
}
CGContextSaveGState(context);

// Clip to inverted mask to prevent main area from being filled
if (!invertedMask)
invertedMask = CGImageCreateInvertedMaskWithMask(self.mask);

// Clip to inverted mask translated by innerShadowOffset
CGAffineTransform innerShadowOffsetTransform = CGAffineTransformMakeTranslation(self.innerShadowOffset.width, -self.innerShadowOffset.height);
CGContextClipToMask(context, CGRectApplyAffineTransform(imageRect, innerShadowOffsetTransform), invertedMask);
// Fill inner shadow color
[self.innerShadowColor set];
CGContextFillRect(context, imageRect);
CGContextRestoreGState(context);
}

CGImageRelease(invertedMask); // Done with invertedMask

// Draw overlay
if (self.overlay)
{
Expand Down Expand Up @@ -455,7 +455,6 @@ - (void)setGradientStartColor:(UIColor *)gradientStartColor;
return;
}


[self willChangeValueForKey:@"gradientColors"];
_gradientColors = (_iconViewFlags.hasGradientEndColor) ? [NSArray arrayWithObjects:gradientStartColor, self.gradientEndColor, nil] : [NSArray arrayWithObject:gradientStartColor];
[self didChangeValueForKey:@"gradientColors"];
Expand Down

0 comments on commit a9af1bc

Please sign in to comment.