Don_Mag

Collection View Example

Oct 21st, 2021 (edited)
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.02 KB | None | 0 0
  1. class MyCellView: UICollectionViewCell {
  2.    
  3.     let theLabel = UILabel()
  4.    
  5.     override init(frame: CGRect) {
  6.         super.init(frame: frame)
  7.         commonInit()
  8.     }
  9.     required init?(coder: NSCoder) {
  10.         super.init(coder: coder)
  11.         commonInit()
  12.     }
  13.     func commonInit() -> Void {
  14.         theLabel.translatesAutoresizingMaskIntoConstraints = false
  15.         theLabel.textAlignment = .center
  16.         theLabel.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
  17.         contentView.addSubview(theLabel)
  18.         let g = contentView.layoutMarginsGuide
  19.         NSLayoutConstraint.activate([
  20.             theLabel.centerXAnchor.constraint(equalTo: g.centerXAnchor),
  21.             theLabel.centerYAnchor.constraint(equalTo: g.centerYAnchor),
  22.         ])
  23.     }
  24.    
  25. }
  26.  
  27. class CollViewVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  28.    
  29.     let cellId: String = "c"
  30.    
  31.     let numItems: Int = 100
  32.    
  33.     let colors: [UIColor] = [
  34.         .systemRed, .systemBlue, .systemGreen,
  35.         .systemPink, .systemYellow, .systemTeal
  36.     ]
  37.    
  38.     lazy var navToLabel: UILabel = {
  39.         let v = UILabel()
  40.         v.numberOfLines = 0
  41.         v.backgroundColor = .green
  42.         v.text = "Nav To Label: \(numItems) Items (zero-based)"
  43.         v.translatesAutoresizingMaskIntoConstraints = false
  44.         return v
  45.     }()
  46.    
  47.     let scrollView: UIScrollView = {
  48.         let v = UIScrollView()
  49.         v.translatesAutoresizingMaskIntoConstraints = false
  50.         return v
  51.     }()
  52.    
  53.     public lazy var myCollection : UICollectionView = {
  54.         let routine = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout());
  55.         let routineLayout = UICollectionViewFlowLayout()
  56.         routineLayout.scrollDirection = .horizontal
  57.         routine.collectionViewLayout = routineLayout
  58.         routine.decelerationRate = UIScrollView.DecelerationRate.fast
  59.         routine.translatesAutoresizingMaskIntoConstraints = false
  60.         routine.delegate = self
  61.         routine.dataSource = self
  62.         routine.backgroundColor = .clear
  63.         routine.register(MyCellView.self, forCellWithReuseIdentifier: cellId)
  64.         routine.isPagingEnabled = true
  65.         routine.showsHorizontalScrollIndicator = false
  66.         routine.showsVerticalScrollIndicator = false
  67.         routine.layer.cornerRadius = 20
  68.         routine.layer.masksToBounds = true
  69.         return routine
  70.     }()
  71.    
  72.     override func viewDidLoad() {
  73.         super.viewDidLoad()
  74.        
  75.         view.addSubview(myCollection)
  76.        
  77.         view.addSubview(scrollView)
  78.         scrollView.addSubview(navToLabel)
  79.        
  80.         let cg = scrollView.contentLayoutGuide
  81.         let fg = scrollView.frameLayoutGuide
  82.        
  83.         NSLayoutConstraint.activate([
  84.            
  85.             myCollection.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
  86.             myCollection.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor),
  87.             myCollection.widthAnchor.constraint(equalTo: view.safeAreaLayoutGuide.widthAnchor, multiplier: 0.9),
  88.             myCollection.heightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.heightAnchor, multiplier: 0.40),
  89.  
  90.             scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 40.0),
  91.             scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -40.0),
  92.             scrollView.topAnchor.constraint(equalTo: myCollection.bottomAnchor, constant: 40.0),
  93.             scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -20.0),
  94.  
  95.             navToLabel.topAnchor.constraint(equalTo: cg.topAnchor, constant: 0.0),
  96.             navToLabel.leadingAnchor.constraint(equalTo: cg.leadingAnchor, constant: 0.0),
  97.             navToLabel.trailingAnchor.constraint(equalTo: cg.trailingAnchor, constant: 0.0),
  98.             navToLabel.bottomAnchor.constraint(equalTo: cg.bottomAnchor, constant: 0.0),
  99.            
  100.             navToLabel.widthAnchor.constraint(equalTo: fg.widthAnchor, constant: 0.0),
  101.            
  102.         ])
  103.     }
  104.    
  105.     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  106.         return 0
  107.     }
  108.    
  109.     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  110.         return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
  111.     }
  112.    
  113.     func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
  114.         let navigatedTo =  Int(targetContentOffset.pointee.x) / Int(myCollection.frame.width)
  115.         guard let t = navToLabel.text else {
  116.             return
  117.         }
  118.         let str = t + "\n\(targetContentOffset.pointee.x) / \(navigatedTo)"
  119.         navToLabel.text = str
  120.     }
  121.    
  122.     func numberOfSections(in collectionView: UICollectionView) -> Int {
  123.         return 1
  124.     }
  125.     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  126.         return numItems
  127.     }
  128.     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  129.         let c = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MyCellView
  130.         c.contentView.backgroundColor = colors[indexPath.item % colors.count]
  131.         c.theLabel.text = "\(indexPath.item)"
  132.         return c
  133.     }
  134.    
  135. }
  136.  
Add Comment
Please, Sign In to add comment