Skip to content

Instantly share code, notes, and snippets.

@maxcampolo
maxcampolo / EquatableProtocol.swift
Created March 15, 2016 19:56
Implementation of the Equatable protocol in Swift on a custom object.
public class MyCustomObject: Equatable {}
// MARK: Equatable
/**
Evaluate equality as an identity check on an ObjectIdentifier constructed with an instance of our type. This is a public function in the global scope.
*/
public func ==(lhs: MyCustomObject, rhs: MyCustomObject) -> Bool {
return ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
}
@maxcampolo
maxcampolo / UIView+PassThrough.swift
Last active December 14, 2020 11:07
UIView subclass that is transparent to all touch events besides those on eligible child views.
import UIKit
/**
UIView subclass that is transparent to all touch events besides those on eligible child views.
*/
class TKPassThroughView: UIView {
// MARK - Touch Handling
@maxcampolo
maxcampolo / DropShadow.swift
Created March 4, 2016 19:45
Add a drop shadow to a UIView
let shadowPath = UIBezierPath(rect: self.myView.layer.bounds)
self.myView.layer.masksToBounds = false
self.myView.layer.shadowColor = UIColor.blackColor().CGColor
self.myView.layer.shadowOffset = CGSizeMake(0.0, -2.0)
self.myView.layer.shadowOpacity = 0.3
self.myView.layer.shadowRadius = 1.0
self.myView.layer.shadowPath = shadowPath.CGPath
@maxcampolo
maxcampolo / UIColor+Hex.swift
Created February 18, 2016 20:54
UIColor extension for returning UIColor from hex code
// Initialize a color from hex string with format #RRGGBB
extension UIColor {
convenience init(hex: String, alpha: CGFloat = 1) {
assert(hex[hex.startIndex] == "#", "Expected hex string of format #RRGGBB")
let scanner = NSScanner(string: hex)
scanner.scanLocation = 1 // skip #
var rgb: UInt32 = 0
scanner.scanHexInt(&rgb)
@maxcampolo
maxcampolo / CALayer+BorderColor.swift
Created January 19, 2016 22:35
CALayer extension that adds borderUIColor as an attribute that can be set by key path in interface builder.
extension CALayer {
func borderUIColor() -> UIColor? {
return borderColor != nil ? UIColor(CGColor: borderColor!) : nil
}
func setBorderUIColor(color: UIColor) {
borderColor = color.CGColor
}
}
@maxcampolo
maxcampolo / AnimatedStatusBar.swift
Created January 11, 2016 19:36
Animate the application status bar
func animateStatusBarHidden(hidden: Bool) {
let key = NSString(data: NSData(bytes: [0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x42, 0x61, 0x72] as [CUnsignedChar], length: 9), encoding: NSASCIIStringEncoding)
let object = UIApplication.sharedApplication()
var statusBar: UIView?
if let mKey = key as? String {
if object.respondsToSelector(NSSelectorFromString(mKey)) {
statusBar = object.valueForKey(mKey) as? UIView
}
}
@maxcampolo
maxcampolo / UIViewController+Tag.swift
Last active July 4, 2023 07:12
Extension that adds a tag property to UIViewController
/*
*
* This class provides a UIViewController extension which adds a tag property to all UIViewController subclasses.
*
*/
import Foundation
private var tagAssociationKey: UInt8 = 0