Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active June 12, 2019 22:35
Show Gist options
  • Save KentarouKanno/3f7dbacaab8ae4a4e341 to your computer and use it in GitHub Desktop.
Save KentarouKanno/3f7dbacaab8ae4a4e341 to your computer and use it in GitHub Desktop.
UICollectionView Simple Template

UICollectionView Simple Template

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView.delegate = self
        collectionView.dataSource = self
    }
    
    // MARK: - UICollectionViewDelegate
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
        cell.titleLabel.text = "Apple"
        cell.imageView.image = UIImage(named: "Apple")
        cell.backgroundColor = UIColor.yellow
        return cell
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 30
    }
    
    // Cell Size Change
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 150, height: 150)
    }
}

// Custom Cell Class
import UIKit

class CustomCell: UICollectionViewCell {
    
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
    
    override init(frame: CGRect){
        super.init(frame: frame)
    }
    
    required init(coder aDecoder: NSCoder){
        super.init(coder: aDecoder)!
    }
}

UICollectionView Image

image

★ UICollectionView Paging Sample

参考URL: 参考

import UIKit

// ---------  ViewController ----------

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

   @IBOutlet weak var collectionView: UICollectionView!
   
   override func viewDidLoad() {
       super.viewDidLoad()
       
   }
   
   func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return 10;
   }
   
   func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
       
       let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
       cell.label.text = "\(indexPath.section) - \(indexPath.row)";
       
       return cell
   }
}

// ---------  Custom Cell ----------

class CustomCell: UICollectionViewCell {
   
   @IBOutlet weak var label: UILabel!
}


// ---------  Custom UICollectionViewFlowLayout ----------


class CustomCollectionViewFlowLayout: UICollectionViewFlowLayout {
   
   let itemLength: CGFloat = 130
   let itemWidth : CGFloat = 280
   let minInteritemSpacing: CGFloat = 20
   let velocityThreshold   = 0.2
   
   override func awakeFromNib() {
       super.awakeFromNib()
       prepare()
   }
   
   override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
       
       let currentPage = self.collectionView!.contentOffset.x / pageWidth()
       
       if fabs(Double(velocity.x)) > velocityThreshold {
           let nextPage = (velocity.x > 0.0) ? ceil(currentPage) : floor(currentPage)
           return CGPointMake((nextPage * pageWidth()), proposedContentOffset.y)
       } else {
           return CGPointMake((round(currentPage) * pageWidth()), proposedContentOffset.y)
       }
   }
   
   func pageWidth() -> CGFloat {
       return itemSize.width + minimumLineSpacing
   }
   
   func prepare() {
       self.itemSize = CGSizeMake(itemWidth, itemLength);
       self.minimumLineSpacing = minInteritemSpacing;
       self.scrollDirection = .Horizontal
       
       let horizontalInset = (UIScreen.mainScreen().bounds.size.width - itemWidth) / 2
       let verticalInset: CGFloat = 20
       
       self.sectionInset = UIEdgeInsetsMake(verticalInset, horizontalInset, verticalInset, horizontalInset);
   }
}

サンプルURL: Paging CollectionView

s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment