Skip to content

Instantly share code, notes, and snippets.

@uruly
Created June 11, 2018 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uruly/61f59a125fafb8d69d0fe8924307a733 to your computer and use it in GitHub Desktop.
Save uruly/61f59a125fafb8d69d0fe8924307a733 to your computer and use it in GitHub Desktop.
CarouselView - Transform Scale
//
// 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
}
// セルのスケールを変更する
func transformScale(cell: UICollectionViewCell) {
let cellCenter:CGPoint = self.convert(cell.center, to: nil) //セルの中心座標
let screenCenterX:CGFloat = UIScreen.main.bounds.width / 2 //画面の中心座標x
let reductionRatio:CGFloat = -0.0005 //縮小率
let maxScale:CGFloat = 1 //最大値
let cellCenterDisX:CGFloat = abs(screenCenterX - cellCenter.x) //中心までの距離
let newScale = reductionRatio * cellCenterDisX + maxScale //新しいスケール
cell.transform = CGAffineTransform(scaleX:newScale, y:newScale)
}
}
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
}
}
// 画面内に表示されているセルを取得
let cells = self.visibleCells
for cell in cells {
// ここでセルのScaleを変更する
transformScale(cell: cell)
}
}
}
//
// 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