Skip to content

Instantly share code, notes, and snippets.

@dagronf
dagronf / DSFRestrictedPanGestureRecognizer.swift
Last active August 9, 2019 08:16
A swift pan gestureRecognizer that can discriminate in a particular direction. Useful for views requiring (for example) horizontal pan support nested inside a uiscrollview (for example)
//
// DSFRestrictedPanGestureRecognizer.swift
// Time Zones
//
// Created by Darren Ford on 30/6/18.
// Copyright © 2018 Darren Ford. All rights reserved.
//
import UIKit
import UIKit.UIGestureRecognizerSubclass
@dagronf
dagronf / DSFUnenabledButton.swift
Last active August 9, 2019 08:14
NSButton with interaction disabled
import Cocoa
class DSFUnenabledButton: NSButton {
var isUserInteractionEnabled = false
override func hitTest(_ point: NSPoint) -> NSView? {
return isUserInteractionEnabled ? super.hitTest(point) : nil
}
}
// FROM: https://www.objc.io/blog/2018/12/18/atomic-variables/
final class Atomic<A> {
private let queue = DispatchQueue(label: "Atomic serial queue")
private var _value: A
init(_ value: A) {
self._value = value
}
var value: A {
@dagronf
dagronf / String+DSFExtensions.swift
Last active August 9, 2019 08:18
Simple remove/replace characters in a swift string
import Foundation
extension String {
/// Returns a new string by removing any instances of the specified characters
///
/// - Parameter
/// - replacementChars: String containing the characters to replace
/// - Returns: a new string filtering out the specified characters
func removing(charactersIn replacementChars: String) -> String {
return self.filter { replacementChars.contains($0) == false }
@dagronf
dagronf / QueryExample.swift
Created February 10, 2019 00:53
How to use Spotlight searches in Swift (basic example)
let query = MDQueryCreate(kCFAllocatorDefault, "kMDItemContentType = com.apple.application-bundle" as CFString, nil, nil)
MDQueryExecute(query, CFOptionFlags(kMDQuerySynchronous.rawValue))
let count = MDQueryGetResultCount(query);
for i in 0 ..< count {
let rawPtr = MDQueryGetResultAtIndex(query, i)
let item = Unmanaged<MDItem>.fromOpaque(rawPtr!).takeUnretainedValue()
if let path = MDItemCopyAttribute(item, kMDItemPath) as? String {
print(path)
}
@dagronf
dagronf / README.swift
Last active April 16, 2021 22:26
macOS detecting contrast changes using notifications (NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification, NSWorkspace.accessibilityDisplayOptionsDidChangeNotification)
// NSWorkspace.accessibilityDisplayOptionsDidChangeNotification fires when option changes.
// The only thing is, it fires in NSWorkspace's notification center. Not in the default one.
///// Using a selector
NSWorkspace.shared.notificationCenter.addObserver(
self,
selector: #selector(accessibilityDisplayOptionsDidChange(_:)),
name: NSWorkspace.accessibilityDisplayOptionsDidChangeNotification,
object: NSWorkspace.shared)
@dagronf
dagronf / CAAnimation+blockCallback.swift
Last active July 30, 2024 13:37
CAAnimation extension with starting/completion callback blocks
//
// CAAnimation+BlockCallback.swift
//
import UIKit
//
// Modified from http://onedayitwillmake.com/blog/2016/06/caanimation-completion-callback-block/
// Updated for Swift 4 syntax
// All credit to the original author (Mario Gonzalez)
@dagronf
dagronf / UUID+DataExtension.swift
Last active April 29, 2024 21:56
Swift UUID -> Data code extension
extension UUID {
/// Errors thrown
public enum LoadError: Error {
case invalidUUIDData
}
/// Returns the raw bytes of the UUID as a Data object.
///
/// Note that this method assumes a specific memory layout (the layout on the device calling the method)
/// and as such may not be portable to other systems.
@dagronf
dagronf / NSImage+checkerboard.swift
Last active June 3, 2019 20:17
Generate an NSImage containing a checkerboard pattern for a specific size.
private extension NSImage {
static func checkerboardImage(ofSize: CGSize, color1: NSColor, color2: NSColor, checkSize: CGFloat = 8) -> NSImage? {
let width = NSNumber(value: Float(checkSize))
let center = CIVector(cgPoint: CGPoint(x: 0, y: 0))
let darkColor = CIColor(cgColor: color1.cgColor)
let lightColor = CIColor(cgColor: color2.cgColor)
let sharpness = NSNumber(value: 1.0)
guard let filter = CIFilter(name: "CICheckerboardGenerator") else {
return nil
@dagronf
dagronf / ImageConversion.swift
Last active October 21, 2024 20:06
Extensions for converting NSImage <-> CGImage <-> CIImage
extension NSImage {
/// Create a CIImage using the best representation available
///
/// - Returns: Converted image, or nil
func asCIImage() -> CIImage? {
if let cgImage = self.asCGImage() {
return CIImage(cgImage: cgImage)
}
return nil
}