gpt4 book ai didi

swift - 在自定义 UICollectionViewCell 中将叠加层添加到 UIImageView

转载 作者:可可西里 更新时间:2023-11-01 00:59:08 25 4
gpt4 key购买 nike

我有一个带有一些属性的自定义 UICollectionViewCell 类:

class SimpleCollectionCell: UICollectionViewCell {

@IBOutlet weak var title_more: UILabel!

@IBOutlet weak var image_more: UIImageView!

@IBOutlet weak var title: UILabel!

@IBOutlet weak var distance: UILabel!

@IBOutlet weak var activity: UIImageView!

}

我填充数据的方法是这样的:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! SimpleCollectionCell

var place = Place()

// set the current place
place = Places[indexPath.row]

// set the label
cell.title.text = place.title

// setup url
let url = NSURL(string: currentThing.imageUrl!)

// set the cell image
cell.activity.hnk_setImageFromURL(url!)

return cell

}

单元格按应有的方式呈现并显示:

UICollectionViewCells

我的问题是如何让图像变暗?

我尝试在 cellForItemAtIndexPath 方法的底部添加以下代码,但是当您滚动时它会变暗,因为它会不断添加更多 subview !

// Create a subview which will add an overlay effect on image view
let overlay: UIView = UIView(frame: CGRectMake(0, 0, cell.activity.frame.size.width, cell.activity.frame.size.height))
overlay.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.3);

// Add the subview to the UIImageView
cell.activity.addSubview(overlay)

最佳答案

一个快速的解决方案是检查是否已将叠加层添加到单元格中,如果是,则不添加另一个叠加层。

使用标签:

if cell.activity.viewWithTag(99) == nil {
// Create a subview which will add an overlay effect on image view
let overlay = UIView(frame: CGRect(x: 0, y: 0, width: cell.frame.size.width, height: cell.frame.size.height))
overlay.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.3);
overlay.tag = 99

// Add the subview to the UIImageView
cell.activity.addSubview(overlay)
}

或者只是从单元格内创建叠加层。

关于swift - 在自定义 UICollectionViewCell 中将叠加层添加到 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38251578/

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