Skip to content

Instantly share code, notes, and snippets.

@Hexfire
Hexfire / UIView+Xib
Created Jul 10, 2019
UIView.loadFromXib()
View UIView+Xib
import UIKit
extension UIView {
static func loadFromXib() -> UIView? {
let bundle = Bundle(for: self)
let nib = UINib(nibName: String(describing: self), bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil).first as? UIView
return view
}
@Hexfire
Hexfire / HTTPStatusCodes.swift
Created Dec 5, 2017 — forked from brennanMKE/HTTPStatusCodes.swift
Swift Enums for HTTP Status Codes
View HTTPStatusCodes.swift
enum HTTPStatusCodes: Int {
// 100 Informational
case Continue = 100
case SwitchingProtocols
case Processing
// 200 Success
case OK = 200
case Created
case Accepted
case NonAuthoritativeInformation
View uiimage-merge.swift
extension UIImage {
static func imageByMergingImages(topImage: UIImage, bottomImage: UIImage, scaleForTop: CGFloat = 1.0) -> UIImage {
let size = bottomImage.size
let container = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 2.0)
UIGraphicsGetCurrentContext()!.interpolationQuality = .high
bottomImage.draw(in: container)
let topWidth = size.width / scaleForTop
View AXUIElement_in_Swift_pert2.swift
import Cocoa
extension NSWorkspace {
class func frontmostApp() -> NSRunningApplication? {
return self.sharedWorkspace().frontmostApplication
}
class func runningApp(bundleIdentifier:NSString) -> NSRunningApplication? {
let runningApplications = NSWorkspace.sharedWorkspace().runningApplications
return runningApplications.filter({$0.bundleIdentifier == bundleIdentifier}).first
}
View AXUIElement_in_Swift.swift
import Cocoa
protocol AXUIProtocol {
func AXUIWindowArray(processIdentifier pid:pid_t) -> [AXUIElement]
func AXUIWindowArray(bundleIdentifier bid:NSString) -> [AXUIElement]
}
extension AXUIProtocol {
func AXUIWindowArray(processIdentifier pid:pid_t) -> [AXUIElement] {
let windowList : UnsafeMutablePointer<AnyObject?> = UnsafeMutablePointer<AnyObject?>.alloc(1)
@Hexfire
Hexfire / visibleViewController.swift
Created May 23, 2017
[Swift 3] Visible UIViewController
View visibleViewController.swift
extension UIApplication {
class func visibleViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return visibleViewController(nav.visibleViewController)
}
if let tab = base as? UITabBarController {
let moreNavigationController = tab.moreNavigationController
@Hexfire
Hexfire / osx-messagebox.swift
Last active Apr 24, 2017
macOS Modal NSAlert with completion handlers
View osx-messagebox.swift
// As Modal window:
func messageBox(_ title: String!, message: String! = nil,
firstOption: String? = nil, secondOption: String? = nil, thirdOption: String? = nil,
firstAction: (() -> ())? = nil, secondAction: (() -> ())? = nil, thirdAction: (() -> ())? = nil ) {
let alert = NSAlert()
alert.messageText = title ?? ""
alert.informativeText = message ?? ""
@Hexfire
Hexfire / ImageRotation.swift
Created Apr 19, 2017
Rotate NSImageView (UIImageView) around its center
View ImageRotation.swift
extension NSView { // UIView
// Set source/destination angle.
// Angle is set in radians (0..2π), hence 360* rotation = 2π/-2π
func spinClockwise(timeToRotate: Double) {
startRotation(angle: -1 * CGFloat.pi * 2.0, timeToRotate: timeToRotate)
}
func spinAntiClockwise(timeToRotate: Double) {
startRotation(angle: CGFloat.pi * 2.0, timeToRotate: timeToRotate)
You can’t perform that action at this time.