Skip to content

Instantly share code, notes, and snippets.

@uruly
Created June 10, 2018 17:35
Show Gist options
  • Save uruly/6629c544b9e036636f8514747d6e180e to your computer and use it in GitHub Desktop.
Save uruly/6629c544b9e036636f8514747d6e180e to your computer and use it in GitHub Desktop.
CarouselView - Infinity CollectionView
//
// CarouselCell.swift
// CardCarousel2
//
// Created by Reona Kubo on 2018/06/11.
// Copyright © 2018年 Reona Kubo. All rights reserved.
//
import UIKit
class CarouselCell: UICollectionViewCell {
}
//
// CarouselView.swift
// CardCarousel2
//
// Created by Reona Kubo on 2018/06/11.
// Copyright © 2018年 Reona Kubo. All rights reserved.
//
import UIKit
class CarouselView: UICollectionView {
let cellIdentifier = "carousel"
let pageCount = 5
let colors:[UIColor] = [.blue,.yellow,.red,.green,.gray]
let isInfinity = true
var cellItemsWidth: CGFloat = 0.0
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
super.init(frame: frame, collectionViewLayout: layout)
self.delegate = self
self.dataSource = self
self.register(CarouselCell.self, forCellWithReuseIdentifier: cellIdentifier)
}
convenience init(frame: CGRect) {
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: 200, height: frame.height / 2)
layout.scrollDirection = .horizontal
self.init(frame: frame, collectionViewLayout: layout)
// 水平方向のスクロールバーを非表示にする
self.showsHorizontalScrollIndicator = false
self.backgroundColor = UIColor.white
}
}
extension CarouselView: UICollectionViewDelegate {
}
extension CarouselView: UICollectionViewDataSource {
// セクションごとのセル数
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return isInfinity ? pageCount * 3 : pageCount
}
// セルの設定
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell:CarouselCell = dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! CarouselCell
configureCell(cell: cell, indexPath: indexPath)
return cell
}
func configureCell(cell: CarouselCell,indexPath: IndexPath) {
// indexを修正する
let fixedIndex = isInfinity ? indexPath.row % pageCount : indexPath.row
cell.contentView.backgroundColor = colors[fixedIndex]
}
}
extension CarouselView: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if isInfinity {
if cellItemsWidth == 0.0 {
cellItemsWidth = floor(scrollView.contentSize.width / 3.0) // 表示したい要素群のwidthを計算
}
if (scrollView.contentOffset.x <= 0.0) || (scrollView.contentOffset.x > cellItemsWidth * 2.0) { // スクロールした位置がしきい値を超えたら中央に戻す
scrollView.contentOffset.x = cellItemsWidth
}
}
}
}
//
// ViewController.swift
// CardCarousel2
//
// Created by Reona Kubo on 2018/06/11.
// Copyright © 2018年 Reona Kubo. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var carouselView:CarouselView!
override func viewDidLoad() {
super.viewDidLoad()
let width = self.view.frame.width
let height = self.view.frame.height
carouselView = CarouselView(frame: CGRect(x:0, y:0, width:width, height:height))
carouselView.center = CGPoint(x:width / 2,y: height / 2)
self.view.addSubview(carouselView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment