Skip to content

Instantly share code, notes, and snippets.

@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")
@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 / 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 / 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 / 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 / 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 / CustomView.h
Last active August 29, 2015 14:15
Filling Segmented Custom View
//
// FillingSegmentedCircleView.h
// Created by Ben Norris on 2/20/15.
// Copyright (c) 2015 BSN Design. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface FillingSegmentedCircleView : UIView
@benjaminsnorris
benjaminsnorris / DataSource.h
Last active August 29, 2015 14:14
TableView FetchedResultsController
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface DXListTableViewDataSource : NSObject <UITableViewDataSource>
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
- (void)registerTableView:(UITableView *)tableView;
- (void)configureFetchedResultsControllerForTableView:(UITableView *)tableView;
@benjaminsnorris
benjaminsnorris / GetPersonInfo.m
Created January 10, 2015 16:23
Get multiple values from selected contacted
+ (NSDictionary *)getMultipleValuesForPerson:(ABRecordRef)person forProperty:(ABPropertyID)property sanitizeToNumbers:(BOOL)sanitize {
NSMutableDictionary *mutableValuesWithLabels = [NSMutableDictionary dictionary];
ABMultiValueRef values = ABRecordCopyValue(person, property);
if (values && ABMultiValueGetCount(values) > 0)
{
for (CFIndex index = 0; index < ABMultiValueGetCount(values); index++) {
NSString *label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(values, index));
NSString *value = CFBridgingRelease(ABMultiValueCopyValueAtIndex(values, index));
if (sanitize) {
value = [[value componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
@benjaminsnorris
benjaminsnorris / ViewController.m
Created December 30, 2014 18:32
NavController Toolbar
- (void)setUpBottomToolbar {
self.navigationController.toolbar.tintColor = [UIColor orangeColor];
self.navigationController.toolbarHidden = NO;
self.viewPlayOrHistory = [[UISegmentedControl alloc] initWithItems:@[@"Standard", @"History"]];
self.viewPlayOrHistory.selectedSegmentIndex = 0;
UIBarButtonItem *viewSwitchButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.viewPlayOrHistory];
[self.viewPlayOrHistory addTarget:self action:@selector(switchViewToPlayOrHistory) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];