Last active
December 24, 2016 20:27
-
-
Save hectormatos2011/05682ed2825c588aed92 to your computer and use it in GitHub Desktop.
UIGestureRecognizer+Closures.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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