Last active
December 16, 2015 17:10
-
-
Save lonelytango/5468827 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CGRect rectFor1PxStroke(CGRect rect) { | |
return CGRectMake(rect.origin.x + 0.5, rect.origin.y + 0.5, rect.size.width - 1, rect.size.height - 1); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor) | |
{ | |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | |
CGFloat locations[] = { 0.0, 1.0 }; | |
NSArray *colors = @[(__bridge id)startColor, (__bridge id)endColor]; | |
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations); | |
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect)); | |
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect)); | |
CGContextSaveGState(context); | |
CGContextAddRect(context, rect); | |
CGContextClip(context); | |
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsBeforeStartLocation); | |
CGContextRestoreGState(context); | |
CGGradientRelease(gradient); | |
CGColorSpaceRelease(colorSpace); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct CGPatternCallbacks { | |
unsigned int version; | |
CGPatternDrawPatternCallback drawPattern; | |
CGPatternReleaseInfoCallback releaseInfo; | |
}; | |
typedef struct CGPatternCallbacks CGPatternCallbacks; | |
- (void)drawRect:(CGRect)rect | |
{ | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
UIColor * bgColor = [UIColor colorWithHue:0 saturation:0 brightness:0.15 alpha:1.0]; | |
CGContextSetFillColorWithColor(context, bgColor.CGColor); | |
CGContextFillRect(context, rect); | |
static const CGPatternCallbacks callbacks = { 0, &MyDrawColoredPattern, NULL }; | |
CGContextSaveGState(context); | |
CGColorSpaceRef patternSpace = CGColorSpaceCreatePattern(NULL); | |
CGContextSetFillColorSpace(context, patternSpace); | |
CGColorSpaceRelease(patternSpace); | |
CGPatternRef pattern = CGPatternCreate(NULL, | |
rect, | |
CGAffineTransformIdentity, | |
24, | |
24, | |
kCGPatternTilingConstantSpacing, | |
true, | |
&callbacks); | |
CGFloat alpha = 1.0; | |
CGContextSetFillPattern(context, pattern, &alpha); | |
CGPatternRelease(pattern); | |
CGContextFillRect(context, self.bounds); | |
CGContextRestoreGState(context); | |
} | |
void MyDrawColoredPattern (void *info, CGContextRef context) | |
{ | |
UIColor * dotColor = [UIColor colorWithHue:0 saturation:0 brightness:0.07 alpha:1.0]; | |
UIColor * shadowColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.1]; | |
CGContextSetFillColorWithColor(context, dotColor.CGColor); | |
CGContextSetShadowWithColor(context, CGSizeMake(0, 1), 1, shadowColor.CGColor); | |
CGContextAddArc(context, 3, 3, 4, 0, radians(360), 0); | |
CGContextFillPath(context); | |
CGContextAddArc(context, 16, 16, 4, 0, radians(360), 0); | |
CGContextFillPath(context); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void draw1PxStroke(CGContextRef context, CGPoint startPoint, CGPoint endPoint, CGColorRef color) { | |
CGContextSaveGState(context); | |
CGContextSetLineCap(context, kCGLineCapSquare); | |
CGContextSetStrokeColorWithColor(context, color); | |
CGContextSetLineWidth(context, 1.0); | |
CGContextMoveToPoint(context, startPoint.x + 0.5, startPoint.y + 0.5); | |
CGContextAddLineToPoint(context, endPoint.x + 0.5, endPoint.y + 0.5); | |
CGContextStrokePath(context); | |
CGContextRestoreGState(context); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment