[ad_1]
I wish to zoom a collectionView inside a scrollView, however after I zoom the scrollView, the contentOffset of the scrollView would not change. Due to this fact, I am unable to scroll to the highest of the collectionView when zoomed. How can I deal with this example? I wish to easily zoom and scroll, just like studying a PDF.
Right here is my code
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
static let cellId = "CellId"
var collectionView: UICollectionView?
var scrollView: UIScrollView?
override func viewDidLoad() {
tremendous.viewDidLoad()
let format = UICollectionViewFlowLayout()
format.itemSize = CGSize(width: view.body.width, top: view.body.top)
format.scrollDirection = .vertical
format.minimumLineSpacing = 0
format.minimumInteritemSpacing = 0
format.sectionInset = UIEdgeInsetsMake(0,0,0,0)
collectionView = UICollectionView(body: CGRect(origin: .zero, measurement: self.view.body.measurement), collectionViewLayout: format)
collectionView!.register(ZoomableCell.self, forCellWithReuseIdentifier: "singleCell")
collectionView!.delegate = self
collectionView!.dataSource = self
scrollView = UIScrollView(body: self.view.body)
self.view.addSubview(scrollView!)
scrollView!.addSubview(collectionView!)
scrollView!.delegate = self
scrollView!.maximumZoomScale = 3
let doubleTapGestureRecognizer = UITapGestureRecognizer(goal: self, motion: #selector(doubleTapGesture(_:)))
doubleTapGestureRecognizer.numberOfTapsRequired = 2
collectionView!.addGestureRecognizer(doubleTapGestureRecognizer)
}
@objc
non-public func doubleTapGesture(_ sender: UITapGestureRecognizer) {
if ( self.scrollView!.zoomScale > 1.0) {
self.scrollView!.setZoomScale(1.0, animated: true)
} else {
let zoomRect:CGRect = self.zoomRectForScale(scale: 3.0 / 2 , heart: sender.location(in: sender.view))
self.scrollView!.zoom(to: zoomRect, animated: true)
}
}
func zoomRectForScale(scale:CGFloat, heart: CGPoint) -> CGRect{
var zoomRect: CGRect = CGRect()
// this level (heart) is relative to assortment view, want convert again to contentView
let contentW = view.body.width
let contentH = view.body.top
let newX = heart.x - contentW * flooring(heart.x / contentW)
let newPoint = CGPoint(x: newX, y: heart.y)
zoomRect.measurement.top = contentH / scale
zoomRect.measurement.width = contentW / scale
zoomRect.origin.x = newPoint.x - zoomRect.measurement.width / 2.0
zoomRect.origin.y = newPoint.y - zoomRect.measurement.top / 2.0
return zoomRect
}
override func didReceiveMemoryWarning() {
tremendous.didReceiveMemoryWarning()
// Eliminate any assets that may be recreated.
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell:ZoomableCell = collectionView.dequeueReusableCell(withReuseIdentifier: "singleCell", for: indexPath) as! ZoomableCell
let picture = UIImage(named:"check")
cell.imageView.picture = picture
return cell
}
}
extension ViewController: UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return collectionView
}
}
class ZoomableCell: UICollectionViewCell {
var imageView:UIImageView!
required init(coder aDecoder:NSCoder){
tremendous.init(coder: aDecoder)!
}
override init(body:CGRect){
tremendous.init(body:body)
setup()
}
func setup() {
//imageViewを生成
imageView = UIImageView()
imageView.body = CGRect(x:0,y:0,width:self.body.width,top:self.body.top)
contentView.addSubview(imageView)
}
}
[ad_2]