Gre*_*mbs 8 cocoa-touch gradient core-graphics quartz-graphics
把我的脚浸入更多的核心图形绘图,我试图重新创建一个邪恶的金属旋钮,我已经登陆了可能是一个显示停止的问题.
似乎没有任何方法可以在Core Graphics中绘制角度渐变.我看到有CGContextDrawRadialGradient()和CGContextDrawLinearGradient(),但没有什么,我看,让我画的角度梯度.有没有人知道一个变通方法,或一些隐藏在某处的框架来实现这一点,而无需将旋钮预先渲染到图像文件中?
AngleGradientKnob http://dl.dropbox.com/u/3009808/AngleGradient.png.
Rob*_*ier 11
这是一种抛在一起的方式,但这是我可能采取的方法.这是通过使用一些简单的trig将其直接绘制到位图中,然后将其剪切为圆形来创建角度渐变.我使用灰度颜色空间创建一个内存网格,计算从给定点到中心的角度,然后根据周期函数着色,运行在0到255之间.你当然可以扩展它以做RGBA颜色.
当然你会缓存它并使用数学来获得你想要的颜色.目前从黑色到白色一直在运行,这看起来并不像你想的那么好.

- (void)drawRect:(CGRect)rect {
CGImageAlphaInfo alphaInfo = kCGImageAlphaNone;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
size_t components = CGColorSpaceGetNumberOfComponents( colorSpace );
size_t width = 100;
size_t height = 100;
size_t bitsPerComponent = 8;
size_t bytesPerComponent = bitsPerComponent / 8;
size_t bytesPerRow = width * bytesPerComponent * components;
size_t dataLength = bytesPerRow * height;
uint8_t data[dataLength];
CGContextRef imageCtx = CGBitmapContextCreate( &data, width, height, bitsPerComponent,
bytesPerRow, colorSpace, alphaInfo );
NSUInteger offset = 0;
for (NSUInteger y = 0; y < height; ++y) {
for (NSUInteger x = 0; x < bytesPerRow; x += components) {
CGFloat opposite = y - height/2.;
CGFloat adjacent = x - width/2.;
if (adjacent == 0) adjacent = 0.001;
CGFloat angle = atan(opposite/adjacent);
data[offset] = abs((cos(angle * 2) * 255));
offset += components * bytesPerComponent;
}
}
CGImageRef image = CGBitmapContextCreateImage(imageCtx);
CGContextRelease(imageCtx);
CGColorSpaceRelease(colorSpace);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect buttonRect = CGRectMake(100, 100, width, width);
CGContextAddEllipseInRect(ctx, buttonRect);
CGContextClip(ctx);
CGContextDrawImage(ctx, buttonRect, image);
CGImageRelease(image);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2262 次 |
| 最近记录: |