Skip to content

Commit

Permalink
General do, UIButton.font, UIGestureRecognizer.action with object
Browse files Browse the repository at this point in the history
  • Loading branch information
wordlessj committed Nov 3, 2016
1 parent aa772c7 commit b745e54
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 7 deletions.
8 changes: 8 additions & 0 deletions Panda.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
3BB2EEA11DC9CE7E001D7AAB /* UIViewControllerExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BB2EEA01DC9CE7E001D7AAB /* UIViewControllerExtensions.swift */; };
3BB2EEAB1DC9E891001D7AAB /* Font.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BB2EEAA1DC9E891001D7AAB /* Font.swift */; };
3BB2EEAD1DC9E967001D7AAB /* UIFontExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BB2EEAC1DC9E967001D7AAB /* UIFontExtensions.swift */; };
3BB2EEAF1DCA1E67001D7AAB /* Do.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BB2EEAE1DCA1E67001D7AAB /* Do.swift */; };
3BB2EEB11DCA24B4001D7AAB /* UIButtonExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BB2EEB01DCA24B4001D7AAB /* UIButtonExtensions.swift */; };
3BF5F3451DC8CE3C0037C598 /* UIButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BF5F3441DC8CE3C0037C598 /* UIButton.swift */; };
3BF5F3471DC8D1120037C598 /* UILabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BF5F3461DC8D1120037C598 /* UILabel.swift */; };
3BF5F3491DC8D5600037C598 /* UITextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BF5F3481DC8D5600037C598 /* UITextField.swift */; };
Expand Down Expand Up @@ -56,6 +58,8 @@
3BB2EEA01DC9CE7E001D7AAB /* UIViewControllerExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIViewControllerExtensions.swift; sourceTree = "<group>"; };
3BB2EEAA1DC9E891001D7AAB /* Font.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Font.swift; sourceTree = "<group>"; };
3BB2EEAC1DC9E967001D7AAB /* UIFontExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIFontExtensions.swift; sourceTree = "<group>"; };
3BB2EEAE1DCA1E67001D7AAB /* Do.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Do.swift; sourceTree = "<group>"; };
3BB2EEB01DCA24B4001D7AAB /* UIButtonExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIButtonExtensions.swift; sourceTree = "<group>"; };
3BF5F3441DC8CE3C0037C598 /* UIButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIButton.swift; sourceTree = "<group>"; };
3BF5F3461DC8D1120037C598 /* UILabel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UILabel.swift; sourceTree = "<group>"; };
3BF5F3481DC8D5600037C598 /* UITextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UITextField.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -118,10 +122,12 @@
isa = PBXGroup;
children = (
3B2B901F1DC8530800B4E92F /* Actions.swift */,
3BB2EEAE1DCA1E67001D7AAB /* Do.swift */,
3BB2EEAA1DC9E891001D7AAB /* Font.swift */,
3BB2EE9A1DC9C3E2001D7AAB /* CGAffineTransformExtensions.swift */,
3BB2EE9C1DC9C5DE001D7AAB /* CGContextExtensions.swift */,
3BB2EE981DC9C2B1001D7AAB /* CGGeometryExtensions.swift */,
3BB2EEB01DCA24B4001D7AAB /* UIButtonExtensions.swift */,
3BB2EE8E1DC99F0E001D7AAB /* UICollectionViewExtensions.swift */,
3BB2EE9E1DC9C6B1001D7AAB /* UIEdgeInsetsExtensions.swift */,
3BB2EEAC1DC9E967001D7AAB /* UIFontExtensions.swift */,
Expand Down Expand Up @@ -247,7 +253,9 @@
3B2B90201DC8530800B4E92F /* Actions.swift in Sources */,
3BF5F3471DC8D1120037C598 /* UILabel.swift in Sources */,
3BB2EE911DC9B65B001D7AAB /* UICollectionViewFlowLayout.swift in Sources */,
3BB2EEB11DCA24B4001D7AAB /* UIButtonExtensions.swift in Sources */,
3BF5F3491DC8D5600037C598 /* UITextField.swift in Sources */,
3BB2EEAF1DCA1E67001D7AAB /* Do.swift in Sources */,
3BF5F34D1DC8DB220037C598 /* UITextView.swift in Sources */,
3B2B901E1DC8519D00B4E92F /* UIControl.swift in Sources */,
3BB2EE991DC9C2B1001D7AAB /* CGGeometryExtensions.swift in Sources */,
Expand Down
34 changes: 27 additions & 7 deletions Panda/Actions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,45 @@ extension UIControl {
}
}

extension UIGestureRecognizer {
private var actionBox: ActionBox? {
get { return objc_getAssociatedObject(self, &actionBoxKey) as? ActionBox }
private class ObjectActionBox {
static let triggerSelector = #selector(trigger)

unowned let object: AnyObject
let action: (AnyObject) -> ()

init(object: AnyObject, action: @escaping (AnyObject) -> ()) {
self.object = object
self.action = action
}

@objc func trigger() {
action(object)
}
}

public protocol GestureRecognizer: class {}

extension GestureRecognizer where Self: UIGestureRecognizer {
private var actionBox: ObjectActionBox? {
get { return objc_getAssociatedObject(self, &actionBoxKey) as? ObjectActionBox }
set { objc_setAssociatedObject(self, &actionBoxKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
}

@discardableResult
public func action(_ action: (() -> ())?) -> Self {
public func action(_ action: ((Self) -> ())?) -> Self {
if let box = actionBox {
removeTarget(box, action: ActionBox.triggerSelector)
removeTarget(box, action: ObjectActionBox.triggerSelector)
actionBox = nil
}

if let action = action {
let box = ActionBox(action: action)
addTarget(box, action: ActionBox.triggerSelector)
let box = ObjectActionBox(object: self, action: action as! (AnyObject) -> ())
addTarget(box, action: ObjectActionBox.triggerSelector)
actionBox = box
}

return self
}
}

extension UIGestureRecognizer: GestureRecognizer {}
21 changes: 21 additions & 0 deletions Panda/Do.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Do.swift
// Panda
//
// Created by Javier on 11/2/16.
// Copyright © 2016 Javier. All rights reserved.
//

import Foundation

public protocol Doable {}

extension Doable {
@discardableResult
public func `do`(_ action: (Self) -> ()) -> Self {
action(self)
return self
}
}

extension NSObject: Doable {}
1 change: 1 addition & 0 deletions Panda/Font.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ extension FontContainer {
}
}

extension UIButton: FontContainer {}
extension UILabel: FontContainer {}
extension UITextField: FontContainer {}
extension UITextView: FontContainer {}
17 changes: 17 additions & 0 deletions Panda/UIButtonExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// UIButtonExtensions.swift
// Panda
//
// Created by Javier on 11/2/16.
// Copyright © 2016 Javier. All rights reserved.
//

import UIKit

extension UIButton {
@discardableResult
public func font(_ value: UIFont?) -> Self {
titleLabel?.font = value
return self
}
}

0 comments on commit b745e54

Please sign in to comment.