iOS Swift CollectionView 列表与网格布局之间切换(带动画)

68 篇文章 1 订阅
这篇博客介绍了如何在iOS应用中使用Swift编程语言,通过UICollectionView实现列表和网格布局之间的平滑切换。代码示例展示了如何创建和注册UICollectionViewCell,以及如何配置UICollectionViewFlowLayout来定义列表和网格的布局参数。此外,还提供了点击事件处理方法,用于在两种布局间切换,并带有动画效果。
摘要由CSDN通过智能技术生成

iOS Swift CollectionView 列表与网格布局之间切换(带动画)

//
//  ViewController.swift
//  ChangeList
//
//  Created by wumeng on 2021/5/7.
//

import UIKit

let SCREEN_WIDTH = UIScreen.main.bounds.size.width
let SCREEN_HEIGHT = UIScreen.main.bounds.size.height


class ViewController: UIViewController ,UICollectionViewDelegate ,UICollectionViewDataSource ,UICollectionViewDelegateFlowLayout{
    
    private lazy var listCVLayout: UICollectionViewFlowLayout = {
        
        let collectionFlowLayout = UICollectionViewFlowLayout()
        collectionFlowLayout.sectionInset = UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 30)
        collectionFlowLayout.itemSize = CGSize(width: SCREEN_WIDTH, height: 80)
        collectionFlowLayout.minimumInteritemSpacing = 0
        collectionFlowLayout.minimumLineSpacing = 0
        collectionFlowLayout.scrollDirection = .vertical
        return collectionFlowLayout
    }()
    
    private lazy var gridCVLayout: UICollectionViewFlowLayout = {
        
        let collectionFlowLayout = UICollectionViewFlowLayout()
        collectionFlowLayout.scrollDirection = .vertical
        collectionFlowLayout.sectionInset = UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 30)
        collectionFlowLayout.itemSize = CGSize(width: (SCREEN_WIDTH - 80) / 2 , height: SCREEN_HEIGHT*0.16)
        collectionFlowLayout.minimumInteritemSpacing = 20
        collectionFlowLayout.minimumLineSpacing = 20
        return collectionFlowLayout
    }()
    
    
    private var listCV:UICollectionView!
    private var isListView:Bool = true
    
    private let reuseIdentifier = "Cell"
    private let reuseHeaderIdentifier = "headerView"
    private let reuseFooterIdentifier = "footerView"
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        self.initColleionView()
        
        //
        let controlView = UIControl.init(frame: CGRect.init(x: 10, y: 10, width: 50, height: 50));
        controlView.backgroundColor = UIColor.blue;
        self.view.addSubview(controlView)
        controlView.addTarget(self, action: #selector(WM_FUNC_changeClick(sender:)), for: UIControl.Event.touchUpInside)
        
    }
    

    
    func initColleionView()
    {
        self.listCV =  UICollectionView.init(frame: self.view.frame,collectionViewLayout: self.gridCVLayout)

        //        //注册一个cell
        listCV.register(UICollectionViewCell.self, forCellWithReuseIdentifier:reuseIdentifier)
        //        //注册一个headView
        listCV.register(UICollectionReusableView.self, forSupplementaryViewOfKind:UICollectionView.elementKindSectionHeader, withReuseIdentifier: reuseHeaderIdentifier)
        //        //注册一个footerView
        listCV.register(UICollectionReusableView.self, forSupplementaryViewOfKind:UICollectionView.elementKindSectionFooter, withReuseIdentifier: reuseFooterIdentifier)
        listCV.delegate = self;
        listCV.dataSource = self;
        listCV.backgroundColor = UIColor.white
        //设置每一个cell的宽高
        self.view.addSubview(listCV)
        
    }
    
    // MARK: UICollectionViewDataSource
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 100
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
        
        // Configure the cell
        cell.backgroundColor = UIColor.red;
        
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView{
        var view:UICollectionReusableView =  UICollectionReusableView();
        if kind == UICollectionView.elementKindSectionHeader
        {
            view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: reuseHeaderIdentifier, for: indexPath);
            view.backgroundColor = UIColor.lightGray;
        }
        else
        {
            view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: reuseFooterIdentifier, for: indexPath);
            view.backgroundColor = UIColor.darkGray;
        }
        
        return view;
    }
    
    
    
    // MARK: UICollectionViewDelegate
    
    /*
     // Uncomment this method to specify if the specified item should be highlighted during tracking
     func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
     return true
     }
     */
    
    /*
     // Uncomment this method to specify if the specified item should be selected
     func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
     return true
     }
     */
    
    /*
     // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
     func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
     return false
     }
     
     func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
     return false
     }
     
     func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
     
     }
     */

    //change
    @objc func WM_FUNC_changeClick(sender:Any) -> Void {
        self.isListView = !self.isListView;
        self.WM_FUNC_changeLayout()
    }
    
    //更新布局
    func WM_FUNC_changeLayout() -> Void {
        self.listCV.setCollectionViewLayout(isListView ? self.listCVLayout : self.gridCVLayout, animated: true) { isfinish in
            print("isfinish == \(isfinish)")
        }
    }
    
    
}

图片示例:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WMSmile

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值