Skip to content

Instantly share code, notes, and snippets.

@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
@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 / 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 / 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 / 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 / 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 / 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 / ContainedClosure.swift
Created April 1, 2016 19:26
Call a function from within another function as a closure.
public someFunction() {
({ self.someStringVariable = "Some value" })()
}
@maxcampolo
maxcampolo / SwiftSynchronized.swift
Created April 19, 2016 19:07
Mutual exclusion in Swift (equivalent to @synchronized in obj-c)
/**
Function that locks an object while it is being acted upon in the closure parameter.
Rethrows allows the function to throw an error for throwing closures or not for non-throwing closures (do not need `try` for non-throwing closures).
parameter lock: Object to be sync'd
parameter closure: Code of critical section
*/
public func synchronized<T>(lock: AnyObject, @noescape closure: () throws -> T) rethrows -> T {
objc_sync_enter(lock)
defer {
objc_sync_exit(lock)
@maxcampolo
maxcampolo / PerformSelector.swift
Created April 28, 2016 16:01
Performing selector after delay and cancelling previous perform requests in Swift.
// Perform selector
self.performSelector(#selector(MyViewController.myFunction(_:)), withObject: myObject, afterDelay: 1.0)
// Cancel perform requests
self.classForCoder.cancelPreviousPerformRequestsWithTarget(self, selector: #selector(MyViewController.myFunction(_:)), object: myObject)
// Note: -
// myObject can be nil