使用多个子视图使交叉点透明

发布于 2024-12-23 03:18:38 字数 1081 浏览 0 评论 0原文

请检查下图。我添加了“黑色”颜色的滚动视图,并添加了“灰色”颜色的子视图。现在我想让子视图透明,定义为“白色”颜色。

请参考下面的代码。让我知道如何使按钮在特定框架下透明,或者让我知道您是否有其他选择。

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 40.0, self.frame.size.width, 300.0)];
self.scrollView.contentSize = CGSizeMake(self.bounds.size.width,ViewHeight);
self.scrollView.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.scrollView.delegate = self;
self.scrollView.backgroundColor =[UIColor blackColor];
[self.view addSubview:self.scrollView


UIButton *butApp = [UIButton buttonWithType:UIButtonTypeCustom];
[butApp setFrame:CGRectMake(x, y , w, h)];
[butApp setBackgroundColor:[UIColor greyColor]];
[self.scrollView addSubview:butApp];


UIButton* gapButton = [UIButton buttonWithType:UIButtonTypeCustom];
[gapButton setFrame:CGRectMake(x+3, y+10, w-6, 10)];
[gapButton setBackgroundColor:[UIColor whiteColor]];
[self.scrollView addSubview:gapButton];

我需要灰色的透明部分,而不是这个间隙按钮,这样用户就可以看到该部分的黑色。

在此处输入图像描述

Please check below image. i have added scrollview in "BLACK" color and added subview in "GREY" color. now i want to make subview transparent which is define as "WHITE" color.

Please refer the below code. Let me know how to make button transparent with particular frame or let me know if you have any alternative for that.

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 40.0, self.frame.size.width, 300.0)];
self.scrollView.contentSize = CGSizeMake(self.bounds.size.width,ViewHeight);
self.scrollView.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.scrollView.delegate = self;
self.scrollView.backgroundColor =[UIColor blackColor];
[self.view addSubview:self.scrollView


UIButton *butApp = [UIButton buttonWithType:UIButtonTypeCustom];
[butApp setFrame:CGRectMake(x, y , w, h)];
[butApp setBackgroundColor:[UIColor greyColor]];
[self.scrollView addSubview:butApp];


UIButton* gapButton = [UIButton buttonWithType:UIButtonTypeCustom];
[gapButton setFrame:CGRectMake(x+3, y+10, w-6, 10)];
[gapButton setBackgroundColor:[UIColor whiteColor]];
[self.scrollView addSubview:gapButton];

Instead of this gapButton i need transparent portion in grey color so user can see black color in that portion.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故人的歌 2024-12-30 03:18:38

试试这个,我在评论中建议的。其他答案需要子类化 UIButton,您认为这在您的情况下并不理想。

UIButton *butApp = [UIButton buttonWithType:UIButtonTypeCustom];
[butApp setFrame:CGRectMake(x, y , w, h)];
//[butApp setBackgroundColor:[UIColor greyColor]]; //replace this...
[butApp setBackgroundImage:[UIImage imageNamed:@"greyWithHoleInCenter.png"]
                   forState:UIControlStateNormal]; //...with this
[self.scrollView addSubview:butApp];

我创建了一个原始的 .png 文件来表示您可能正在寻找的背景图像类型。请注意,中心是透明的,而不是白色的。因此,它应该显示其后面的任何图像:

button withhole

Try this, I suggested it in comments. Other answer requires subclassing UIButton, which you suggested was not ideal in your situation.

UIButton *butApp = [UIButton buttonWithType:UIButtonTypeCustom];
[butApp setFrame:CGRectMake(x, y , w, h)];
//[butApp setBackgroundColor:[UIColor greyColor]]; //replace this...
[butApp setBackgroundImage:[UIImage imageNamed:@"greyWithHoleInCenter.png"]
                   forState:UIControlStateNormal]; //...with this
[self.scrollView addSubview:butApp];

I created a crude .png file to represent the kind of backgroundImage you might be looking for. Note that the center is clear, not white. Therefore, it should show whatever image is behind it:

button with hole

你如我软肋 2024-12-30 03:18:38

好的。有趣的问题。在我看来,你必须通过子类化 UIView 类来重写 drawRect: + drawInContext: 方法。
您还需要将容器视图(灰色视图)+按钮 bg 设置为clearColor

-(void)drawRect:(CGRect)r
{
 // Let this method blank will cause drawLayer:InContext to be called
}

-(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
{
       // Fill the bg with gray 
    CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
    CGContextFillRect(context, self.bounds);

      // I clear the subview frames
    for (UIView * v in [self subviews]) // I do it for all subviews
    {
        CGContextSetBlendMode(context, kCGBlendModeClear);
        CGContextFillRect(context, v.frame);
    }
}

here is a screen shot

Ok. Funny question. In my opinion you have to override drawRect: + drawInContext: methods by subclassing UIView class.
You also need to set the container view (the gray view) + button bg to clearColor

-(void)drawRect:(CGRect)r
{
 // Let this method blank will cause drawLayer:InContext to be called
}

-(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
{
       // Fill the bg with gray 
    CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
    CGContextFillRect(context, self.bounds);

      // I clear the subview frames
    for (UIView * v in [self subviews]) // I do it for all subviews
    {
        CGContextSetBlendMode(context, kCGBlendModeClear);
        CGContextFillRect(context, v.frame);
    }
}

here is a screen shot

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文