开发者

Darkening a UIView

I'd like to "darken" a UIView by a certain amount. Right now, I'm do开发者_如何学Cing the darkening like this:

UIView *overlay = [[UIView alloc] initWithFrame:mainView.bounds];
overlay.backgroundColor = [UIColor blackColor];
overlay.alpha = 0.5f;
[mainView addSubview:overlay];

Unfortunately, this also adds a semi-transparent black overlay to parts of mainView that are transparent. Is there any way to only darken the non-transparent sections?


Here is something to try. I have never used UIRectFillUsingBlendMode.

-(void) drawRect:(CGRect)inDirty {
  [[UIColor colorWithWhite:0.0 alpha:0.5] setFill];
  UIRectFillUsingBlendMode( inDirty , kCGBlendModeDarken );
}

The view implementing this would have to be set up to composite with only the other views you want to affect. That probably means one parent view that contains this view and all the other views you want to darken.

You can also look into CGContextSetBlendMode.


Try using something like this (adjust the colors, obviously)?

overlay.backgroundColor = [UIColor colorWithRed:0.317647 green:0.317647 blue:0.317647 alpha:0.85];

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