Skip to content

Instantly share code, notes, and snippets.

@cook
Last active December 13, 2015 20:29
Show Gist options
  • Save cook/4970737 to your computer and use it in GitHub Desktop.
Save cook/4970737 to your computer and use it in GitHub Desktop.
利用transform的帮助,使绘制过程和坐标相互独立。
void drawStripes(void *info, CGContextRef con)
{
// assume 4x4 cell
CGContextSetFillColorWithColor(con, [[UIColor redColor] CGColor]);
CGContextFillRect(con, CGRectMake(0, 2, 4, 2));
CGContextSetFillColorWithColor(con, [[UIColor blueColor] CGColor]);
CGContextFillRect(con, CGRectMake(0, 0, 4, 2));
}
void drawArrow(CGContextRef con)
{
// 这句话避免去改变下面的坐标就可以在(0,0)开始绘制了。
CGContextTranslateCTM(con, -80, 0);
// assume the background color is white
CGContextSaveGState(con);
// punch triangle hole in context clipping region
CGContextMoveToPoint(con, 90, 100);
CGContextAddLineToPoint(con, 100, 90);
CGContextAddLineToPoint(con, 110, 100);
CGContextClosePath(con);
CGContextAddRect(con, CGContextGetClipBoundingBox(con));
CGContextEOClip(con);
// draw the vertical line, add its shape to the clipping region
CGContextMoveToPoint(con, 100, 100);
CGContextAddLineToPoint(con, 100, 19);
CGContextSetLineWidth(con, 20);
CGContextReplacePathWithStrokedPath(con);
CGContextClip(con);
// draw the gradient
CGFloat locs[3] = {0., .5, 1.};
CGFloat colors[12] = {
.3, .3, .3, .8, // starting color
.0,.0,.0,1.0, // intermediate color
.3, .3, .3, .8 // ending color
};
CGColorSpaceRef sp = CGColorSpaceCreateDeviceGray();
CGGradientRef grad = CGGradientCreateWithColorComponents(sp, colors, locs, 3);
CGContextDrawLinearGradient(con, grad, CGPointMake(89, 0), CGPointMake(111, 0), 0);
CGColorSpaceRelease(sp);
CGGradientRelease(grad);
CGContextRestoreGState(con);
// draw the triangle with pattern, the point of the arrow
CGColorSpaceRef sp2 = CGColorSpaceCreatePattern(NULL);
CGContextSetFillColorSpace(con, sp2);
CGColorSpaceRelease(sp2);
CGPatternCallbacks callback = {
0, &drawStripes, NULL
};
CGAffineTransform tr = CGAffineTransformIdentity;
CGPatternRef patt = CGPatternCreate(NULL, CGRectMake(0, 0, 4, 4), tr, 4, 4, kCGPatternTilingConstantSpacingMinimalDistortion, true, &callback);
CGFloat alph = 1.0;
CGContextSetFillPattern(con, patt, &alph);
CGPatternRelease(patt);
CGContextMoveToPoint(con, 80, 25);
CGContextAddLineToPoint(con, 100, 0);
CGContextAddLineToPoint(con, 120, 25);
CGContextFillPath(con);
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(40, 100), NO, 0.0);
CGContextRef con = UIGraphicsGetCurrentContext();
drawArrow(con);
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
con = UIGraphicsGetCurrentContext();
[im drawAtPoint:CGPointMake(0, 0)];
for (int i = 0; i < 3; i++) {
CGContextTranslateCTM(con, 20, 100);
CGContextRotateCTM(con, 30 * M_PI/180.);
CGContextTranslateCTM(con, -20, -100);
[im drawAtPoint:CGPointMake(0, 0)];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment