2016-07-22 104 views
0

好吧,我有一個集合視圖和我使用Xib文件創建的自定義單元格,以便可以在另一個視圖的另一個集合視圖中使用同一個單元格。在我cellForItemAtIndexPath我:CAGradientLayer沒有顯示?

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是一個UIView創建和鏈接從xib,順便說一句。

+0

檢查梯度frame屬性,寬度,高度... –

+0

是啊,我已經籤的是,在被插入子的時候,根據LLDB print語句是:width - 180.0,height - 35.0 ...這與txtView是相同的,並且都具有相同的x,y值 –

回答

0

嘗試寫這樣的代碼來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 

}