简体   繁体   English

UIVisualEffectView明显添加到UICollectionViewCell

[英]UIVisualEffectView added to UICollectionViewCell visibly

I have UICollectionView with a lot of cells. 我有很多单元格的UICollectionView The cells have images on them that need to be blurred. 细胞上有需要模糊的图像。 That's why I am adding UIVisualEffectView with blur effect to the image views . 这就是为什么我要在image views添加具有模糊效果的UIVisualEffectView The cells are like pages - every cell takes up the whole screen. 单元格就像页面-每个单元格占据整个屏幕。

The problem is with the second cell. 问题出在第二个单元格上。 When it appears the blurred view is added visibly and it's ugly. 当它出现时,模糊的视图被明显地添加并且很丑陋。 The rest of the cells have the blurred view already when they become visible on the screen and are okay. 当其余的单元格在屏幕上可见并且可以时,它们的视图已经模糊。 This is the code I am using to add the blur view : 这是我用来添加blur view的代码:

class ImageCell: UICollectionViewCell {

    @IBOutlet weak var mainImageView: UIImageView!
    @IBOutlet weak var backgroundImageView: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()

        let regularBlur = UIBlurEffect(style: .regular)
        let blurView = UIVisualEffectView(effect: regularBlur)
        blurView.frame = backgroundImageView.bounds
        blurView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        backgroundImageView.addSubview(blurView)

    }
}

Modifying your code: 修改代码:

class ImageCell: UICollectionViewCell {

    @IBOutlet weak var mainImageView: UIImageView!
    @IBOutlet weak var backgroundImageView: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()

        backgroundImageView.isHidden = true
        let regularBlur = UIBlurEffect(style: .regular)
        let blurView = UIVisualEffectView(effect: regularBlur)
        blurView.frame = backgroundImageView.bounds
        blurView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        backgroundImageView.addSubview(blurView)
        backgroundImageView.isHidden = false
    }
}

You can code like this: 您可以这样编写:

class ImageCell: UICollectionViewCell {

@IBOutlet weak var mainImageView: UIImageView!
@IBOutlet weak var backgroundImageView: UIImageView!

let blurView = UIVisualEffectView(effect: UIBlurEffect(style: .regular))


override func awakeFromNib() {
    super.awakeFromNib()

    blurView.frame = backgroundImageView.bounds
    blurView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
    backgroundImageView.addSubview(blurView)
}

override func prepareForReuse() {
        blurView.isHidden = true
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM