Skip to content

Commit

Permalink
Updated to support outlines.
Browse files Browse the repository at this point in the history
  • Loading branch information
caylanlarson committed Feb 22, 2014
1 parent 6c3b6cb commit 042a4f7
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 20 deletions.
13 changes: 13 additions & 0 deletions AngleGradient/AngleGradientView.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,20 @@
@interface AngleGradientView : ANGLE_GRADIENT_SUPERCLASS

@property(strong, nonatomic) AngleGradientLayer *angleGradientLayer;
@property(strong, nonatomic) UIBezierPath *maskBezierPath;
@property(strong, nonatomic) CAShapeLayer *outlineLayer;
@property(strong, nonatomic) UIBezierPath *outlineBezierPath;

@property(strong, nonatomic) UIColor *borderColor;
@property(nonatomic) CGFloat borderWidth;

@property(strong, nonatomic) NSArray *colors;
@property(strong, nonatomic) NSArray *locations;

-(void)setOutlineBezierPath:(UIBezierPath *)outlineBezierPath
strokeColor:(UIColor*)strokeColor
lineWidth:(CGFloat)lineWidth;

-(void)applyRoundMask;

@end
87 changes: 87 additions & 0 deletions AngleGradient/AngleGradientView.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,16 @@
#import "AngleGradientView.h"
#import "AngleGradientLayer.h"

@interface AngleGradientView ()

@property (strong, nonatomic) CALayer *maskLayer;

@end

@implementation AngleGradientView

@synthesize outlineBezierPath = _outlineBezierPath;

-(NSArray*)colors
{
return self.angleGradientLayer.colors;
Expand Down Expand Up @@ -65,5 +73,84 @@ -(AngleGradientLayer*)angleGradientLayer
return _angleGradientLayer;
}

-(void)setMaskBezierPath:(UIBezierPath *)maskBezierPath
{
_maskBezierPath = maskBezierPath;

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = maskBezierPath.CGPath;
maskLayer.fillRule = kCAFillRuleEvenOdd;
maskLayer.fillColor = [UIColor grayColor].CGColor;
self.maskLayer = maskLayer;
}

-(void)setMaskLayer:(CALayer *)maskLayer
{
[self.maskLayer removeFromSuperlayer];
_maskLayer = maskLayer;
[self.angleGradientLayer setMask:_maskLayer];
}

-(void)setOutlineBezierPath:(UIBezierPath *)outlineBezierPath
{
[self setOutlineBezierPath:outlineBezierPath strokeColor:[UIColor blackColor] lineWidth:1];
}

-(void)setOutlineBezierPath:(UIBezierPath *)outlineBezierPath
strokeColor:(UIColor*)strokeColor
lineWidth:(CGFloat)lineWidth
{
_outlineBezierPath = outlineBezierPath;

CAShapeLayer *outlineShapeLayer = [CAShapeLayer layer];
outlineShapeLayer.path = outlineBezierPath.CGPath;
outlineShapeLayer.strokeColor = strokeColor.CGColor;
outlineShapeLayer.fillColor = [UIColor clearColor].CGColor;
outlineShapeLayer.lineWidth = lineWidth;

self.outlineLayer = outlineShapeLayer;
}

-(UIBezierPath*)outlineBezierPath
{
if(!_outlineBezierPath)
_outlineBezierPath = [self.maskBezierPath copy];

return _outlineBezierPath;
}

-(void)setOutlineLayer:(CAShapeLayer *)outlineLayer
{
[self.outlineLayer removeFromSuperlayer];
_outlineLayer = outlineLayer;
[self.layer addSublayer:_outlineLayer];
}

-(void)applyRoundMask
{
self.maskBezierPath = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
}

-(void)setBorderColor:(UIColor *)borderColor
{
_borderColor = borderColor;
if(self.maskBezierPath)
[self setOutlineBezierPath:self.outlineBezierPath
strokeColor:borderColor
lineWidth:self.outlineLayer.lineWidth];
else
self.layer.borderColor = borderColor.CGColor;
}

