Skip to content

Instantly share code, notes, and snippets.

@benjaminsnorris
benjaminsnorris / Layout.m
Created February 28, 2015 18:27
Scroll collection view to always have cell centered in view
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity {
CGFloat offsetAdjustment = MAXFLOAT;
CGFloat horizontalCenter = proposedContentOffset.x + (CGRectGetWidth(self.collectionView.bounds) / 2.0);
CGRect targetRect = CGRectMake(proposedContentOffset.x, 0.0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
NSArray *attributesArray = [super layoutAttributesForElementsInRect:targetRect];
for (UICollectionViewLayoutAttributes *layoutAttributes in attributesArray) {
CGFloat itemHorizontalCenter = layoutAttributes.center.x;
if (ABS(itemHorizontalCenter - horizontalCenter) < ABS(offsetAdjustment)) {
@benjaminsnorris
benjaminsnorris / EventJSONSchema.json
Last active September 23, 2016 15:28
JSON Schema for Event API
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Event Response",
"type": "object",
"properties": {
"id": {
"description": "Unique identifier for an event",
"type": "integer"
},
"title": {
@benjaminsnorris
benjaminsnorris / CustomTabBar.swift
Last active December 17, 2015 23:02
Custom Tab Bar
//
// CustomTabBar.swift
// welbe
//
// Created by Ben Norris on 12/8/15.
// Copyright © 2015 O.C. Tanner Corporation. All rights reserved.
//
import UIKit
@benjaminsnorris
benjaminsnorris / ListViewController.swift
Created December 17, 2015 23:50
Page View Controller with Custom Tab Bar
//
// ListViewController.swift
// welbe
//
// Created by Ben Norris on 12/3/15.
// Copyright © 2015 O.C. Tanner Corporation. All rights reserved.
//
import UIKit
@benjaminsnorris
benjaminsnorris / GBClock.swift
Created January 20, 2016 18:19
Reference Time
class GBClock {
var referenceDate = NSDate(timeIntervalSinceReferenceDate: 0)
var referenceDateInterval: NSTimeInterval { referenceDate.timeIntervalSinceReferenceDate }
func currentTime() → NSDate {
return NSDate(timeIntervalSinceNow: referenceDateInterval)
}
}
@benjaminsnorris
benjaminsnorris / Preferences.swift
Created January 27, 2016 17:40
Preferences "wrapper" around NSUserDefaults
protocol Preferences: class {
var showNames: Bool { get set }
func registerDefaults(registrar: Preferences -> Void)
}
private class DefaultPreferenceValues: Preferences {
var showNames = false
func registerDefaults(registrar: Preferences -> Void) {
fatalError("The registrar cannot register defaults")
struct FlagEmoji {
private static let regionalIndicatorA: UInt32 = 0x1F1E6
enum Error: ErrorType {
case ExpectedOnlyAZ
}
private static func toRegionalIndicator(scalar: UnicodeScalar) throws -> UnicodeScalar {
let offset: UInt32
switch scalar {
@benjaminsnorris
benjaminsnorris / CollectionViewController.swift
Created March 10, 2016 03:00
Snap to center collection view cell
class CollectionViewController: UIViewController {
...
func snapToCenter() {
let centerPoint = view.convertPoint(view.center, toView: collectionView)
guard let centerIndexPath = collectionView.indexPathForItemAtPoint(centerPoint)
collectionView.scrollToItemAtIndexPath(centerIndexPath, atScrollPosition: .CenteredHorizontally, animated: true)
}
...
}
@benjaminsnorris
benjaminsnorris / KeychainWrapper.swift
Created April 21, 2016 22:34
Keychain Wrapper via Dave DeLong
import Security
/// A wrapper around the Keychain API.
public class Keychain {
private let name: String
private let accessGroup: String?
public init(name: String, accessGroup: String? = nil) {
self.name = name
// From Bart Whiteley
public protocol UnmarshalingFactory: Marshal.ValueType {
associatedtype ConvertibleType = Self
static func createFromObject(object: MarshaledObject) throws -> ConvertibleType
}
extension UnmarshalingFactory {
public static func value(object: Any) throws -> ConvertibleType {