diff --git a/pxSVG/pxSVGRenderPath.m b/pxSVG/pxSVGRenderPath.m index 201685f..040e466 100644 --- a/pxSVG/pxSVGRenderPath.m +++ b/pxSVG/pxSVGRenderPath.m @@ -37,6 +37,7 @@ @interface pxSVGRenderPath () @interface pxSVGPatternLayer : CALayer @property (nonatomic) CALayer *patternLayer; @property (nonatomic) CATransform3D patternTransform; +@property UIBezierPath *clipPath; @end @implementation pxSVGPatternLayer @@ -77,6 +78,12 @@ - (void)setNeedsDisplay - (void)drawInContext:(CGContextRef)ctx { + if (self.clipPath) { + UIBezierPath *cp = [self.clipPath copy]; + [cp applyTransform:CGAffineTransformMakeTranslation(-cp.bounds.origin.x, -cp.bounds.origin.y)]; + CGContextAddPath(ctx, cp.CGPath); + CGContextClip(ctx); + } CGRect r = CGContextGetClipBoundingBox(ctx); CGAffineTransform tr = CATransform3DGetAffineTransform(self.patternTransform); CGSize sz = self.patternLayer.frame.size; @@ -101,6 +108,59 @@ - (void)drawInContext:(CGContextRef)ctx @end +@interface pxSVGGradientLayer : CALayer +@property UIBezierPath *clipPath; +@property NSArray *colors; +@property NSArray *locations; +@property CGPoint startPoint, endPoint; +@end + +@implementation pxSVGGradientLayer + ++ (instancetype)layer +{ + return [super layer]; +} + +- (instancetype)init +{ + self = [super init]; + [self setNeedsDisplayOnBoundsChange:YES]; + [self setNeedsDisplay]; + return self; +} + +- (void)setNeedsDisplay +{ + if ([NSThread isMainThread]) return [super setNeedsDisplay]; + __weak id weakself = self; + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + if (weakself) [weakself setNeedsDisplay]; + }]; +} + +- (void)drawInContext:(CGContextRef)ctx +{ + CGContextSaveGState(ctx); + CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB(); + CGFloat locs[self.locations.count]; + for (NSUInteger i=0; i