Skip to content

Instantly share code, notes, and snippets.

View MaherKSantina's full-sized avatar
🎯
Focusing

Maher Santina MaherKSantina

🎯
Focusing
  • New South Wales, Australia
View GitHub Profile
@MaherKSantina
MaherKSantina / snippet.swift
Last active July 24, 2018 08:19
MSPCVDI Complete Example
import UIKit
import MSPeekCollectionViewDelegateImplementation
class ViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
let delegate = MSPeekCollectionViewDelegateImplementation()
override func viewDidLoad() {
super.viewDidLoad()
@MaherKSantina
MaherKSantina / snippet.swift
Created July 24, 2018 08:22
MSPCVDI Snippet 1
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)
}
@MaherKSantina
MaherKSantina / snippet.swift
Created July 24, 2018 08:24
MSPCVDI Snippet 2
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)
}
@MaherKSantina
MaherKSantina / snippet.swift
Created July 24, 2018 08:25
MSPCVDI Snippet 3
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
@MaherKSantina
MaherKSantina / snippet.swift
Created July 24, 2018 08:26
MSPCVDI Snippet 4
public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
currentScrollOffset = scrollView.contentOffset
}
@MaherKSantina
MaherKSantina / snippet.swift
Created July 24, 2018 08:28
MSPCVDI Snippet 5
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)
@MaherKSantina
MaherKSantina / snippet.swift
Created July 24, 2018 08:33
MSPCVDI Snippet 0
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return cellSpacing
}
import UIKit
import MSAutoView
class ListingView: MSAutoView { }
class ListingView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
initView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initView()
// 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)