美文网首页
自定义控件-LFImagePickerLayoutView

自定义控件-LFImagePickerLayoutView

作者: 菲特峰 | 来源:发表于2020-09-20 12:21 被阅读0次
    //
    //  LFImagePickerLayoutView.swift
    //  PXSSwift
    //
    //  Created by Pro on 2019/12/24.
    //  Copyright © 2019 刘刘峰. All rights reserved.
    //
    
    import UIKit
    
    public typealias CallBack = ()->()
    public typealias DeleteCallBack = (_ deleteIndex:Int)->()
    public typealias IndexCallBack = (_ selectRow:Int)->()
    
    public struct ItemSize {
        var width:CGFloat = 70
        var height:CGFloat = 70
        var minimumInteritemSpacing:CGFloat = 10
        var minimumLineSpacing:CGFloat = 10
    }
    
    class LFImagePickerLayoutView: UIView {
        
        let cellIdentifier = "LFImagePickerCell"
        public var itemSize:ItemSize!
        public var space:CGFloat = 10
        public var datasourceHeight:CGFloat = 0
        //添加回调
        public var addCallBack:CallBack?
        //点击回调
        public var tapCellCallBack:IndexCallBack?
        //删除回调
        public var deletePhotoCallBack:DeleteCallBack?
        //image个数
        public var dataSource:[ZYPhotoModel]?
        //当需要加号的时候是否隐藏加号
        var neeHiddenPlus = false
        //是否需要加号
        public var hiddenPlus = false {
            didSet{
                if hiddenPlus == true{
                    neeHiddenPlus = false
                }
            }
        }
        
        //一行个数
        public var numberOfLine = 3
        //最大几个数
        public var maxNumber = 9
        public var hiddenDelete = false
        
        
        private lazy var imageCollectionView: UICollectionView = {
            let flowLayout = UICollectionViewFlowLayout()
            flowLayout.scrollDirection = .vertical
            //  collectionView
            let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout)
            collectionView.backgroundColor = UIColor.clear
            collectionView.isPagingEnabled = true
            //  添加协议方法
            collectionView.delegate = self
            collectionView.dataSource = self
            //  设置 cell
            
            collectionView.register(cellWithClass: LFImagePickerCell.self)
            collectionView.register(cellWithClass: LFAddImageCell.self)
        
            return collectionView
        }()
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            self.setupUI()
        }
        override public func awakeFromNib() {
            super.awakeFromNib()
            self.setupUI()
        }
        
        required public init?(coder aDecoder: NSCoder) {
            //        fatalError("init(coder:) has not been implemented")
            super.init(coder: aDecoder)
        }
        
    }
    
    //MARK: - UI
    extension LFImagePickerLayoutView {
        override func layoutSubviews() {
            super.layoutSubviews()
            for constanst in  self.constraints {
                var lineNumber:Float = 0.0
                if neeHiddenPlus == true{
                    lineNumber  = ceilf(Float(CGFloat(dataSource?.count ?? 0)/CGFloat(numberOfLine)))
                }else{
                    lineNumber  = ceilf(Float(CGFloat((dataSource?.count ?? 0)+1)/CGFloat(numberOfLine)))
                }
                print(lineNumber)
                constanst.constant = CGFloat(lineNumber) * (itemSize.width + 10.0)
                imageCollectionView.frame.size.width = self.frame.width
                imageCollectionView.frame.size.height = constanst.constant
            }
            self.snp_updateConstraints { (make) in
                make.height.equalTo(imageCollectionView.frame.size.height )
            }
        
    
            
        }
        
        func setupUI(){
            self.imageCollectionView.frame = self.frame
            self.addSubview(self.imageCollectionView)
            itemSize = ItemSize()
            
        }
        
        func updateCollectionView(){
            
        }
        
        public func reloadView(){
            checkHiddenPlus()
            
            let spaceNumber = CGFloat(numberOfLine) - 1
            let width =  (self.frame.size.width - (space * spaceNumber))/CGFloat(numberOfLine)
            itemSize = ItemSize.init(width:width , height: width, minimumInteritemSpacing: space, minimumLineSpacing: space)
            self.layoutSubviews()
            imageCollectionView.reloadData()
        }
        
        func checkHiddenPlus(){
            
            if dataSource == nil{
                neeHiddenPlus = false
                return
            }
            //显示加号且不是最大的时候
            if maxNumber > (dataSource?.count)! && hiddenPlus == false{
                neeHiddenPlus = false
            }else{
                neeHiddenPlus = true
                let array  = dataSource?[0..<maxNumber]
                dataSource = Array(array!)
    
            }
        }
        
    }
    
    //MARK:- UICollectionViewDelegate
    extension LFImagePickerLayoutView:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
        
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            if neeHiddenPlus == true{
    
                return dataSource?.count ?? 0
            }else{
        
                return (dataSource?.count ?? 0) + 1
            }
            
        }
        
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            //加号cell
            if indexPath.row >= dataSource?.count ?? 0 {
                guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier:"LFAddImageCell", for: indexPath) as? LFAddImageCell else {
                    return UICollectionViewCell()
                }
                
                return cell
            //图片cell
            }else{
                guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier:cellIdentifier, for: indexPath) as? LFImagePickerCell else {
                    return UICollectionViewCell()
                }
                //==========
                let photoTuple: ZYPhotoModel = self.dataSource![indexPath.row]
                 if let originImage = photoTuple.originImage {
                    cell.imageView.image = originImage
                 } else if let imageURL = photoTuple.imageURL {
                     if let thumbnailImage = photoTuple.thumbnailImage {
                        cell.imageView.image = thumbnailImage
                     }
                
                    cell.imageView.kf.setImage(with: URL.init(string: imageURL))
                 } else {
                    cell.imageView.image = photoTuple.thumbnailImage
                 }
                //==========
                    
                
                cell.deleteBtn.isHidden = hiddenDelete
    
                //删除
                cell.deleteCallBack = { () in
                    self.dataSource?.remove(at: indexPath.row)
                    self.imageCollectionView.reloadData()
                    self.checkHiddenPlus()
                    self.deletePhotoCallBack!(indexPath.row)
                }
                return cell
            }
            
        }
        
        public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            if indexPath.row >= dataSource?.count ?? 0 { //加号按钮
                if let callback = addCallBack{
                    callback()
                }
            }else{
                if let callback = tapCellCallBack {//点击图片
                    callback(indexPath.row)
                }
            }
        }
        
        public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
            return itemSize.minimumLineSpacing
        }
        
        public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
            return itemSize.minimumInteritemSpacing
        }
        
        public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            return CGSize(width:itemSize.width, height: itemSize.height)
        }
        
    }
    
    

    相关文章

      网友评论

          本文标题:自定义控件-LFImagePickerLayoutView

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