From d012e92dcd8562a007718ff57909d04f57a90941 Mon Sep 17 00:00:00 2001 From: Yang Tun-Kai Date: Tue, 16 Jan 2018 22:30:11 +0800 Subject: [PATCH] update to swift 4 --- MVCarouselCollectionView/MVCarouselCell.swift | 4 +- MVCarouselCollectionView/MVCarouselCell.xib | 20 +++- .../MVCarouselCellScrollView.swift | 24 ++-- .../MVCarouselCollectionView.swift | 106 +++++++++--------- 4 files changed, 81 insertions(+), 73 deletions(-) diff --git a/MVCarouselCollectionView/MVCarouselCell.swift b/MVCarouselCollectionView/MVCarouselCell.swift index 0bfe1ca..95c15b5 100644 --- a/MVCarouselCollectionView/MVCarouselCell.swift +++ b/MVCarouselCollectionView/MVCarouselCell.swift @@ -23,7 +23,7 @@ import UIKit public class MVCarouselCell: UICollectionViewCell { - + @IBOutlet weak var scrollView : MVCarouselCellScrollView! var cellSize : CGSize { get { @@ -58,7 +58,7 @@ public class MVCarouselCell: UICollectionViewCell { scrollView.imageLoader = newValue } } - + func resetZoom() { scrollView.resetZoom() } diff --git a/MVCarouselCollectionView/MVCarouselCell.xib b/MVCarouselCollectionView/MVCarouselCell.xib index 5a3ae2d..bea03be 100644 --- a/MVCarouselCollectionView/MVCarouselCell.xib +++ b/MVCarouselCollectionView/MVCarouselCell.xib @@ -1,8 +1,12 @@ - - + + + + + - + + @@ -11,10 +15,11 @@ - + - + + @@ -30,21 +35,24 @@ + + - + + diff --git a/MVCarouselCollectionView/MVCarouselCellScrollView.swift b/MVCarouselCollectionView/MVCarouselCellScrollView.swift index 5b825e0..8dbf7e5 100644 --- a/MVCarouselCollectionView/MVCarouselCellScrollView.swift +++ b/MVCarouselCollectionView/MVCarouselCellScrollView.swift @@ -23,18 +23,18 @@ import UIKit // Image loader closure type -public typealias MVImageLoaderClosure = ((imageView: UIImageView, imagePath : String, completion: (newImage: Bool) -> ()) -> ()) +public typealias MVImageLoaderClosure = ((_ imageView: UIImageView, _ imagePath : String, _ completion: (_ newImage: Bool) -> ()) -> ()) class MVCarouselCellScrollView: UIScrollView, UIScrollViewDelegate { - + let MaximumZoom = 4.0 - var cellSize : CGSize = CGSizeZero + var cellSize : CGSize = .zero var maximumZoom = 0.0 var imagePath : String = "" { didSet { assert(self.imageLoader != nil, "Image loader must be specified") - self.imageLoader?(imageView : self.imageView, imagePath: imagePath, completion: { + self.imageLoader?(self.imageView, imagePath, { (newImage) in self.resetZoom() }) @@ -43,14 +43,14 @@ class MVCarouselCellScrollView: UIScrollView, UIScrollViewDelegate { var imageLoader: MVImageLoaderClosure? @IBOutlet weak private var imageView : UIImageView! - + override func awakeFromNib() { super.awakeFromNib() self.delegate = self - self.imageView.contentMode = UIViewContentMode.ScaleAspectFit + self.imageView.contentMode = .scaleAspectFit } - + func resetZoom() { if self.imageView.image == nil { return @@ -58,14 +58,14 @@ class MVCarouselCellScrollView: UIScrollView, UIScrollViewDelegate { let imageSize = self.imageView.image!.size // nothing to do if image is not set - if CGSizeEqualToSize(imageSize, CGSizeZero) { + if imageSize.equalTo(.zero) { return } // Stack overflow people suggest this, not sure if it applies to us - self.imageView.contentMode = UIViewContentMode.Center + self.imageView.contentMode = UIViewContentMode.center if cellSize.width > imageSize.width && cellSize.height > imageSize.height { - self.imageView.contentMode = UIViewContentMode.ScaleAspectFit + self.imageView.contentMode = .scaleAspectFit } let cellAspectRatio : CGFloat = self.cellSize.width / self.cellSize.height @@ -85,14 +85,14 @@ class MVCarouselCellScrollView: UIScrollView, UIScrollViewDelegate { let horzContentInset = cellAspectRatioWiderThanImage ? 0.5 * (cellSize.width - adjustedContentWidth) : 0.0 let adjustedContentHeight = cellSize.width / imageAspectRatio let vertContentInset = !cellAspectRatioWiderThanImage ? 0.5 * (cellSize.height - adjustedContentHeight) : 0.0 - + self.contentInset = UIEdgeInsetsMake(vertContentInset, horzContentInset, vertContentInset, horzContentInset) } func zoomToUse() -> Double { return maximumZoom < 1.0 ? MaximumZoom : maximumZoom } - + func viewForZoomingInScrollView(scrollView : UIScrollView) -> UIView? { return self.imageView } diff --git a/MVCarouselCollectionView/MVCarouselCollectionView.swift b/MVCarouselCollectionView/MVCarouselCollectionView.swift index cb788e7..53fb190 100644 --- a/MVCarouselCollectionView/MVCarouselCollectionView.swift +++ b/MVCarouselCollectionView/MVCarouselCollectionView.swift @@ -30,61 +30,61 @@ import Foundation */ @objc public protocol MVCarouselCollectionViewDelegate { // method to provide a custom loader for a cell - optional func imageLoaderForCell(atIndexPath indexPath: NSIndexPath, imagePath: String) -> MVImageLoaderClosure - func carousel(carousel: MVCarouselCollectionView, didSelectCellAtIndexPath indexPath: NSIndexPath) - func carousel(carousel: MVCarouselCollectionView, didScrollToCellAtIndex cellIndex : NSInteger) + @objc optional func imageLoaderForCell(at indexPath: IndexPath, imagePath: String) -> MVImageLoaderClosure + func carousel(carousel: MVCarouselCollectionView, didSelectCellAt indexPath: IndexPath) + func carousel(carousel: MVCarouselCollectionView, didScrollToCellAt cellIndex : NSInteger) } public class MVCarouselCollectionView: UICollectionView, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { - + private let reuseID = "SomeReuseID" - + // MARK: Variables public var imagePaths : [String] = [] public var selectDelegate : MVCarouselCollectionViewDelegate? public var currentPageIndex : Int = 0 public var maximumZoom : Double = 0.0 - + // Default clousure used to load images public var commonImageLoader: MVImageLoaderClosure? - + // Trick to avoid updating the page index more than necessary private var clientDidRequestScroll : Bool = false - + // MARK: Initialisation override public func awakeFromNib() { super.awakeFromNib() - + self.delegate = self self.dataSource = self - + // Loading bundle from class, see: http://stackoverflow.com/questions/25138989/uicollectionview-nib-from-a-framework-target-registered-as-a-cell-fails-at-runt - let bundle = NSBundle(forClass: MVCarouselCell.self) + let bundle = Bundle(for: MVCarouselCell.self) let nib = UINib(nibName : "MVCarouselCell", bundle: bundle) - self.registerNib(nib, forCellWithReuseIdentifier: self.reuseID) + self.register(nib, forCellWithReuseIdentifier: self.reuseID) } - + // MARK: UICollectionViewDataSource - public func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { + public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.imagePaths.count } - - public func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { - + + public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { + // Should be set at this point assert(commonImageLoader != nil) - - let cell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseID, forIndexPath: indexPath) as! MVCarouselCell + + let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.reuseID, for: indexPath) as! MVCarouselCell cell.cellSize = self.bounds.size - + // Pass the closure to the cell let imagePath = self.imagePaths[indexPath.row] - let loader = self.selectDelegate?.imageLoaderForCell?(atIndexPath: indexPath, imagePath: imagePath) + let loader = self.selectDelegate?.imageLoaderForCell?(at: indexPath, imagePath: imagePath) cell.imageLoader = loader != nil ? loader : self.commonImageLoader // Set image path, which will call closure cell.imagePath = imagePath cell.maximumZoom = maximumZoom - + // http://stackoverflow.com/questions/16960556/how-to-zoom-a-uiscrollview-inside-of-a-uicollectionviewcell if let gestureRecognizer = cell.scrollView.pinchGestureRecognizer { self.addGestureRecognizer(gestureRecognizer) @@ -92,12 +92,12 @@ public class MVCarouselCollectionView: UICollectionView, UICollectionViewDataSou if let gestureRecognizer = cell.scrollView?.panGestureRecognizer { self.addGestureRecognizer(gestureRecognizer) } - + return cell } - - public func collectionView(collectionView : UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) { - + + public func collectionView(_ collectionView : UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { + if let cell = cell as? MVCarouselCell { // http://stackoverflow.com/questions/16960556/how-to-zoom-a-uiscrollview-inside-of-a-uicollectionviewcell if let gestureRecognizer = cell.scrollView?.pinchGestureRecognizer { @@ -108,56 +108,56 @@ public class MVCarouselCollectionView: UICollectionView, UICollectionViewDataSou } } } - + // MARK: UICollectionViewDelegateFlowLayout - public func collectionView(collectionView : UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { - + public func collectionView(_ collectionView : UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { + return self.superview!.bounds.size } - - public func collectionView(collectionView : UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { - self.selectDelegate?.carousel(self, didSelectCellAtIndexPath: indexPath) + + public func collectionView(_ collectionView : UICollectionView, didSelectItemAt indexPath: IndexPath) { + self.selectDelegate?.carousel(carousel: self, didSelectCellAt: indexPath) } - + // MARK: UIScrollViewDelegate - public func scrollViewDidScroll(scrollView: UIScrollView) { - + public func scrollViewDidScroll(_ scrollView: UIScrollView) { + if scrollView == self { if !self.clientDidRequestScroll { self.updatePageIndex() } } } - public func scrollViewDidEndDecelerating(scrollView: UIScrollView) { - + public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { + if scrollView == self { self.clientDidRequestScroll = false self.updatePageIndex() } } - public func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) { - + public func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) { + if scrollView == self { self.clientDidRequestScroll = false self.updatePageIndex() } } - + public func updatePageIndex() { let pageIndex = self.getPageNumber() if currentPageIndex != pageIndex { -// println("old page: \(currentPageIndex), new page: \(pageIndex)") + // println("old page: \(currentPageIndex), new page: \(pageIndex)") currentPageIndex = pageIndex - self.selectDelegate?.carousel(self, didScrollToCellAtIndex: pageIndex) + self.selectDelegate?.carousel(carousel: self, didScrollToCellAt: pageIndex) } } - + public func getPageNumber() -> NSInteger { - + // http://stackoverflow.com/questions/4132993/getting-the-current-page let width : CGFloat = self.frame.size.width var page : NSInteger = NSInteger((self.contentOffset.x + (CGFloat(0.5) * width)) / width) - let numPages = self.numberOfItemsInSection(0) + let numPages = self.numberOfItems(inSection: 0) if page < 0 { page = 0 } @@ -166,18 +166,18 @@ public class MVCarouselCollectionView: UICollectionView, UICollectionViewDataSou } return page } - - public func setCurrentPageIndex(pageIndex: Int, animated: Bool) { + + public func setCurrent(_ pageIndex: Int, animated: Bool) { self.currentPageIndex = pageIndex self.clientDidRequestScroll = true; - - let indexPath = NSIndexPath(forRow: currentPageIndex, inSection: 0) - self.scrollToItemAtIndexPath(indexPath, atScrollPosition:UICollectionViewScrollPosition.CenteredHorizontally, animated: animated) + + let indexPath = IndexPath(row: currentPageIndex, section: 0) + self.scrollToItem(at: indexPath, at:UICollectionViewScrollPosition.centeredHorizontally, animated: animated) } - - + + public func resetZoom() { - for cell in self.visibleCells() as! [MVCarouselCell] { + for cell in self.visibleCells as! [MVCarouselCell] { cell.resetZoom() } }