【发布时间】:2014-03-10 04:13:31
【问题描述】:
我一直在尝试使用 CGContextDrawLinearGradient,但我对起点和终点的含义感到非常困惑?我认为它们的意思是当前 CGContext 上的坐标,所以如果我将起点定义为 0,0 并将终点定义为 100,100,我会得到一个带有渐变的正方形。我得到了完全无法连接到我的坐标的其他东西。
这是我的代码:
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef current_context = UIGraphicsGetCurrentContext();
CGContextSaveGState(current_context);
// Gradient
CGFloat locations[3] = {0.0, 0.5, 1.0};
CGFloat components[12] = {1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0};
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, components, locations, 3);
CGPoint startPoint = CGPointMake(0, 0);
CGPoint endPoint = CGPointMake(40, 40);
CGContextDrawLinearGradient(current_context, gradient, startPoint, endPoint, 0);
// Shadow
CGContextSetShadow(current_context, CGSizeMake(4,7), 1.0);
// Image
UIImage *logoImage = [UIImage imageNamed:@"logo.png"];
[logoImage drawInRect:bounds];
CGContextRestoreGState(current_context);
}
提前感谢您的帮助..
【问题讨论】:
-
我不希望你得到一个正方形,但我希望你得到渐变。你在看什么你不明白?你能发布一张你得到的和你预期的对比的图片吗?
标签: objective-c cgcontext cgcontextref