gpt4 book ai didi

iphone - tableView 作为半透明 View 中的 subview

转载 作者:行者123 更新时间:2023-12-03 21:15:51 26 4
gpt4 key购买 nike

我正在开发一个小的 iOS 组件,并且我遇到了带有 subview 的半透明 View 的问题。这是我的场景:
- 使用 [UIColor colorWithRed:green:blue:alpha]
具有半透明背景的一个 View - 一个 UITableViewalpha = 1.0,作为 subview 添加到半透明 View
- 一些其他 subview

一切正常,但当 UITableView 向上或向下滚动时,问题就会出现,事实上 UITableView 周围的半透明 View 区域会失去透明度,变得透明比其原始背景颜色更深。

这是解释问题的图片:

查看带有两个箭头的空间...

谁能帮我解决这个问题吗?
非常感谢您的关注!

更新:
一些代码:

_alertBg = [[UIView alloc] initWithFrame:CGRectZero];
_alertBg.backgroundColor = self.backgroundColor;
_alertBg.frame = CGRectMake((_bgView.frame.size.width - 240) / 2, (_bgView.frame.size.height - 260) / 2, 240, 260);
_alertBg.layer.cornerRadius = 8.0;
_alertBg.layer.borderWidth = 2.0;
_alertBg.layer.borderColor = self.borderColor.CGColor;
_alertBg.layer.shadowColor = [UIColor grayColor].CGColor;
_alertBg.layer.shadowOffset = CGSizeMake(0, 3);
_alertBg.layer.shadowOpacity = 0.8;
_alertBg.layer.masksToBounds = YES;
[_bgView addSubview:_alertBg];

_table = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_table.frame = CGRectMake(10, _titleLabel.frame.origin.y + _titleLabel.frame.size.height + 12, _alertBg.frame.size.width - 20, 150);
_table.layer.cornerRadius = 6.0;
_table.layer.masksToBounds = YES;
_table.delegate = self;
_table.dataSource = self;
[_alertBg addSubview:_table];

从上面的代码中,self.backgroundColor类似于[UIColor colorWithRed:0 green:0 blue:1 alpha:0.7]

最佳答案

我将可用的代码放入测试项目中,并遇到了与您相同的问题。注释掉 _alertBg.layer.shadowOpacity = 0.5; 为我解决了这个问题。也许有人可以澄清为什么这是一个问题,我对 QuartzCore 的经验有限。

编辑:好的,更接近真正的原因了。看来,如果您没有设置 _alertBg.layer.shadowPath 属性,当您滚动表格时,就会发生各种疯狂的事情(我的猜测是表格滚动调用了 _alertBg 并且阴影重绘被快速连续调用太多次,并且您会得到这些视觉伪像)。

添加 shadowPath 可以解决该问题,因此 _alertBg 的图层代码应如下所示:

_alertBg.layer.cornerRadius = 8.0;
_alertBg.layer.borderWidth = 2.0;
_alertBg.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:0.7].CGColor;
_alertBg.layer.shadowColor = [UIColor grayColor].CGColor;
_alertBg.layer.shadowOffset = CGSizeMake(0, 5);
_alertBg.layer.shadowOpacity = 0.8;
_alertBg.layer.shadowPath = [UIBezierPath bezierPathWithRect:_alertBg.bounds].CGPath;
_alertBg.layer.masksToBounds = YES;

只需根据您的喜好修复shadowPath,您就可以开始了。

PS:在我的 Google 搜索中,我发现 this excellent blog post关于阴影,可能会有帮助。

关于iphone - tableView 作为半透明 View 中的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13846936/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com