Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to swift 4 #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions MVCarouselCollectionView/MVCarouselCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import UIKit

public class MVCarouselCell: UICollectionViewCell {

@IBOutlet weak var scrollView : MVCarouselCellScrollView!
var cellSize : CGSize {
get {
Expand Down Expand Up @@ -58,7 +58,7 @@ public class MVCarouselCell: UICollectionViewCell {
scrollView.imageLoader = newValue
}
}

func resetZoom() {
scrollView.resetZoom()
}
Expand Down
20 changes: 14 additions & 6 deletions MVCarouselCollectionView/MVCarouselCell.xib
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="9531" systemVersion="15B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina5_5" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
Expand All @@ -11,10 +15,11 @@
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" ambiguous="YES" showsHorizontalScrollIndicator="NO" showsVerticalScrollIndicator="NO" translatesAutoresizingMaskIntoConstraints="NO" id="bSM-bD-wC3" customClass="MVCarouselCellScrollView" customModule="MVCarouselCollectionView">
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" showsHorizontalScrollIndicator="NO" showsVerticalScrollIndicator="NO" translatesAutoresizingMaskIntoConstraints="NO" id="bSM-bD-wC3" customClass="MVCarouselCellScrollView" customModule="MVCarouselCollectionView">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" ambiguous="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Pey-Zt-VKe">
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="Pey-Zt-VKe">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<constraints>
<constraint firstAttribute="height" id="Uyb-Bs-mDb"/>
<constraint firstAttribute="width" id="azf-WB-O7i"/>
Expand All @@ -30,21 +35,24 @@
<constraints>
<constraint firstAttribute="bottom" secondItem="Pey-Zt-VKe" secondAttribute="bottom" id="LPd-LF-t6b"/>
<constraint firstAttribute="trailing" secondItem="Pey-Zt-VKe" secondAttribute="trailing" id="lem-qo-oGC"/>
<constraint firstItem="Pey-Zt-VKe" firstAttribute="centerY" secondItem="bSM-bD-wC3" secondAttribute="centerY" id="lt4-hu-WZz"/>
<constraint firstItem="Pey-Zt-VKe" firstAttribute="leading" secondItem="bSM-bD-wC3" secondAttribute="leading" id="oLf-jy-Y3M"/>
<constraint firstItem="Pey-Zt-VKe" firstAttribute="top" secondItem="bSM-bD-wC3" secondAttribute="top" id="qcI-xQ-iYt"/>
<constraint firstItem="Pey-Zt-VKe" firstAttribute="centerX" secondItem="bSM-bD-wC3" secondAttribute="centerX" id="tbR-az-lNt"/>
</constraints>
<connections>
<outlet property="imageView" destination="Pey-Zt-VKe" id="peH-Md-aPV"/>
</connections>
</scrollView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="bottom" secondItem="bSM-bD-wC3" secondAttribute="bottom" id="B75-M8-A7w"/>
<constraint firstAttribute="trailing" secondItem="bSM-bD-wC3" secondAttribute="trailing" id="F7E-3S-4Fs"/>
<constraint firstItem="bSM-bD-wC3" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" id="HX3-7V-bLQ"/>
<constraint firstItem="bSM-bD-wC3" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" id="Zh9-zI-DLe"/>
</constraints>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<connections>
<outlet property="scrollView" destination="bSM-bD-wC3" id="5hx-En-8dS"/>
</connections>
Expand Down
24 changes: 12 additions & 12 deletions MVCarouselCollectionView/MVCarouselCellScrollView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand All @@ -43,29 +43,29 @@ 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
}
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
Expand All @@ -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
}
Expand Down
106 changes: 53 additions & 53 deletions MVCarouselCollectionView/MVCarouselCollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,74 +30,74 @@ 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)
}
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 {
Expand All @@ -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
}
Expand All @@ -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()
}
}
Expand Down