- (voID)drawRect:(CGRect)rect{ CGContextRef context = UIGraphicsGetCurrentContext(); //this should be white color with 0.7 opacity right CGContextSetRGBFillcolor(context,1.0,0.7); CGContextFillRect(context,self.bounds);}
好的,这只会使颜色变灰.我也试过这个,但结果相同:
CGContextSetRGBFillcolor(context,1.0);
CGContextSetAlpha(context,0.7);
另一个尝试:
- (voID)drawRect:(CGRect)rect{ CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetRGBFillcolor(context,1.0); CGContextFillRect(context,self.bounds); self.Alpha = 0.7;}
瞧!我解决了但不完全是.我想在上下文中绘制一个半透明的矩形,而不是让整个视图透明.然后例如添加另一个完全不透明的矩形.很想听听你的想法.谢谢.
解决方法 我猜你无法看到rect的透明度的原因是你的视图中存在背景颜色.你会尝试像这样设置视图背景颜色:
self.backgroundcolor = [UIcolor clearcolor];
顺便说一句,这个语句以及设置自定义视图属性的任何语句(例如,self.Alpha = 0.7;)都应该进入init方法.
编辑:
正如@borrrden所指出的那样,为了制造这个技巧而设置的不动产可能是不透明的.根据Apple Docs,这通常设置为YES,因此意味着以下内容:
An opaque vIEw is expected to fill its bounds with entirely opaque content—that is,the content should have an Alpha value of 1.0. If the vIEw is opaque and either does not fill its bounds or contains wholly or partially transparent content,the results are unpredictable. You should always set the value of this property to NO if the vIEw is fully or partially transparent.
将背景颜色设置为clearcolor可能会将此属性的值设置为NO作为附带效果.
编辑:
我不明白为什么你在自定义UIVIEw中覆盖drawRect来绘制一个半透明的矩形,可能是因为我不知道你想要做什么.在任何情况下,您可能想要研究的另一种方法是使用嵌套的UIVIEws或嵌套的CALayers,在其中为其中任何一个分配不同的Alpha / opacity.
如果您只对具有不同程度不透明度的矩形感兴趣,那么就像在父视图中嵌套UIVIEws一样简单,并为它们分配半透明背景颜色.
总结以上是内存溢出为你收集整理的在iOS中绘制半透明矩形全部内容,希望文章能够帮你解决在iOS中绘制半透明矩形所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)