UICollectionView细胞之间的活力效果

时间:2016-10-20 13:31:54

标签: ios iphone swift cocoa-touch uicollectionview

我有UICollectionView个3个单元格(单元格宽度为self.collectionView.frame.size.width / 3)。

collectionView位于具有UIVisualEffectView模糊效果的dark内。

我想在每个细胞之间添加1px的活力差距,我试过了:

let divider1 = UIVisualEffectView(frame: CGRect(x: self.collectionView.frame.size.width / 3, y: 0, width: 1, height: self.collectionView.frame.size.height))
divider1.effect = UIVibrancyEffect(blurEffect: UIBlurEffect(style: .dark))

let divider2 = UIVisualEffectView(frame: CGRect(x: (self.collectionView.frame.size.width / 3) * 2, y: 0, width: 1, height: self.collectionView.frame.size.height))
        divider2.effect = UIVibrancyEffect(blurEffect: UIBlurEffect(style: .dark))

self.collectionView.addSubview(divider1)
self.collectionView.addSubview(divider2)

但它没有提供任何东西。如果我将分隔符的backgroundColor设置为它确实有效的颜色,那么它可能具有振动效果。

知道为什么吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

Try this code:

Note: Tested in Swift 3

   override func viewDidLoad() {
    super.viewDidLoad()

        // Add a background view to the collectionView
        let backgroundImage = UIImage(named: "Set your image file here")
        let imageView = UIImageView(image: backgroundImage)
        self.collectionView.backgroundView = imageView

        //To get a balance look from collectionView background
        collectionView.backgroundColor = UIColor(patternImage: backgroundImage!)

        // To setup a blurView background
        let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
        let blurView = UIVisualEffectView(effect: blurEffect)
        blurView.frame = view.bounds
        imageView.addSubview(blurView)

}

Output:

enter image description here

答案 1 :(得分:0)

要使其正常工作,您需要使用在集合视图中使用的模糊效果来创建UIVibrancyEffect。