我已经通过CoreGraphics创建了一个菱形形状,用作UIButton。我在设置它的按下状态时遇到了问题,当选择时设置背景颜色会使整个框架着色,而且由于菱形按钮是绘制的,所以没有图像可用于它的选择状态。我怎样才能让它在按下菱形按钮时,只有UIButton视图中的钻石会改变颜色?
发布于 2013-12-24 06:31:09
如果您有一个用于菱形的图像,为什么不创建一个选定的状态图像并使用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);https://stackoverflow.com/questions/20751700
复制相似问题