【发布时间】:2013-12-23 21:50:40
【问题描述】:
我通过 CoreGraphics 创建了一个菱形,用作 UIButton。我在设置它的按下状态时遇到问题,在选择时设置背景颜色会使整个框架着色,并且由于绘制了菱形按钮,因此没有图像可用于其选定状态。我怎样才能得到它,以便当按下菱形按钮时,只有 UIButton 视图中的菱形会改变颜色?
【问题讨论】:
标签: ios objective-c xcode uibutton core-graphics
我通过 CoreGraphics 创建了一个菱形,用作 UIButton。我在设置它的按下状态时遇到问题,在选择时设置背景颜色会使整个框架着色,并且由于绘制了菱形按钮,因此没有图像可用于其选定状态。我怎样才能得到它,以便当按下菱形按钮时,只有 UIButton 视图中的菱形会改变颜色?
【问题讨论】:
标签: ios objective-c xcode uibutton core-graphics
如果您有钻石的图像,为什么不制作选定状态图像并使用 [UIButton setImage: forState:]?
如果您自己完成所有绘图,则需要将绘图夹在钻石上。 ctx 是您要绘制到的 CGContext。
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef maskCtx = CGBitmapContextCreate(NULL, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(colorSpace);
UIBezierPath* path = CreateADiamondBezierPath();
CGContextSetRGBFillColor(maskCtx, 0, 0, 0, 1);
CGContextBeginPath(maskCtx);
CGContextAddPath(maskCtx, path.CGPath);
CGContextFillPath(maskCtx);
CGImageRef maskImg = CGBitmapContextCreateImage(maskCtx);
//Clip to the mask
CGContextClipToMask(ctx, scaledFrame, maskImg);
//Now fil in your ctx with the selected color
CGContextSetRGBFillColor(ctx, 1, 0, 0, 1); //this does red
CFRelease(maskImg);
CFRelease(maskCtx);
【讨论】: