当前位置: 代码迷 >> 综合 >> Swift5学习之旅之UICollectionView、UIVisualEffectView(模糊)
  详细解决方案

Swift5学习之旅之UICollectionView、UIVisualEffectView(模糊)

热度:18   发布时间:2024-01-09 15:29:01.0

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 = .centeraddSubview(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 = 1layout.minimumInteritemSpacing = 1layout.footerReferenceSize = CGSize(width: frame.width, height: 50)layout.headerReferenceSize = CGSize(width: frame.width, height: 50)super.init(frame: frame, collectionViewLayout: layout)self.backgroundColor = UIColor.clearself.register(MyCollectionViewCell.self, forCellWithReuseIdentifier: "cell")}required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")}
}