swift 4.0 仿淘宝网格与列表切换效果

1//设置一个全局Bool类型的变量并赋值

 var _isGrid:Bool = true//默认网格

2. 视图采用UICollectionView展现,具体方法不赘述

3.切换按钮点击事件响应

 //MARK:切换列表或者网格

 func switchListOrCollection() {

//更改itemsize的宽和高,并更改全局变量的值

        if _isGrid == true{

            _isGrid = false

            layout.itemSize=CGSize(width:self.view.frame.size.width - 27, height:160)

        }

        else

        {

            letwidth = (kScreenWidth-34)/2.0

            _isGrid=true

            layout.itemSize=CGSize(width: width, height:263)

        }

//刷新collectionView

        collectionView.reloadData()

    }

4.UICollectionViewDataSource代理方法实现

func collectionView(_collectionView:UICollectionView, cellForItemAt indexPath:IndexPath) ->UICollectionViewCell{

        if _isGrid == true {

            let cell =  collectionView.dequeueReusableCell(withReuseIdentifier:String(NSStringFromClass(ZKHomeCollectionViewCell.self)), for: indexPath) as! ZKHomeCollectionViewCell

            return cell

        }

        else

        {

            let cell =  collectionView.dequeueReusableCell(withReuseIdentifier:String(NSStringFromClass(ZKHomeListCell.self)), for: indexPath) as! ZKHomeListCell

            returncell

        }

    }


注意:ZKHomeCollectionViewCell与ZKHomeListCell 系本人封装的cell.swift文件,不展示

效果图如下:

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容