-(void)setBorderWidth:(CGFloat)borderWidth
{
_borderWidth = borderWidth;
if(self.maskBezierPath)
[self setOutlineBezierPath:self.outlineBezierPath
strokeColor:[UIColor colorWithCGColor:self.outlineLayer.strokeColor]
lineWidth:borderWidth];
else
self.layer.borderWidth = borderWidth;
}

@end
11 changes: 4 additions & 7 deletions AngleGradientSample/AngleGradientSample/UserControl2.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ - (id)initWithFrame:(CGRect)frame
{
if (!(self = [super initWithFrame:frame]))
return nil;

self.backgroundColor = [UIColor whiteColor];


NSMutableArray *colors = [[NSMutableArray alloc] initWithCapacity:16];

[colors addObject:(id)[UIColor colorWithWhite:0.65 alpha:1].CGColor];
Expand All @@ -31,10 +29,9 @@ - (id)initWithFrame:(CGRect)frame

self.colors = colors;

self.layer.cornerRadius = CGRectGetWidth(self.bounds) / 2;
self.clipsToBounds = YES;
self.layer.borderColor = [UIColor colorWithWhite:0.55 alpha:1].CGColor;
self.layer.borderWidth = 1;
[self applyRoundMask];
self.borderColor = [UIColor grayColor];
self.borderWidth = 1;

return self;
}
Expand Down
3 changes: 3 additions & 0 deletions AngleGradientSample/AngleGradientSample/UserControl3.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ - (id)initWithFrame:(CGRect)frame

self.transform = CGAffineTransformMakeRotation(0.75 * M_PI);

self.borderColor = [UIColor blueColor];
self.borderWidth = 3;

return self;
}

Expand Down
7 changes: 2 additions & 5 deletions AngleGradientSample/AngleGradientSample/UserControl4.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ - (id)initWithFrame:(CGRect)frame
{
if (!(self = [super initWithFrame:frame]))
return nil;

self.backgroundColor = [UIColor whiteColor];


NSMutableArray *colors = [[NSMutableArray alloc] initWithCapacity:4];

[colors addObject:(id)[UIColor colorWithRed:1 green:0 blue:0 alpha:1].CGColor];
Expand All @@ -28,8 +26,7 @@ - (id)initWithFrame:(CGRect)frame

self.colors = colors;

self.layer.cornerRadius = CGRectGetWidth(self.bounds) / 2;
self.clipsToBounds = YES;
[self applyRoundMask];
self.transform = CGAffineTransformMakeRotation(-M_PI_2);

return self;
Expand Down
7 changes: 2 additions & 5 deletions AngleGradientSample/AngleGradientSample/UserControl5.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ - (id)initWithFrame:(CGRect)frame
{
if (!(self = [super initWithFrame:frame]))
return nil;

self.backgroundColor = [UIColor whiteColor];


NSMutableArray *colors = [[NSMutableArray alloc] initWithCapacity:4];
NSMutableArray *locations = [[NSMutableArray alloc] initWithCapacity:16];

Expand Down Expand Up @@ -48,8 +46,7 @@ - (id)initWithFrame:(CGRect)frame
self.colors = colors;
self.locations = locations;

self.layer.cornerRadius = CGRectGetWidth(self.bounds) / 2;
self.clipsToBounds = YES;
[self applyRoundMask];

return self;
}
Expand Down
4 changes: 1 addition & 3 deletions AngleGradientSample/AngleGradientSample/UserControl6.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ - (id)initWithFrame:(CGRect)frame
{
if (!(self = [super initWithFrame:frame]))
return nil;

self.backgroundColor = [UIColor clearColor];


NSMutableArray *colors = [[NSMutableArray alloc] initWithCapacity:4];
NSMutableArray *locations = [[NSMutableArray alloc] initWithCapacity:16];

Expand Down

0 comments on commit 042a4f7

Please sign in to comment.