简体   繁体   English

设置部分UIView的alpha

[英]Set alpha of a portion of a UIView

I need to make a rectangular portion of my view blacked out when the user taps the SearchBar. 当用户点击SearchBar时,我需要使视图的矩形部分变黑。 I cant figure out a way to do that. 我想不通一种方法。 I tried CGRectMake but how do i set alpha of that rect?? 我尝试了CGRectMake,但如何设置该矩形的Alpha?

为什么不添加另一个UIView并将其添加到某个事件的UIView上?

It can be done by adding sublayers to the layer of your view in the following way 可以通过以下方式将子层添加到视图层中来完成

UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(50.0, 0.0, 100.0, 100.0)] autorelease]; UIView * view = [[[[UIView alloc] initWithFrame:CGRectMake(50.0,0.0,100.0,100.0)]自动释放]; view.backgroundColor = [UIColor grayColor]; view.backgroundColor = [UIColor grayColor]; [self.view addSubview:view]; [self.view addSubview:view];

CALayer *layerUP = [CALayer layer];
[layerUP setFrame:CGRectMake(0.0, 0.0, 100.0, 50.0)];
[layerUP setBackgroundColor:[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0].CGColor];
[view.layer addSublayer:layerUP];

CALayer *layerDown = [CALayer layer];
[layerDown setFrame:CGRectMake(0.0, 50.0, 100.0, 50.0)];
[layerDown setBackgroundColor:[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.2].CGColor];
[view.layer addSublayer:layerDown];

Add an UIImageView with a black background: 添加黑色背景的UIImageView:

    UIImageView *myView= [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    myView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:myView];
    [myView release];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM