Swift5学习之旅-----UICollectionView、UIVisualEffectView(模糊)
-
整体代码
Github还没上传,先用着Dropbox(可能要翻墙),看完有收获的感谢点个赞👍
https://www.dropbox.com/sh/jseevw291rrgf6d/AACwt1FsOKRKuEi64glXGWd_a?dl=0
Dropbox UICollectionView、UIVisualEffectView(模糊) -
UICollectionViewCell
class MyCollectionViewCell: UICollectionViewCell {
var title: UILabel!
var imageView: UIImageView!
var blur: UIVisualEffectView!
override init(frame: CGRect) {
super.init(frame: frame)
imageView = UIImageView()
imageView.frame = CGRect(x: 10, y: 10, width:bounds.size.width, height: bounds.size.width)
blur = UIVisualEffectView(frame: CGRect(x: 10, y: 10, width:bounds.size.width, height: bounds.size.width))
title = UILabel(frame: CGRect(x: 0, y: imageView.frame.origin.y + imageView.frame.size.width + 10, width: self.bounds.size.width, height: 20))
title.textAlignment = .center
addSubview(imageView)
addSubview(title)
addSubview(blur)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
- UICollectionView
class MyCollectionView: UICollectionView {
override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
let layout = UICollectionViewFlowLayout()
//每个元素的大小
layout.itemSize = CGSize(width: (frame.width - 60)/3, height: (frame.width)/3 + 20)
layout.minimumLineSpacing = 1
layout.minimumInteritemSpacing = 1
layout.footerReferenceSize = CGSize(width: frame.width, height: 50)
layout.headerReferenceSize = CGSize(width: frame.width, height: 50)
super.init(frame: frame, collectionViewLayout: layout)
self.backgroundColor = UIColor.clear
self.register(MyCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}