Skip to content

Instantly share code, notes, and snippets.

View jamesrochabrun's full-sized avatar
🇵🇪

James Rochabrun jamesrochabrun

🇵🇪
View GitHub Profile
@jamesrochabrun
jamesrochabrun / navigationfadeonscrolling.swift
Created November 9, 2019 23:10
Math to animate Navigation bar on Scrolling
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
let safeAreaTop = UIApplication.shared.windows.filter { $0.isKeyWindow }.first?.safeAreaInsets.top ?? 0
let navigationNeededOfset: CGFloat = safeAreaTop// + (navigationController?.navigationBar.frame.height ?? 0)
let offset = scrollView.contentOffset.y + navigationNeededOfset
let alpha = 1 - ((scrollView.contentOffset.y + navigationNeededOfset) / navigationNeededOfset)
// Use the alpha here
navigationController?.navigationBar.backgroundColor = .blue
navigationController?.navigationBar.transform = .init(translationX: 0, y: min(0, -offset))
class AbsoluteFrameAnimator: NSObject {
private var absoluteFrame: CGRect = CGRect.zero
init(duration: CGFloat) {
self.duration = duration
}
private let duration: CGFloat
@jamesrochabrun
jamesrochabrun / CollectionReusable.swift
Created June 24, 2019 15:32
Protocol Oriented Reuse Identifier.
protocol CollectionReusable {}
/// Disclaimer: From Apple UI engineer - its allow to force cast the cell in this method, if it fails its mostly another issue in the implementation.
/// MARK:- UITableView
extension CollectionReusable where Self: UITableViewCell {
@jamesrochabrun
jamesrochabrun / RecursiveEnumerations.swift
Created December 20, 2018 05:48
Recursive enumeration in Swift 4.2
import UIKit
// Helpers
// takes a list and checks if a given predicate is true for every element O(n)
func all<T>(_ xs: [T], predicate: (T) -> Bool) -> Bool {
for x in xs {
if !predicate(x) {
return false
}
@jamesrochabrun
jamesrochabrun / IntFromString.swift
Created November 18, 2018 02:03
Convert a String in to an Integer using a hashmap in Swift 4.2
func convert(_ textValue: String) -> Int{
var total = 0
var valueMap = [
"1" as Character: 1,
"2": 2,
"3": 3,
"4": 4,
"5": 5,
"6": 6,
func fadeHorizontalEdges(in collectionView: UICollectionView, modifier: CGFloat) {
let visibleCells = collectionView.visibleCells
guard !visibleCells.isEmpty else { return }
let firstCell = visibleCells.first!
let lastCell = visibleCells.last!
visibleCells.forEach { $0.alpha = 1 }
extension Optional {
var isNil: Bool {
return self == nil
}
var isNotNil: Bool {
return self != nil
}
@jamesrochabrun
jamesrochabrun / VNFaceObservation.extension.swift
Created May 18, 2018 00:12
VNFaceObservation extension for calculate the overall bounding box that contains face observation bounding boxes.
extension VNFaceObservation {
static func overAllBoundingBoxFrom(boundingBoxes: [CGRect], tolerance: CGFloat) -> CGRect {
/// Sort Max X coordinates
let originXCoordinates = boundingBoxes.map { $0.minX }
/// Sort Max Y coordinates
let originYCoordinates = boundingBoxes.map { $0.minY }
/// Find overall minX
extension UICollectionView {
func dynamicGrid(numbersOfSquares :Float) -> (rows: Float, columns: Float, cellSize: CGSize) {
let ratio: Float = Float(self.frame.size.width / self.frame.size.height)
let numberOfColumns = sqrt(numbersOfSquares * ratio)
let numberOfRows = numbersOfSquares / numberOfColumns;
// Find best option filling the whole height
@jamesrochabrun
jamesrochabrun / PHAsset+Extension.swift
Created May 6, 2018 05:25
PHAsset extension that returns an array of assets in a time period related to an asset.
extension PHAsset {
// MARK: This returns an array of alternate assets from a PHAsset
func getAlternatePhotos() -> [PHAsset] {
/// get the collection of the asset to avoid fetching all photos in the library
let collectionFetchResult = PHAssetCollection.fetchAssetCollectionsContaining(self, with: .moment, options: nil)
let options = PHFetchOptions()
options.sortDescriptors = [NSSortDescriptor.init(key: "creationDate", ascending: true)]