gpt4 book ai didi

ios - CAGradientLayer 未显示?

转载 作者:行者123 更新时间:2023-11-30 13:13:03 33 4
gpt4 key购买 nike

好的,我有一个 Collection View 和一个使用 Xib 文件创建的自定义单元格,以便我可以在不同 View 的另一个 Collection View 中使用相同的单元格。在我的 cellForItemAtIndexPath 中我有:

let cell = collectionView.dequeueReusableCellWithReuseIdenitifier(cellIdentifier, forIndexPath: indexPath) as! StoriesCollectionViewCell
cell.setCell(arrArticles[indexPath.item])

return cell

以及 setCell 函数中我的自定义单元类:

func setCell(article : Article) {
self.articleImageView.alpha = 0.0
self.storyHeadline.text = article.headline

//set the background for the txtView to a gradient, from clear to white
let gradient = CAGradientLayer()
gradient.frame = txtView.frame
gradient.colors = [UIColor.clearColor().CGColor, UIColor.whiteColor().CGColor]
// gradient.startPoint = txtView.frame.origin
// gradient.endPoint = CGPointMake(txtView.frame.maxX, txtView.frame.maxY)
self.txtView.layer.insertSublayer(gradient, atIndex: 0)

//for testing purpose only, will be replaced by web service response
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = .MediumStyle
dateFormatter.timeStyle = .NoStyle

self.storyHeadlineDate.text = dateFormatter.stringFromDate(article.date!)
manager.imageForArticle(article) { (image) in
self.articleImageView.image = image
UIView.animateWithDuration(1, animations: {
self.articleImageView.alpha = 1.0
})
}
self.layer.cornerRadius = 5.0
}

除了渐变背景颜色之外,一切都按照我想要的方式工作。我还想知道是否有更好的委托(delegate)方法来设置此渐变。顺便说一句,txtView 是从 xib 创建和链接的 UIView。

最佳答案

尝试将此代码写入tableViewCell文件

override func didMoveToSuperview() {

let screenWidth = UIScreen.mainScreen().bounds.width
gradientView.bounds.size.width = screenWidth
let gradient: CAGradientLayer = CAGradientLayer()
gradient.frame = gradientView.bounds
gradient.colors = [
UIColor(hex: "#030303" ,alpha: 0).CGColor,
UIColor(hex: "#030303" ,alpha: 0.6).CGColor ]
print("color set: ", gradient.colors)
gradient.locations = [0.0, 0.6]
gradient.opacity = 0.4

gradientView.layer.insertSublayer(gradient, atIndex: 0)
gradientView.layer.masksToBounds = true

}

关于ios - CAGradientLayer 未显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38527223/

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