Skip to content

Instantly share code, notes, and snippets.

@hectormatos2011
Last active December 24, 2016 20:27
Show Gist options
  • Save hectormatos2011/05682ed2825c588aed92 to your computer and use it in GitHub Desktop.
Save hectormatos2011/05682ed2825c588aed92 to your computer and use it in GitHub Desktop.
UIGestureRecognizer+Closures.swift
//
// UIGestureRecognizer+Closures.swift
// Closures
//
// Created by Hector on 7/23/14.
// Copyright (c) 2014 Hector Matos. All rights reserved.
//
import Foundation
extension UIGestureRecognizer {
convenience init(trailingClosure: (() -> ())?) {
self.init()
let invokingTarget = InvokeTarget.sharedInstance
invokingTarget.trailingClosure = trailingClosure
self.addTarget(InvokeTarget.sharedInstance, action: "invokeTrailingClosure:")
}
convenience init(gestureClosure: ((gestureRecognizer: UIGestureRecognizer) -> ())?) {
self.init()
let invokingTarget = InvokeTarget.sharedInstance
invokingTarget.gestureClosure = gestureClosure
self.addTarget(InvokeTarget.sharedInstance, action: "invokeTrailingClosure:")
}
}
class InvokeTarget: NSObject {
class var sharedInstance: InvokeTarget {
struct Singleton {
static let instance = InvokeTarget()
}
return Singleton.instance
}
var trailingClosure: (() -> ())?
var gestureClosure: ((recognizer: UIGestureRecognizer) -> ())?
func invokeTrailingClosure(recognizer: UIGestureRecognizer) {
if let unwrappedTrailingTarget = trailingClosure {
unwrappedTrailingTarget()
} else if let unwrappedTrailingTarget = gestureClosure {
unwrappedTrailingTarget(recognizer: recognizer)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment