Last active
August 29, 2015 14:25
-
-
Save juliengdt/a80deda0ed2240b4d347 to your computer and use it in GitHub Desktop.
ClosureMiniKit
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
// | |
// UIViewExtension.swift | |
// SwiftTester | |
// | |
// Created by JulienGdt on 07/07/15. | |
// Copyright (c) 2015 JulienGdt @jlngdt. All rights reserved. | |
// @see https://gist.github.com/juliengdt/a80deda0ed2240b4d347 | |
// | |
import UIKit | |
var blockAction: (() -> ()) = {} | |
extension UIView { | |
/** | |
Call this function to add a selector on touch. Prefer @whenTapped or @whenDoubleTapped | |
:param: touchNumbers the given number of touchs needed to interact with | |
:param: tapNumbers the given number of taps needed to interact with | |
*/ | |
func whenTouch(NumberOfTouch touchNumbers: Int,NumberOfTaps tapNumbers: Int) -> Void { | |
let tapGesture = UITapGestureRecognizer() | |
tapGesture.numberOfTouchesRequired = touchNumbers | |
tapGesture.numberOfTapsRequired = tapNumbers | |
tapGesture.addTarget(self, action: "tapActions") | |
self.addGestureRecognizer(tapGesture) | |
} | |
/** | |
Call this action to add a target on Tap of the given view | |
:param: action The closure called on Tap | |
*/ | |
func whenTapped(action :(() -> Void)) { | |
self.whenTouch(NumberOfTouch: 1, NumberOfTaps: 1) | |
blockAction = action | |
} | |
/** | |
Call this action to add a target on double tap of the given view | |
:param: action The closure called on double tap | |
*/ | |
func whenDoubleTapped(action :(() -> Void)) { | |
self.whenTouch(NumberOfTouch: 1, NumberOfTaps: 2) | |
blockAction = action | |
} | |
func tapActions() { | |
blockAction() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment