iPhone:无法控制绘制渐变的宽度?
我需要绘制一个填充渐变的框。我正在使用 UIView 并覆盖 -drawRect 方法。
这是我的代码(简化):
CGContextRef c = UIGraphicsGetCurrentContext();
CGFloat components[8] = {158.0/255.0,36.0/255.0,134.0/255.0,1.0,115.0/255.0,26.0/255.0,93.0/255.0,1.0};
CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[2] = {0.0,1.0};
CGGradientRef glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, 2);
CGPoint topCenter = CGPointMake(10, 0);
CGPoint bottomCenter = CGPointMake(10, 10);
CGContextDrawLinearGradient(c, glossGradient, topCenter, bottomCenter, 0);
目前渐变填充填充视图的整个宽度!我该如何控制正在绘制的渐变填充框的宽度?
I need to draw a box filled with a gradient. I'm using a UIView and overwriting the -drawRect method.
Here is my code (simplified):
CGContextRef c = UIGraphicsGetCurrentContext();
CGFloat components[8] = {158.0/255.0,36.0/255.0,134.0/255.0,1.0,115.0/255.0,26.0/255.0,93.0/255.0,1.0};
CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[2] = {0.0,1.0};
CGGradientRef glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, 2);
CGPoint topCenter = CGPointMake(10, 0);
CGPoint bottomCenter = CGPointMake(10, 10);
CGContextDrawLinearGradient(c, glossGradient, topCenter, bottomCenter, 0);
The gradient fill at present fills the entire width of the view! What can I do to control the width of the gradient-filled box I'm drawing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系..找到答案了。我需要剪辑 CGContextRef 以匹配我想要的 CGRect:
Nevermind.. Found the answer. I need to clip my CGContextRef to match the CGRect I wanted: