Swift 在自定义UICollectionViewCell中将覆盖添加到UIImageView
我有一个自定义UICollectionViewCell类,它有几个属性:Swift 在自定义UICollectionViewCell中将覆盖添加到UIImageView,swift,uiimageview,uicollectionviewcell,Swift,Uiimageview,Uicollectionviewcell,我有一个自定义UICollectionViewCell类,它有几个属性: class SimpleCollectionCell: UICollectionViewCell { @IBOutlet weak var title_more: UILabel! @IBOutlet weak var image_more: UIImageView! @IBOutlet weak var title: UILabel! @IBOutlet weak var dist
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
}
该单元按其应有的方式进行渲染,并按如下方式显示:
我的问题是如何使图像更暗?
我曾尝试在cellForItemAtIndexPath方法的底部添加以下代码,但当您滚动时,它会变得更暗,因为它会不断添加更多的子视图
// 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)
}
或者只从单元格内创建覆盖图。为什么不从xib文件创建单元格,并在其中添加覆盖图视图?