This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import MSPeekCollectionViewDelegateImplementation | |
class ViewController: UIViewController { | |
@IBOutlet weak var collectionView: UICollectionView! | |
let delegate = MSPeekCollectionViewDelegateImplementation() | |
override func viewDidLoad() { | |
super.viewDidLoad() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { | |
let leftAndRightInsets = cellSpacing + cellPeekWidth | |
return UIEdgeInsets(top: 0, left: leftAndRightInsets, bottom: 0, right: leftAndRightInsets) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { | |
let itemWidth = max(0, collectionView.frame.size.width - 2 * (cellSpacing + cellPeekWidth)) | |
return CGSize(width: itemWidth, height: collectionView.frame.size.height) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { | |
return 0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { | |
currentScrollOffset = scrollView.contentOffset | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { | |
let target = targetContentOffset.pointee | |
//Current scroll distance is the distance between where the user tapped and the destination for the scrolling (If the velocity is high, this might be of big magnitude) | |
let currentScrollDistance = target.x - currentScrollOffset.x | |
//Make the value an integer between -1 and 1 (Because we don't want to scroll more than one item at a time) | |
let coefficent = Int(max(-1, min(currentScrollDistance/scrollThreshold, 1))) | |
let currentIndex = Int(round(currentScrollOffset.x/itemWidth)) | |
let adjacentItemIndex = currentIndex + coefficent | |
let adjacentItemIndexFloat = CGFloat(adjacentItemIndex) | |
let adjacentItemOffsetX = adjacentItemIndexFloat * (itemWidth(scrollView) + cellSpacing) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { | |
return cellSpacing | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import MSAutoView | |
class ListingView: MSAutoView { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ListingView: UIView { | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
initView() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
initView() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Step 1 | |
guard let xibItems = Bundle.main.loadNibNamed("ListingView", owner: self, options: nil), let firstXibView = xibItems[0] as? UIView else { | |
fatalError("Xib is empty or first view is not a UIView") | |
} | |
// Step 2 | |
firstXibView.translatesAutoresizingMaskIntoConstraints = false | |
// Step 3 | |
let viewConstraints = [NSLayoutAttribute.top, NSLayoutAttribute.left, NSLayoutAttribute.bottom, NSLayoutAttribute.right].map { (attribute) -> NSLayoutConstraint in | |
return NSLayoutConstraint(item: firstXibView, attribute: attribute, relatedBy: .equal, toItem: self, attribute: attribute, multiplier: 1, constant: 0) |
OlderNewer