Skip to content

Instantly share code, notes, and snippets.

View regnerjr's full-sized avatar

John Regner regnerjr

View GitHub Profile
@regnerjr
regnerjr / ch6UIViews.swift
Created August 30, 2014 17:13
BNR Chapter 6 - UIViews and Quartz Drawing
//
// AppDelegate.swift
// Hypnosister3
//
// Created by John Regner on 8/23/14.
// Copyright (c) 2014 In Your Dreams Software. All rights reserved.
//
import UIKit
@regnerjr
regnerjr / SwiftNotifications.swift
Created September 7, 2014 17:31
Handling Notifications in Swift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
let dev = UIDevice.currentDevice()
dev.beginGeneratingDeviceOrientationNotifications()
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
ErrorView.errorInView(self.view, animated: true)
}
}
//
// ViewController.swift
// error-view
//
// Created by John Regner on 11/1/14.
// Copyright (c) 2014 In Your Dreams Software. All rights reserved.
//
import UIKit
@regnerjr
regnerjr / pictPopBGView.swift
Created November 9, 2014 18:01
A custom PopoverBackgroundView - Which does not work. Errors is EXC_BAD_ACCESS when trying to access the arrowOffset or arrowDirection.
//
// pictPopBGView.swift
//
import UIKit
class pictPopBGView: UIPopoverBackgroundView {
override var arrowOffset: CGFloat {
get{
@regnerjr
regnerjr / gist:27b74811ce690021f27b
Created November 14, 2014 21:33
Horray Fixed in Xcode 6.1.1
//Class methods and initializers that satisfy protocol requirements now properly invoke subclass
//overrides when called in generic contexts. (18828217) For example:
protocol P {
class func foo()
}
class C: P {
class func foo() { println("C!") }
}
@regnerjr
regnerjr / gist:6960525a154671b76882
Last active August 29, 2015 14:15
animateTransition, Top and bottom halves slide to reveal next view underneath.
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
// Get transition context views and things
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIView *fromView = fromVC.view;
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *toView = toVC.view;
//here is where you do the transition
@regnerjr
regnerjr / Microfiche.Swift
Created March 15, 2015 14:41
A set of functions for Archiving Swift Collections.
import Swift
import Foundation
// convert input Collection<T> into an NSMutableArray<NSData>
// works with Array<T>, Set<T>, Dictionary<T,U>
func convertCollectionToArrayOfData<T: CollectionType>(collection: T) -> NSMutableArray {
return NSMutableArray(array: map(collection){
var mutableItem = $0
return NSData(bytes: &mutableItem, length: sizeof(T.Generator.Element.self))
})
@regnerjr
regnerjr / roundClock.swift
Last active August 29, 2015 14:17
A playground for calculating hours and minutes into fractional hours i.e. 4 hours and 12 minutes should be 4.2 hours.
/// A function for calculating the time in 0.1 hour increments
/// i.e. 1 hour and 6 min is 1.1hours, 3 hours 12 minutes is 3.2 hours
func calc(hours: Int, min: Int) -> Double {
let dbl = Double(min)
let roundedMin = (dbl / 60.0)
let flr = floor(roundedMin * 10 ) / 10
return Double(hours) + floor(roundedMin * 10) / 10
}
/// How to round a number?
//Turns out in swift Bool is a struct!
/// From the Docs. `import Swift`
/// A value type whose instances are either `true` or `false`.
struct Bool {
/// Default-initialize Boolean value to `false`.
init()
}