Guest User

Untitled

a guest
Jul 16th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. void CGContextFillPathWithLinearGradientVertical(CGContextRef context, CGPathRef pathRef, CGColorRef startColor, CGColorRef endColor)
  2. {
  3. CGContextSaveGState(context);
  4.  
  5. enum { kNumLocations = 2 };
  6. CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
  7. CGFloat locations[kNumLocations] = { 0.0f, 1.0f };
  8.  
  9. CGRect boundingBox = CGPathGetBoundingBox(pathRef);
  10. CGPoint pointTop = CGPointMake(CGRectGetMidX(boundingBox), CGRectGetMinY(boundingBox));
  11. CGPoint pointBottom = CGPointMake(CGRectGetMidX(boundingBox), CGRectGetMaxY(boundingBox));
  12.  
  13. CGFloat r1, g1, b1, a1, r2, g2, b2, a2;
  14. CGColorToRGBA(startColor, &r1, &g1, &b1, &a1);
  15. CGColorToRGBA(endColor, &r2, &g2, &b2, &a2);
  16. ZRGBA components[kNumLocations] = { { r1, g1, b1, a1 }, { r2, g2, b2, a2 } };
  17.  
  18. CGContextAddPath(context, pathRef);
  19. CGContextClip(context);
  20. CGGradientRef gradientRef = CGGradientCreateWithColorComponents(colorSpaceRef, (CGFloat *)components, locations, kNumLocations);
  21. CGContextDrawLinearGradient(context, gradientRef, pointTop, pointBottom, kCGGradientDrawsBeforeStartLocation);
  22. CGGradientRelease(gradientRef);
  23. CGColorSpaceRelease(colorSpaceRef);
  24.  
  25. CGContextRestoreGState(context);
  26. }
Add Comment
Please, Sign In to add comment