【问题标题】:CAGradientLayer is not being shown?没有显示 CAGradientLayer?
【发布时间】:2016-07-22 13:05:19
【问题描述】:

好的,我有一个集合视图和一个使用 Xib 文件创建的自定义单元格,这样我就可以在不同视图的另一个集合视图中使用相同的单元格。在我的 cellForItemAtIndexPath 我有:

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

return cell

在 setCell func 中我的自定义单元格类中:

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
}

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

【问题讨论】:

  • 检查渐变边框属性、宽度、高度...
  • 是的,我已经检查过,在插入子层时,根据 lldb print 语句,它是:宽度 - 180.0,高度 - 35.0 ...这与 txtView 一样正确相同,并且都具有相同的 x,y 值

标签: ios swift cagradientlayer


【解决方案1】:

尝试将此代码写入 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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多