绘制光泽
void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor)
{
CGContextSaveGState(context);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSArray *array = @[(__bridge id)startColor, (__bridge id)endColor];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)array, (CGFloat[]){0.0, 1.0});
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
CGContextAddRect(context, rect);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGColorSpaceRelease(colorSpace);
CGGradientRelease(gradient);
CGContextRestoreGState(context);
}void drawGlossAndLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor)
{
drawLinearGradient(context, rect, startColor, endColor);
UIColor *dark = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.1];
UIColor *light = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.35];
CGRect halfRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height/2); //光泽在上半部分
drawLinearGradient(context, halfRect, light.CGColor, dark.CGColor);
}
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect paintingRect = CGRectMake(5, 5, self.bounds.size.width - 10, self.bounds.size.height - 10);
UIColor *lightBlue = [UIColor colorWithRed:105/255.0 green:179/255.0 blue:216/255.0 alpha:1.0];
UIColor *darkBlue = [UIColor colorWithRed:21/255.0 green:92/255.0 blue:136/255.0 alpha:1.0];
CGContextSaveGState(context);
CGContextAddRect(context, paintingRect);
CGContextSetFillColorWithColor(context, lightBlue.CGColor);
CGContextSetShadowWithColor(context, CGSizeMake(0, 2), 3.0, [[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0] CGColor]);
CGContextFillPath(context);
CGContextStrokePath(context);
CGContextRestoreGState(context);
drawGlossAndLinearGradient(context, paintingRect, lightBlue.CGColor, darkBlue.CGColor);效果如下
仿射变换
- (void)rectTranslate
{
CGMutablePathRef path = CGPathCreateMutable();
CGAffineTransform transform = CGAffineTransformMakeTranslation(100, 10);
CGPathAddRect(path, &transform, CGRectMake(10, 10, 200, 300));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextAddPath(context, path);
[[UIColor colorWithRed:.2f green:.6f blue:.8f alpha:1.0f] setFill];
[[UIColor brownColor] setStroke];
CGContextSetLineWidth(context, 5.0f);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextRestoreGState(context);
CGPathRelease(path);
}这里平移的参数是x轴正向移动100,y轴正向移动10。然后将地址&transform传入到CGPathAddRect函数中。
矩阵变换
- (void)rectScale
{
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(10, 10, 200, 300));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextScaleCTM(context, .5f, .5f); //需要在加路径之前使用,上下文设置的所有内容都进行缩放
CGContextAddPath(context, path);
CGContextSetLineWidth(context, 5.0f);
[[UIColor colorWithRed:.2f green:.6f blue:.8f alpha:1.0f] setFill];
[[UIColor brownColor] setStroke];
CGContextDrawPath(context, kCGPathFillStroke);
CGContextRestoreGState(context);
CGPathRelease(path);
}这个方法中,设置了一个缩放矩阵,之后绘制的矩形则变成了原来尺寸的一半。
3983



被折叠的 条评论
为什么被折叠?



