Skip to content

Commit

Permalink
Draw line with gradient
Browse files Browse the repository at this point in the history
  • Loading branch information
kattrali committed Nov 14, 2014
1 parent cf9c786 commit aaecf97
Showing 1 changed file with 50 additions and 42 deletions.
92 changes: 50 additions & 42 deletions Classes/BEMLine.m
Original file line number Diff line number Diff line change
Expand Up @@ -205,51 +205,38 @@ - (void)drawRect:(CGRect)rect {
// ---------------------------//
// ----- Animate Drawing -----//
// ---------------------------//
if (self.animationTime == 0) {
if (self.enableRefrenceLines == YES) {
[referenceLinesPath setLineWidth:self.lineWidth/2];

if (self.refrenceLineColor) {
[self.refrenceLineColor set];
} else {
[self.color set];
}

[referenceLinesPath strokeWithBlendMode:kCGBlendModeNormal alpha:self.lineAlpha/2];
if (self.enableRefrenceLines == YES) {
CAShapeLayer *referenceLinesPathLayer = [CAShapeLayer layer];
referenceLinesPathLayer.frame = self.bounds;
referenceLinesPathLayer.path = referenceLinesPath.CGPath;
referenceLinesPathLayer.opacity = self.lineAlpha/2;
referenceLinesPathLayer.fillColor = nil;
referenceLinesPathLayer.lineWidth = self.lineWidth/2;

if (self.refrenceLineColor) {
referenceLinesPathLayer.strokeColor = self.refrenceLineColor.CGColor;
} else {
referenceLinesPathLayer.strokeColor = self.color.CGColor;
}

[self.color set];
[line setLineWidth:self.lineWidth];
[line strokeWithBlendMode:kCGBlendModeNormal alpha:self.lineAlpha];

} else {
if (self.enableRefrenceLines == YES) {
CAShapeLayer *referenceLinesPathLayer = [CAShapeLayer layer];
referenceLinesPathLayer.frame = self.bounds;
referenceLinesPathLayer.path = referenceLinesPath.CGPath;
referenceLinesPathLayer.opacity = self.lineAlpha/2;
referenceLinesPathLayer.fillColor = nil;
referenceLinesPathLayer.lineWidth = self.lineWidth/2;

if (self.refrenceLineColor) {
referenceLinesPathLayer.strokeColor = self.refrenceLineColor.CGColor;
} else {
referenceLinesPathLayer.strokeColor = self.color.CGColor;
}


if (self.animationTime > 0)
[self animateForLayer:referenceLinesPathLayer withAnimationType:self.animationType isAnimatingReferenceLine:YES];
[self.layer addSublayer:referenceLinesPathLayer];
}

CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.frame = self.bounds;
pathLayer.path = line.CGPath;
pathLayer.strokeColor = self.color.CGColor;
pathLayer.fillColor = nil;
pathLayer.lineWidth = self.lineWidth;
pathLayer.lineJoin = kCALineJoinBevel;
pathLayer.lineCap = kCALineCapRound;
[self.layer addSublayer:referenceLinesPathLayer];
}

CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.frame = self.bounds;
pathLayer.path = line.CGPath;
pathLayer.strokeColor = self.color.CGColor;
pathLayer.fillColor = nil;
pathLayer.lineWidth = self.lineWidth;
pathLayer.lineJoin = kCALineJoinBevel;
pathLayer.lineCap = kCALineCapRound;
if (self.animationTime > 0)
[self animateForLayer:pathLayer withAnimationType:self.animationType isAnimatingReferenceLine:NO];
if (self.lineGradient) {
[self.layer addSublayer:[self backgroundGradientLayerForLayer:pathLayer]];
} else {
[self.layer addSublayer:pathLayer];
}
}
Expand All @@ -276,4 +263,25 @@ - (void)animateForLayer:(CAShapeLayer *)shapeLayer withAnimationType:(BEMLineAni
}
}

- (CALayer*)backgroundGradientLayerForLayer:(CAShapeLayer *)shapeLayer {
UIGraphicsBeginImageContext(self.bounds.size);
CGContextRef imageCtx = UIGraphicsGetCurrentContext();
CGPoint start, end;
if (self.lineGradientDirection == BEMLineGradientDirectionHorizontal) {
start = CGPointMake(0, CGRectGetMidY(shapeLayer.bounds));
end = CGPointMake(CGRectGetMaxX(shapeLayer.bounds), CGRectGetMidY(shapeLayer.bounds));
} else {
start = CGPointMake(CGRectGetMidX(shapeLayer.bounds), 0);
end = CGPointMake(CGRectGetMidX(shapeLayer.bounds), CGRectGetMaxY(shapeLayer.bounds));
}
CGContextDrawLinearGradient(imageCtx, self.lineGradient, start, end, 0);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CALayer *gradientLayer = [CALayer layer];
gradientLayer.frame = self.bounds;
gradientLayer.contents = (id)image.CGImage;
gradientLayer.mask = shapeLayer;
return gradientLayer;
}

@end

0 comments on commit aaecf97

Please sign in to comment.