iphone - 使用多个 subview 使交叉点透明

标签 iphone objective-c ios transparent

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

请引用以下代码。让我知道如何使用特定框架使按钮透明,或者让我知道您是否有其他选择。

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];

而不是这个 gapButton 我需要灰色的透明部分,以便用户可以在该部分看到黑色。

enter image description here

最佳答案

试试这个,我在评论中建议。其他答案需要子类化 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 文件来表示您可能正在寻找的那种 backgroundImage。请注意,中心是透明的,而不是白色的。因此,它应该显示它背后的任何图像:

button with hole

关于iphone - 使用多个 subview 使交叉点透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8642049/

相关文章:

iphone - UITableView - 加载数据时隐藏所有组/单元格

iphone - iOS 中的多个对象管理器

ios - 在 Xcode 8.1 中创建 NSManagedObject 类显示错误

ios - 如何在 iOS 中推送整个 View ,包括导航栏和工具栏

android - SQLite 在 Android 和 iOS 上的性能差异

iphone - 误报 respondsToSelector with UIApplicationDelegate 导致 NSInvalidArgumentException

iphone - UINavigationController 添加 UIView

ios - 方法 Swizzling 未正确触发?

iphone - 将 SoundTouch 添加到 Xcode/iPhone

ios - 标记曲线段