我曾长时间寻找答案,也试图自己解决问题,但无济于事。我认为这里是提问的最佳地点。我试图实现简单的功能,但却找不到方法。我觉得我在概念层面上遗漏了一些东西。以下是我尝试在子类化UIView的class中创建半透明矩形的方式:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
//this should be white color with 0.7 opacity right
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.7);
CGContextFillRect(context, self.bounds);
}
好的,这只是使颜色变成灰色。我也尝试了以下方法,但结果相同: CGContextSetRGBFillColor(context,1.0,1.0,1.0,1.0); CGContextSetAlpha(context,0.7);
另一种尝试:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(context, self.bounds);
self.alpha = 0.7;
}
看,我解决了。但并不完全是这样。我想在上下文中画一个半透明的矩形,而不是使整个视图透明。然后,例如添加另一个完全不透明的矩形。很想听听你的想法。谢谢。
opaque
属性被设置为YES
,这会忽略透明度。将背景颜色设置为带有透明度的颜色会隐式地将该值设置为NO
并允许透明度。 - borrrden