Skip to content

Instantly share code, notes, and snippets.

@uruly
Last active June 11, 2024 16:19
Show Gist options
  • Save uruly/7fbb3460634c46c31c638d219dd58e25 to your computer and use it in GitHub Desktop.
Save uruly/7fbb3460634c46c31c638d219dd58e25 to your computer and use it in GitHub Desktop.
Zoomable CollectionView
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let collectionView = ZoomableCollectionView(frame:self.view.frame)
self.view.addSubview(collectionView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import UIKit
class ZoomableCell: UICollectionViewCell {
var imageView:UIImageView!
var scrollView:UIScrollView!
required init(coder aDecoder:NSCoder){
super.init(coder: aDecoder)!
}
override init(frame:CGRect){
super.init(frame:frame)
setup()
}
func setup() {
//スクロールビューを設置
scrollView = UIScrollView()
scrollView.frame = CGRect(x:0,y:0,width:self.frame.width,height:self.frame.height)
//デリゲートを設定
scrollView.delegate = self
//最大・最小の大きさを決める
scrollView.maximumZoomScale = 4.0
scrollView.minimumZoomScale = 1.0
self.contentView.addSubview(scrollView)
//imageViewを生成
imageView = UIImageView()
imageView.frame = CGRect(x:0,y:0,width:self.frame.width,height:self.frame.height)
scrollView.addSubview(imageView)
let doubleTap = UITapGestureRecognizer(target:self,action:#selector(self.doubleTap(gesture:)))
doubleTap.numberOfTapsRequired = 2
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(doubleTap)
}
// ダブルタップ
@objc func doubleTap(gesture: UITapGestureRecognizer){
// if ( self.scrollView.zoomScale < self.scrollView.maximumZoomScale ) {
if ( self.scrollView.zoomScale < 3 ) {
let newScale:CGFloat = self.scrollView.zoomScale * 3
let zoomRect:CGRect = self.zoomRectForScale(scale: newScale, center: gesture.location(in: gesture.view))
self.scrollView.zoom(to: zoomRect, animated: true)
} else {
self.scrollView.setZoomScale(1.0, animated: true)
}
}
// 領域
func zoomRectForScale(scale:CGFloat, center: CGPoint) -> CGRect{
var zoomRect: CGRect = CGRect()
zoomRect.size.height = self.scrollView.frame.size.height / scale
zoomRect.size.width = self.scrollView.frame.size.width / scale
zoomRect.origin.x = center.x - zoomRect.size.width / 2.0
zoomRect.origin.y = center.y - zoomRect.size.height / 2.0
return zoomRect
}
}
extension ZoomableCell :UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return self.imageView
}
func scrollViewDidZoom(_ scrollView: UIScrollView) {
print("zoomおわり")
}
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
print("zoomするよ")
}
}
import UIKit
class ZoomableCollectionView: UICollectionView{
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
super.init(frame: frame, collectionViewLayout: layout)
}
convenience init(frame: CGRect) {
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width:frame.width,height:frame.width * 4 / 3)
//スクロールの向き
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
layout.sectionInset = UIEdgeInsetsMake(0,0,0,0)
self.init(frame: frame, collectionViewLayout: layout)
setup()
}
func setup() {
//デリゲートをつける
self.delegate = self
self.dataSource = self
//カスタムセルを指定
self.register(ZoomableCell.self, forCellWithReuseIdentifier: "singleCell")
//スクロールバーを表示するかどうか
self.showsHorizontalScrollIndicator = false
self.showsVerticalScrollIndicator = false
self.backgroundColor = UIColor.gray
//ページングをするかどうか
self.isPagingEnabled = true
}
}
extension ZoomableCollectionView: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("タップ")
}
}
extension ZoomableCollectionView: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//セルの設定
let cell:ZoomableCell = collectionView.dequeueReusableCell(withReuseIdentifier: "singleCell", for: indexPath) as! ZoomableCell
let image = UIImage(named:"image2.png")
cell.imageView.image = image
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment