Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. import UIKit
  2.  
  3. final public class HideKeyboardTapGestureManager {
  4.  
  5. // MARK: - Private
  6. public private (set) var targets: Set<UIView> = Set<UIView>() {
  7. didSet {
  8. targets.subtracting(oldValue).forEach {addGesture(to: $0)}
  9. }
  10. }
  11.  
  12. private func addGesture(to target: UIView) {
  13. let gesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
  14. target.addGestureRecognizer(gesture)
  15. }
  16.  
  17. @objc private func dismissKeyboard() {
  18. targets.first?.superview?.endEditing(true)
  19. }
  20.  
  21. // MARK: - Public
  22. public func add(targets: [UIView]) {
  23. self.targets = self.targets.union(Set(targets))
  24. }
  25.  
  26. public func remove(_ target: UIView) {
  27. target.gestureRecognizers?.forEach {target.removeGestureRecognizer($0)}
  28. self.targets.remove(target)
  29. }
  30.  
  31. public func remove(_ targets: [UIView]) {
  32. targets.forEach {self.remove($0)}
  33. }
  34.  
  35. public func removeAllTargets() {
  36. self.remove(Array(self.targets))
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement