Skip to content

Instantly share code, notes, and snippets.

@juliengdt
Last active August 29, 2015 14:25
Show Gist options
  • Save juliengdt/a80deda0ed2240b4d347 to your computer and use it in GitHub Desktop.
Save juliengdt/a80deda0ed2240b4d347 to your computer and use it in GitHub Desktop.
ClosureMiniKit
//
// 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