美文网首页
swift 4.0 仿淘宝网格与列表切换效果

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

作者: 谁拿浮生伴我一世流年 | 来源:发表于2019-02-27 15:20 被阅读5次

    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文件,不展示

    效果图如下:

    相关文章

      网友评论

          本文标题:swift 4.0 仿淘宝网格与列表切换效果

          本文链接:https://www.haomeiwen.com/subject/gblfuqtx.html