NFCCardVC.swift 5.3 KB
//
//  NFCCardVC.swift
//  Twear
//
//  Created by yangbin on 2022/2/12.
//

import UIKit
import SnapKit

class NFCCardVC: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var collectionViewFlowLayout: UICollectionViewFlowLayout!
    private let cardArray1 = ["NFC_1", "NFC_2", "NFC_3", "NFC_4", "NFC_5", "NFC_6"]
    private let cardArray2 = ["NFC_7", "NFC_8", "NFC_9", "NFC_10", "NFC_11", "NFC_12"]
    private let textArray = [LocString("色彩卡片"), LocString("创意卡片")]
    var cardClosure: ((_ card: String) -> ())?
    
    var curIndexPath: IndexPath = IndexPath(row: 0, section: 0)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = LocString("显示")
        
        let saveButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 28))
        saveButton.setTitle(LocString("保存"), for: .normal)
        saveButton.titleLabel?.font = RegularFont(14)
        saveButton.setTitleColor(TintColor, for: .normal)
        saveButton.addTarget(self, action: #selector(save), for: .touchUpInside)
        navigationItem.rightBarButtonItem =  UIBarButtonItem(customView: saveButton)
        
        collectionViewFlowLayout.minimumLineSpacing = 10
        collectionViewFlowLayout.minimumInteritemSpacing = 10
        collectionViewFlowLayout.sectionInset = UIEdgeInsets(top: 10, left: 12, bottom: 5, right: 12)
        let width = (SCREEN_WIDTH-36)/2.0
        collectionViewFlowLayout.itemSize = CGSize(width: width, height: width*3.0/7.0)
        collectionViewFlowLayout.headerReferenceSize = CGSize(width: SCREEN_WIDTH, height: 30)
//        collectionView.bounces = false
        collectionView.showsVerticalScrollIndicator = false
        
        // Do any additional setup after loading the view.
    }
    
    @objc private func save() {
        cardClosure?("\(curIndexPath.section*6+curIndexPath.row+1)")
        navigationController?.popViewController(animated: true)
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
extension NFCCardVC: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        var reusableView: UICollectionReusableView?
        if kind == UICollectionView.elementKindSectionHeader {
            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "NFCCardHeaderView", for: indexPath) as! NFCCardHeaderView
            let label = UILabel()
            label.font = RegularFont(11)
    //        label.text = LocString(settingArray[section])
            headerView.addSubview(label)
            label.snp.makeConstraints { make in
                make.left.equalToSuperview().offset(12)
                make.bottom.equalToSuperview().offset(-2)
            }
            label.text = textArray[indexPath.section]
//            headerView.backgroundColor = .red
            reusableView = headerView
//            return headerV
        }
        return reusableView!
    }
    
    
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//        if section == 0 {
//            return 1
//        } else {
            return 6
//        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "NFCCardCell", for: indexPath) as! NFCCardCell
        if indexPath.section == 0 {
            cell.imageView.image = UIImage(named: cardArray1[indexPath.row])
        } else {
            cell.imageView.image = UIImage(named: cardArray2[indexPath.row])
        }
        if indexPath == curIndexPath {
            cell.selectedImageView.isHidden = false
        }
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as! NFCCardCell
        let tempCell =  collectionView.cellForItem(at: curIndexPath) as! NFCCardCell
        tempCell.selectedImageView.isHidden = true
        cell.selectedImageView.isHidden = false
        curIndexPath = indexPath
    }
}

class NFCCardHeaderView: UICollectionReusableView {
    
//    var label: UILabel = UILabel()
//
//    override init(frame: CGRect) {
//        super.init(frame: frame)
//        self.backgroundColor = .black
//        label.font = RegularFont(11)
////        label.text = LocString(settingArray[section])
//        self.addSubview(label)
//        label.snp.makeConstraints { make in
//            make.left.equalToSuperview().offset(12)
//            make.top.equalToSuperview().offset(7.5)
//        }
//    }
//
//    required init?(coder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
    
}