- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个 UICollectionView
,单元格像信用卡一样,我想添加渐变层作为这张卡片的背景。首先,我为阴影添加了背景 View (shadowView
),然后为渐变层添加了第二个 View (bgView
)并添加了 CAGradientLayer
作为 的子层>背景 View
。但是我没有得到任何gradient View 。这是我的代码:
class CardCell: BaseCell {
private var shadowView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = UIColor.white
view.layer.shadowColor = UIColor.shadowColor.cgColor
view.layer.shadowOffset = CGSize(width: 0, height: 1.0)
view.layer.shadowOpacity = 0.1
view.layer.shadowRadius = 15.0
return view
}()
private var bgView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.cornerRadius = 5
view.layer.masksToBounds = true
view.backgroundColor = .clear
view.layer.addSublayer(createGradientLayer(for: view))
return view
}()
override func setup() {
super.setup()
addSubview(shadowView)
addSubview(bgView)
// setup constraints
shadowView.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.9).isActive = true
shadowView.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 1).isActive = true
shadowView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
shadowView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
bgView.topAnchor.constraint(equalTo: shadowView.topAnchor).isActive = true
bgView.bottomAnchor.constraint(equalTo: shadowView.bottomAnchor).isActive = true
bgView.leadingAnchor.constraint(equalTo: shadowView.leadingAnchor).isActive = true
bgView.trailingAnchor.constraint(equalTo: shadowView.trailingAnchor).isActive = true
}
private static func createGradientLayer(for view: UIView) -> CAGradientLayer {
let color = UIColor(red:0.95, green:0.42, blue:0.64, alpha:1.0)
let gradientLayer = CAGradientLayer()
gradientLayer.frame = view.bounds
gradientLayer.colors = [color.cgColor, color.withAlphaComponent(0.7).cgColor]
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
gradientLayer.endPoint = CGPoint(x: 1.1, y: 1.1)
return gradientLayer
}
}
但如果我更改此代码并从 bgView init
中删除渐变设置并在约束后写入它,一切正常,但 shadow view 消失了:
. . .
bgView.leadingAnchor.constraint(equalTo: shadowView.leadingAnchor).isActive = true
bgView.trailingAnchor.constraint(equalTo: shadowView.trailingAnchor).isActive = true
bgView.layoutIfNeeded()
bgView.layer.addSublayer(CardCell.createGradientLayer(for: bgView))
最佳答案
extension UIView {
func addGradientToViewWithCornerRadiusAndShadow(radius:CGFloat,firstColor:UIColor,secondColor:UIColor,locations:[CGFloat]){
for layer in (self.layer.sublayers ?? []){
if let layer1 = layer as? CAGradientLayer{
layer1.removeFromSuperlayer()
}
}
let gradient = CAGradientLayer()
var rect = self.bounds
rect.size.width = ScreenWidth
gradient.frame = rect
gradient.colors = [firstColor.cgColor, secondColor.cgColor]
gradient.locations = locations as [NSNumber]
gradient.startPoint = CGPoint.init(x: 0, y: 0)
gradient.endPoint = CGPoint.init(x: 0, y: 1)
gradient.cornerRadius = radius
gradient.shadowRadius = 2
gradient.shadowColor = UIColor.lightGray.cgColor
gradient.shadowOffset = CGSize.init(width: 0.5, height: 0.5)
gradient.shadowOpacity = 0.5
self.layer.insertSublayer(gradient, at: 0)
}
}
尝试使用这个 UIView 的扩展。如果这有帮助,请告诉我。
关于ios - UICollectionViewCell 中的渐变 BG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51606206/
如果这个在过去有效,但突然就不行了。下面是结构:Thre 也有一个设置和控制,背景颜色和图像设置都单独工作,但是当我有两个设置时它不会工作,最初我让它工作,颜色总是工作,除非选择背景图像,那会覆盖它,
我有一个网站,它通过加入 body 标签的 bg 和 div 标签的 bgs 来完成布局。但是,根据您使用的浏览器和操作系统,由于滚动条,它们似乎无法准确匹配。 (我已经通过添加 overflow:h
我正在尝试根据用户将鼠标悬停在求职者按钮还是招聘人员按钮上来更改背景图片。 我尝试了以下代码(在 JS Fiddle 中有效)但失败了。我插入了一个 background-color 命令(参见注释行
所以我有一个包含几行内容的部分。线条覆盖了窗口宽度的 100%,但是内容仅显示在部分的右侧。左边必须有一个图像。我试着用 bg-image 和 z-index 来做,但问题是它被埋在线条的 bg-co
与 Bootstrap 的 bg-danger 类关联的背景颜色未应用于类 form-control 的输入文本框。如何做到这一点? 最佳答案 form-co
我在有序列表中显示代码,以便它显示行号。每个 li 都有一个 pre 标签,其中填充了一行代码。 我的问题是,当我设置 ol 的宽度并允许溢出滚动时,溢出的背景颜色与最初显示区域的背景颜色不同。 ol
例如,在一列中我有 3 个 block (div),在第二列中我有其他 block 的列表 (div)。 第一列 block 代表蛋糕层,第二列 block 代表可以应用于任何蛋糕层的口味。 因此,通
我正在处理日期和时间的本地化代码,当文化设置为保加利亚语并且我传入的日期是 11 月时,我看到了一个奇怪的行为。 本质上,我正在尝试调用: if (DateTime.TryParse(theDateT
在下图中,viewController 背景为白色,pageViewController 背景颜色可见。如何清除或透明 viewController 背景,以便紫罗兰色成为所有 viewControl
阅读 pygame 教程 here ,你会发现这个例子:(箭头是我的) for o in objects: screen.blit(background, o.pos, o.pos) # Re
我制作了一个播放背景音乐的服务,但是当音乐结束时我想再次重播它。我可以在我的服务中使用哪种方法? public class BackgroundSoundService extends Service
我正在设计一个由大量元素组成的常规 android LinearLayout,我将布局参数都设置为 wrap_content 并使其环绕 bg 或 elements 根据哪个更大,那么,如何分配布局,
我有一个像 BasicCell>ContentView>Container 这样的 UITableView 单元设置。当我在界面生成器中设置容器的背景时,我得到了我设置的所需的深灰色阴影。但是,当我以
我正在尝试制作我的横幅图片,目前它的全宽约为 1500 像素,我已将其设置为背景图片,这样我就可以在顶部使用一些元素,我知道你可以制作一个全宽的图片标签,但我不确定您是否可以对背景图片标签执行相同的操
我这里有这样的背景 background:url(../img/arrow_lch.png) right no-repeat #2e443a; 我只需要降低 bg color(#2e443a) 不透明
我的网站有一个看起来很酷的背景,但可能会影响可用性(如果你看得太久它会烧毁)所以我想要一个链接,可以用 javascript 更改/删除背景图像。我用谷歌搜索过,但找不到有用的东西。 有没有办法建立一
我想将视差滚动效果添加到我主页的 .intro-body 中,就像我将此效果添加到我的“关于”中的超大屏幕图像中一样、“简历”和“作品集”页面。然而,出于某种原因,当我将它应用于 .bg 类时,背景图
我得到了一个名为remove.bg 的API。我想在Flutter应用中使用此API(以python语言提供)。可能吗 此API用于删除图像背景。 为了使此功能正常工作,我需要执行哪些步骤/研究? 进
每当我将鼠标悬停在某个元素上时,我都会尝试更改页面背景。目前我已经将不同的图像上传到我的网络服务器中,当我将鼠标悬停在 div 上时,我想将页面背景从其中一个图像更改为另一张图像。我该怎么做呢?无论是
我这里有一段非常基本的 R 代码,以及它生成的图。为什么圆点没有填成红色? plot(1, 1, col="blue", bg="red", cex=4) 最佳答案 您需要使用 pch = 21 到
我是一名优秀的程序员,十分优秀!