├── .gitignore
├── Demo
├── Demo
│ ├── AppDelegate.swift
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Base.lproj
│ │ ├── LaunchScreen.storyboard
│ │ └── Main.storyboard
│ ├── Info.plist
│ └── ViewController.swift
└── Selector-Closure.Demo.xcodeproj
│ ├── project.pbxproj
│ └── project.xcworkspace
│ └── contents.xcworkspacedata
├── LICENSE
├── README.md
└── Source
├── SCExtension.swift
├── UIBarButtonItem+SCExtension.swift
├── UIControl+SCExtension.swift
├── UIGestureRecognizer+SCExtension.swift
└── UIView+SCExtension.swift
/.gitignore:
--------------------------------------------------------------------------------
1 | # Xcode
2 | #
3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
4 |
5 | ## Build generated
6 | build/
7 | DerivedData/
8 |
9 | ## Various settings
10 | *.pbxuser
11 | !default.pbxuser
12 | *.mode1v3
13 | !default.mode1v3
14 | *.mode2v3
15 | !default.mode2v3
16 | *.perspectivev3
17 | !default.perspectivev3
18 | xcuserdata/
19 |
20 | ## Other
21 | *.moved-aside
22 | *.xccheckout
23 | *.xcscmblueprint
24 |
25 | ## Obj-C/Swift specific
26 | *.hmap
27 | *.ipa
28 | *.dSYM.zip
29 | *.dSYM
30 |
31 | ## Playgrounds
32 | timeline.xctimeline
33 | playground.xcworkspace
34 |
35 | # Swift Package Manager
36 | #
37 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
38 | # Packages/
39 | # Package.pins
40 | .build/
41 |
42 | # CocoaPods
43 | #
44 | # We recommend against adding the Pods directory to your .gitignore. However
45 | # you should judge for yourself, the pros and cons are mentioned at:
46 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
47 | #
48 | # Pods/
49 |
50 | # Carthage
51 | #
52 | # Add this line if you want to avoid checking in source code from Carthage dependencies.
53 | # Carthage/Checkouts
54 |
55 | Carthage/Build
56 |
57 | # fastlane
58 | #
59 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
60 | # screenshots whenever they are needed.
61 | # For more information about the recommended setup visit:
62 | # https://docs.fastlane.tools/best-practices/source-control/#source-control
63 |
64 | fastlane/report.xml
65 | fastlane/Preview.html
66 | fastlane/screenshots
67 | fastlane/test_output
68 |
--------------------------------------------------------------------------------
/Demo/Demo/AppDelegate.swift:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.swift
3 | // UIKit.Closures.Demo
4 | //
5 | // Created by ray on 2017/12/22.
6 | // Copyright © 2017年 ray. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | @UIApplicationMain
12 | class AppDelegate: UIResponder, UIApplicationDelegate {
13 |
14 | var window: UIWindow?
15 |
16 |
17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
18 | // Override point for customization after application launch.
19 | return true
20 | }
21 |
22 | func applicationWillResignActive(_ application: UIApplication) {
23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
25 | }
26 |
27 | func applicationDidEnterBackground(_ application: UIApplication) {
28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
30 | }
31 |
32 | func applicationWillEnterForeground(_ application: UIApplication) {
33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
34 | }
35 |
36 | func applicationDidBecomeActive(_ application: UIApplication) {
37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
38 | }
39 |
40 | func applicationWillTerminate(_ application: UIApplication) {
41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
42 | }
43 |
44 |
45 | }
46 |
47 |
--------------------------------------------------------------------------------
/Demo/Demo/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "40x40",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "40x40",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "size" : "60x60",
36 | "scale" : "2x"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "size" : "60x60",
41 | "scale" : "3x"
42 | },
43 | {
44 | "idiom" : "ipad",
45 | "size" : "20x20",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "ipad",
50 | "size" : "20x20",
51 | "scale" : "2x"
52 | },
53 | {
54 | "idiom" : "ipad",
55 | "size" : "29x29",
56 | "scale" : "1x"
57 | },
58 | {
59 | "idiom" : "ipad",
60 | "size" : "29x29",
61 | "scale" : "2x"
62 | },
63 | {
64 | "idiom" : "ipad",
65 | "size" : "40x40",
66 | "scale" : "1x"
67 | },
68 | {
69 | "idiom" : "ipad",
70 | "size" : "40x40",
71 | "scale" : "2x"
72 | },
73 | {
74 | "idiom" : "ipad",
75 | "size" : "76x76",
76 | "scale" : "1x"
77 | },
78 | {
79 | "idiom" : "ipad",
80 | "size" : "76x76",
81 | "scale" : "2x"
82 | },
83 | {
84 | "idiom" : "ipad",
85 | "size" : "83.5x83.5",
86 | "scale" : "2x"
87 | },
88 | {
89 | "idiom" : "ios-marketing",
90 | "size" : "1024x1024",
91 | "scale" : "1x"
92 | }
93 | ],
94 | "info" : {
95 | "version" : 1,
96 | "author" : "xcode"
97 | }
98 | }
--------------------------------------------------------------------------------
/Demo/Demo/Base.lproj/LaunchScreen.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/Demo/Demo/Base.lproj/Main.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Demo/Demo/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 | LSRequiresIPhoneOS
22 |
23 | UILaunchStoryboardName
24 | LaunchScreen
25 | UIMainStoryboardFile
26 | Main
27 | UIRequiredDeviceCapabilities
28 |
29 | armv7
30 |
31 | UISupportedInterfaceOrientations
32 |
33 | UIInterfaceOrientationPortrait
34 | UIInterfaceOrientationLandscapeLeft
35 | UIInterfaceOrientationLandscapeRight
36 |
37 | UISupportedInterfaceOrientations~ipad
38 |
39 | UIInterfaceOrientationPortrait
40 | UIInterfaceOrientationPortraitUpsideDown
41 | UIInterfaceOrientationLandscapeLeft
42 | UIInterfaceOrientationLandscapeRight
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/Demo/Demo/ViewController.swift:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.swift
3 | // UIKit.Closures.Demo
4 | //
5 | // Created by ray on 2017/12/22.
6 | // Copyright © 2017年 ray. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 |
12 |
13 | class RootViewController: UIViewController {
14 |
15 | override func viewDidLoad() {
16 | super.viewDidLoad()
17 |
18 | // MARK: UIButton
19 |
20 | let btn = UIButton(type: .roundedRect)
21 | btn.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30)
22 | btn.setTitle("btn", for: .normal)
23 | btn.backgroundColor = .red
24 | self.view.addSubview(btn)
25 | _ = btn.sce.add {
26 | $0.setTitle("has tapped", for: .normal)
27 | $0.sizeToFit()
28 | }
29 | let invoker = btn.sce.add { sender in
30 | print("another one")
31 | }
32 | btn.sce.remove(invoker) // "another one" never print
33 |
34 | // MARK: UITextField
35 |
36 | let field = UITextField.init(frame: CGRect.init(x: 0, y: 50, width: 100, height: 30))
37 | field.backgroundColor = .yellow
38 | self.view.addSubview(field)
39 | _ = field.sce.add { field in
40 | if let text = field.text {
41 | print(text)
42 | }
43 | }
44 |
45 | // MARK: UIBarButtonItem
46 |
47 | self.navigationItem.rightBarButtonItem = UIBarButtonItem.SCE.initialize(title: "test", style: .plain) {
48 | print("rightBarButtonItem--->\($0)")
49 | }
50 |
51 | self.navigationItem.leftBarButtonItem = UIBarButtonItem.SCE.initialize(title: "left", style: .plain, { item in
52 | print("leftBarButtonItem--->\(item)")
53 | })
54 |
55 | // MARK: GestureRecognizer
56 |
57 | let view = UIView.init(frame: CGRect.init(x: 0, y: 100, width: 50, height: 50))
58 | view.backgroundColor = .green
59 | view.isUserInteractionEnabled = true
60 | self.view.addSubview(view)
61 | let tgr = UITapGestureRecognizer.SCE.initialize {
62 | print("tap--->\($0)")
63 | }
64 | view.addGestureRecognizer(tgr)
65 |
66 | // MARK: WhenTapped
67 | _ = self.view.sce.whenTapped { (tgr) in
68 | print(tgr)
69 | }
70 |
71 | }
72 |
73 | }
74 |
75 |
76 | class ViewController: UINavigationController {
77 |
78 |
79 | required init?(coder aDecoder: NSCoder) {
80 | super.init(coder: aDecoder)
81 | self.viewControllers = [RootViewController()]
82 | self.navigationBar.isTranslucent = false
83 | }
84 | }
85 |
86 |
--------------------------------------------------------------------------------
/Demo/Selector-Closure.Demo.xcodeproj/project.pbxproj:
--------------------------------------------------------------------------------
1 | // !$*UTF8*$!
2 | {
3 | archiveVersion = 1;
4 | classes = {
5 | };
6 | objectVersion = 48;
7 | objects = {
8 |
9 | /* Begin PBXBuildFile section */
10 | 7176745A1FECF87400A72450 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 717674581FECF87400A72450 /* Main.storyboard */; };
11 | 7176745C1FECF87400A72450 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7176745B1FECF87400A72450 /* Assets.xcassets */; };
12 | 7176745F1FECF87400A72450 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 7176745D1FECF87400A72450 /* LaunchScreen.storyboard */; };
13 | 719207AF20806836004ED44C /* UIView+SCExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 719207AE20806836004ED44C /* UIView+SCExtension.swift */; };
14 | 719207B22080B3F0004ED44C /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 719207B02080B3F0004ED44C /* ViewController.swift */; };
15 | 719207B32080B3F0004ED44C /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 719207B12080B3F0004ED44C /* AppDelegate.swift */; };
16 | 71C5A6442074AFEF006C4A69 /* SCExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71C5A6402074AFEF006C4A69 /* SCExtension.swift */; };
17 | 71C5A6452074AFEF006C4A69 /* UIBarButtonItem+SCExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71C5A6412074AFEF006C4A69 /* UIBarButtonItem+SCExtension.swift */; };
18 | 71C5A6462074AFEF006C4A69 /* UIControl+SCExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71C5A6422074AFEF006C4A69 /* UIControl+SCExtension.swift */; };
19 | 71C5A6472074AFEF006C4A69 /* UIGestureRecognizer+SCExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71C5A6432074AFEF006C4A69 /* UIGestureRecognizer+SCExtension.swift */; };
20 | /* End PBXBuildFile section */
21 |
22 | /* Begin PBXFileReference section */
23 | 717674511FECF87400A72450 /* Selector-Closure.Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Selector-Closure.Demo.app"; sourceTree = BUILT_PRODUCTS_DIR; };
24 | 717674591FECF87400A72450 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; };
25 | 7176745B1FECF87400A72450 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; };
26 | 7176745E1FECF87400A72450 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; };
27 | 717674601FECF87400A72450 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
28 | 719207AE20806836004ED44C /* UIView+SCExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+SCExtension.swift"; sourceTree = ""; };
29 | 719207B02080B3F0004ED44C /* ViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; };
30 | 719207B12080B3F0004ED44C /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; };
31 | 71C5A6402074AFEF006C4A69 /* SCExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCExtension.swift; sourceTree = ""; };
32 | 71C5A6412074AFEF006C4A69 /* UIBarButtonItem+SCExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIBarButtonItem+SCExtension.swift"; sourceTree = ""; };
33 | 71C5A6422074AFEF006C4A69 /* UIControl+SCExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIControl+SCExtension.swift"; sourceTree = ""; };
34 | 71C5A6432074AFEF006C4A69 /* UIGestureRecognizer+SCExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIGestureRecognizer+SCExtension.swift"; sourceTree = ""; };
35 | /* End PBXFileReference section */
36 |
37 | /* Begin PBXFrameworksBuildPhase section */
38 | 7176744E1FECF87400A72450 /* Frameworks */ = {
39 | isa = PBXFrameworksBuildPhase;
40 | buildActionMask = 2147483647;
41 | files = (
42 | );
43 | runOnlyForDeploymentPostprocessing = 0;
44 | };
45 | /* End PBXFrameworksBuildPhase section */
46 |
47 | /* Begin PBXGroup section */
48 | 717674481FECF87400A72450 = {
49 | isa = PBXGroup;
50 | children = (
51 | 71C5A63F2074AFEF006C4A69 /* Source */,
52 | 717674531FECF87400A72450 /* Demo */,
53 | 717674521FECF87400A72450 /* Products */,
54 | );
55 | sourceTree = "";
56 | };
57 | 717674521FECF87400A72450 /* Products */ = {
58 | isa = PBXGroup;
59 | children = (
60 | 717674511FECF87400A72450 /* Selector-Closure.Demo.app */,
61 | );
62 | name = Products;
63 | sourceTree = "";
64 | };
65 | 717674531FECF87400A72450 /* Demo */ = {
66 | isa = PBXGroup;
67 | children = (
68 | 719207B4208440BC004ED44C /* Supporting */,
69 | 719207B12080B3F0004ED44C /* AppDelegate.swift */,
70 | 719207B02080B3F0004ED44C /* ViewController.swift */,
71 | );
72 | path = Demo;
73 | sourceTree = "";
74 | };
75 | 719207B4208440BC004ED44C /* Supporting */ = {
76 | isa = PBXGroup;
77 | children = (
78 | 717674581FECF87400A72450 /* Main.storyboard */,
79 | 7176745B1FECF87400A72450 /* Assets.xcassets */,
80 | 7176745D1FECF87400A72450 /* LaunchScreen.storyboard */,
81 | 717674601FECF87400A72450 /* Info.plist */,
82 | );
83 | name = Supporting;
84 | sourceTree = "";
85 | };
86 | 71C5A63F2074AFEF006C4A69 /* Source */ = {
87 | isa = PBXGroup;
88 | children = (
89 | 71C5A6402074AFEF006C4A69 /* SCExtension.swift */,
90 | 71C5A6412074AFEF006C4A69 /* UIBarButtonItem+SCExtension.swift */,
91 | 71C5A6422074AFEF006C4A69 /* UIControl+SCExtension.swift */,
92 | 71C5A6432074AFEF006C4A69 /* UIGestureRecognizer+SCExtension.swift */,
93 | 719207AE20806836004ED44C /* UIView+SCExtension.swift */,
94 | );
95 | name = Source;
96 | path = ../Source;
97 | sourceTree = "";
98 | };
99 | /* End PBXGroup section */
100 |
101 | /* Begin PBXNativeTarget section */
102 | 717674501FECF87400A72450 /* Selector-Closure.Demo */ = {
103 | isa = PBXNativeTarget;
104 | buildConfigurationList = 717674631FECF87400A72450 /* Build configuration list for PBXNativeTarget "Selector-Closure.Demo" */;
105 | buildPhases = (
106 | 7176744D1FECF87400A72450 /* Sources */,
107 | 7176744E1FECF87400A72450 /* Frameworks */,
108 | 7176744F1FECF87400A72450 /* Resources */,
109 | );
110 | buildRules = (
111 | );
112 | dependencies = (
113 | );
114 | name = "Selector-Closure.Demo";
115 | productName = UIKit.Closures.Demo;
116 | productReference = 717674511FECF87400A72450 /* Selector-Closure.Demo.app */;
117 | productType = "com.apple.product-type.application";
118 | };
119 | /* End PBXNativeTarget section */
120 |
121 | /* Begin PBXProject section */
122 | 717674491FECF87400A72450 /* Project object */ = {
123 | isa = PBXProject;
124 | attributes = {
125 | LastSwiftUpdateCheck = 0910;
126 | LastUpgradeCheck = 0910;
127 | ORGANIZATIONNAME = ray;
128 | TargetAttributes = {
129 | 717674501FECF87400A72450 = {
130 | CreatedOnToolsVersion = 9.1;
131 | LastSwiftMigration = 0920;
132 | ProvisioningStyle = Automatic;
133 | };
134 | };
135 | };
136 | buildConfigurationList = 7176744C1FECF87400A72450 /* Build configuration list for PBXProject "Selector-Closure.Demo" */;
137 | compatibilityVersion = "Xcode 8.0";
138 | developmentRegion = en;
139 | hasScannedForEncodings = 0;
140 | knownRegions = (
141 | en,
142 | Base,
143 | );
144 | mainGroup = 717674481FECF87400A72450;
145 | productRefGroup = 717674521FECF87400A72450 /* Products */;
146 | projectDirPath = "";
147 | projectRoot = "";
148 | targets = (
149 | 717674501FECF87400A72450 /* Selector-Closure.Demo */,
150 | );
151 | };
152 | /* End PBXProject section */
153 |
154 | /* Begin PBXResourcesBuildPhase section */
155 | 7176744F1FECF87400A72450 /* Resources */ = {
156 | isa = PBXResourcesBuildPhase;
157 | buildActionMask = 2147483647;
158 | files = (
159 | 7176745F1FECF87400A72450 /* LaunchScreen.storyboard in Resources */,
160 | 7176745C1FECF87400A72450 /* Assets.xcassets in Resources */,
161 | 7176745A1FECF87400A72450 /* Main.storyboard in Resources */,
162 | );
163 | runOnlyForDeploymentPostprocessing = 0;
164 | };
165 | /* End PBXResourcesBuildPhase section */
166 |
167 | /* Begin PBXSourcesBuildPhase section */
168 | 7176744D1FECF87400A72450 /* Sources */ = {
169 | isa = PBXSourcesBuildPhase;
170 | buildActionMask = 2147483647;
171 | files = (
172 | 719207B22080B3F0004ED44C /* ViewController.swift in Sources */,
173 | 71C5A6462074AFEF006C4A69 /* UIControl+SCExtension.swift in Sources */,
174 | 71C5A6472074AFEF006C4A69 /* UIGestureRecognizer+SCExtension.swift in Sources */,
175 | 71C5A6452074AFEF006C4A69 /* UIBarButtonItem+SCExtension.swift in Sources */,
176 | 719207B32080B3F0004ED44C /* AppDelegate.swift in Sources */,
177 | 719207AF20806836004ED44C /* UIView+SCExtension.swift in Sources */,
178 | 71C5A6442074AFEF006C4A69 /* SCExtension.swift in Sources */,
179 | );
180 | runOnlyForDeploymentPostprocessing = 0;
181 | };
182 | /* End PBXSourcesBuildPhase section */
183 |
184 | /* Begin PBXVariantGroup section */
185 | 717674581FECF87400A72450 /* Main.storyboard */ = {
186 | isa = PBXVariantGroup;
187 | children = (
188 | 717674591FECF87400A72450 /* Base */,
189 | );
190 | name = Main.storyboard;
191 | sourceTree = "";
192 | };
193 | 7176745D1FECF87400A72450 /* LaunchScreen.storyboard */ = {
194 | isa = PBXVariantGroup;
195 | children = (
196 | 7176745E1FECF87400A72450 /* Base */,
197 | );
198 | name = LaunchScreen.storyboard;
199 | sourceTree = "";
200 | };
201 | /* End PBXVariantGroup section */
202 |
203 | /* Begin XCBuildConfiguration section */
204 | 717674611FECF87400A72450 /* Debug */ = {
205 | isa = XCBuildConfiguration;
206 | buildSettings = {
207 | ALWAYS_SEARCH_USER_PATHS = NO;
208 | CLANG_ANALYZER_NONNULL = YES;
209 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
210 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
211 | CLANG_CXX_LIBRARY = "libc++";
212 | CLANG_ENABLE_MODULES = YES;
213 | CLANG_ENABLE_OBJC_ARC = YES;
214 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
215 | CLANG_WARN_BOOL_CONVERSION = YES;
216 | CLANG_WARN_COMMA = YES;
217 | CLANG_WARN_CONSTANT_CONVERSION = YES;
218 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
219 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
220 | CLANG_WARN_EMPTY_BODY = YES;
221 | CLANG_WARN_ENUM_CONVERSION = YES;
222 | CLANG_WARN_INFINITE_RECURSION = YES;
223 | CLANG_WARN_INT_CONVERSION = YES;
224 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
225 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
226 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
227 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
228 | CLANG_WARN_STRICT_PROTOTYPES = YES;
229 | CLANG_WARN_SUSPICIOUS_MOVE = YES;
230 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
231 | CLANG_WARN_UNREACHABLE_CODE = YES;
232 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
233 | CODE_SIGN_IDENTITY = "iPhone Developer";
234 | COPY_PHASE_STRIP = NO;
235 | DEBUG_INFORMATION_FORMAT = dwarf;
236 | ENABLE_STRICT_OBJC_MSGSEND = YES;
237 | ENABLE_TESTABILITY = YES;
238 | GCC_C_LANGUAGE_STANDARD = gnu11;
239 | GCC_DYNAMIC_NO_PIC = NO;
240 | GCC_NO_COMMON_BLOCKS = YES;
241 | GCC_OPTIMIZATION_LEVEL = 0;
242 | GCC_PREPROCESSOR_DEFINITIONS = (
243 | "DEBUG=1",
244 | "$(inherited)",
245 | );
246 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
247 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
248 | GCC_WARN_UNDECLARED_SELECTOR = YES;
249 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
250 | GCC_WARN_UNUSED_FUNCTION = YES;
251 | GCC_WARN_UNUSED_VARIABLE = YES;
252 | IPHONEOS_DEPLOYMENT_TARGET = 9.0;
253 | MTL_ENABLE_DEBUG_INFO = YES;
254 | ONLY_ACTIVE_ARCH = YES;
255 | SDKROOT = iphoneos;
256 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
257 | SWIFT_OPTIMIZATION_LEVEL = "-Onone";
258 | SWIFT_SWIFT3_OBJC_INFERENCE = Default;
259 | SWIFT_VERSION = 3.0;
260 | };
261 | name = Debug;
262 | };
263 | 717674621FECF87400A72450 /* Release */ = {
264 | isa = XCBuildConfiguration;
265 | buildSettings = {
266 | ALWAYS_SEARCH_USER_PATHS = NO;
267 | CLANG_ANALYZER_NONNULL = YES;
268 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
269 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
270 | CLANG_CXX_LIBRARY = "libc++";
271 | CLANG_ENABLE_MODULES = YES;
272 | CLANG_ENABLE_OBJC_ARC = YES;
273 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
274 | CLANG_WARN_BOOL_CONVERSION = YES;
275 | CLANG_WARN_COMMA = YES;
276 | CLANG_WARN_CONSTANT_CONVERSION = YES;
277 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
278 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
279 | CLANG_WARN_EMPTY_BODY = YES;
280 | CLANG_WARN_ENUM_CONVERSION = YES;
281 | CLANG_WARN_INFINITE_RECURSION = YES;
282 | CLANG_WARN_INT_CONVERSION = YES;
283 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
284 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
285 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
286 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
287 | CLANG_WARN_STRICT_PROTOTYPES = YES;
288 | CLANG_WARN_SUSPICIOUS_MOVE = YES;
289 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
290 | CLANG_WARN_UNREACHABLE_CODE = YES;
291 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
292 | CODE_SIGN_IDENTITY = "iPhone Developer";
293 | COPY_PHASE_STRIP = NO;
294 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
295 | ENABLE_NS_ASSERTIONS = NO;
296 | ENABLE_STRICT_OBJC_MSGSEND = YES;
297 | GCC_C_LANGUAGE_STANDARD = gnu11;
298 | GCC_NO_COMMON_BLOCKS = YES;
299 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
300 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
301 | GCC_WARN_UNDECLARED_SELECTOR = YES;
302 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
303 | GCC_WARN_UNUSED_FUNCTION = YES;
304 | GCC_WARN_UNUSED_VARIABLE = YES;
305 | IPHONEOS_DEPLOYMENT_TARGET = 9.0;
306 | MTL_ENABLE_DEBUG_INFO = NO;
307 | SDKROOT = iphoneos;
308 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
309 | SWIFT_SWIFT3_OBJC_INFERENCE = Default;
310 | SWIFT_VERSION = 3.0;
311 | VALIDATE_PRODUCT = YES;
312 | };
313 | name = Release;
314 | };
315 | 717674641FECF87400A72450 /* Debug */ = {
316 | isa = XCBuildConfiguration;
317 | buildSettings = {
318 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
319 | CLANG_ENABLE_MODULES = YES;
320 | CODE_SIGN_STYLE = Automatic;
321 | DEVELOPMENT_TEAM = 5TW2U68DN6;
322 | INFOPLIST_FILE = Demo/Info.plist;
323 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
324 | PRODUCT_BUNDLE_IDENTIFIER = "com.ray.selector-closure";
325 | PRODUCT_NAME = "$(TARGET_NAME)";
326 | SWIFT_OPTIMIZATION_LEVEL = "-Onone";
327 | SWIFT_SWIFT3_OBJC_INFERENCE = On;
328 | SWIFT_VERSION = 4.0;
329 | TARGETED_DEVICE_FAMILY = "1,2";
330 | };
331 | name = Debug;
332 | };
333 | 717674651FECF87400A72450 /* Release */ = {
334 | isa = XCBuildConfiguration;
335 | buildSettings = {
336 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
337 | CLANG_ENABLE_MODULES = YES;
338 | CODE_SIGN_STYLE = Automatic;
339 | DEVELOPMENT_TEAM = 5TW2U68DN6;
340 | INFOPLIST_FILE = Demo/Info.plist;
341 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
342 | PRODUCT_BUNDLE_IDENTIFIER = "com.ray.selector-closure";
343 | PRODUCT_NAME = "$(TARGET_NAME)";
344 | SWIFT_SWIFT3_OBJC_INFERENCE = On;
345 | SWIFT_VERSION = 4.0;
346 | TARGETED_DEVICE_FAMILY = "1,2";
347 | };
348 | name = Release;
349 | };
350 | /* End XCBuildConfiguration section */
351 |
352 | /* Begin XCConfigurationList section */
353 | 7176744C1FECF87400A72450 /* Build configuration list for PBXProject "Selector-Closure.Demo" */ = {
354 | isa = XCConfigurationList;
355 | buildConfigurations = (
356 | 717674611FECF87400A72450 /* Debug */,
357 | 717674621FECF87400A72450 /* Release */,
358 | );
359 | defaultConfigurationIsVisible = 0;
360 | defaultConfigurationName = Release;
361 | };
362 | 717674631FECF87400A72450 /* Build configuration list for PBXNativeTarget "Selector-Closure.Demo" */ = {
363 | isa = XCConfigurationList;
364 | buildConfigurations = (
365 | 717674641FECF87400A72450 /* Debug */,
366 | 717674651FECF87400A72450 /* Release */,
367 | );
368 | defaultConfigurationIsVisible = 0;
369 | defaultConfigurationName = Release;
370 | };
371 | /* End XCConfigurationList section */
372 | };
373 | rootObject = 717674491FECF87400A72450 /* Project object */;
374 | }
375 |
--------------------------------------------------------------------------------
/Demo/Selector-Closure.Demo.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Erwa
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Moved to https://github.com/mithyer/Ralyo/tree/master/src/Extension/Selector-Closure
2 |
3 | # Selector-Closure
4 | [![Swift32][swift32-badge]][swift-url]
5 | [![Swift4][swift4-badge]][swift-url]
6 | [![Platform][platform-badge]][platform-url]
7 |
8 | **Selector-Closure** is a light way to convert objc target-action selector to closure style
9 |
10 | # Version 1.x.x
11 |
12 | Type of Object calling "sce" will be auto recognized in closure
13 |
14 | ## UIControl
15 |
16 | **use ".sce" to make closure callback**
17 |
18 | ```swift
19 | let btn = UIButton()
20 | let field = UITextField()
21 |
22 | // for specific event
23 | _ = btn.sce.add(.touchUpInside) { sender in
24 | // ... sender is UIButton
25 | }
26 |
27 | // for default event(UIButton, UISwitch, UISlider, UITextField has default events)
28 | let invoker = field.sce.add { sender in
29 | // ... sender is UITextField
30 | }
31 |
32 | // now you can remove action with invoker returned
33 | field.sce.remove(invoker)
34 |
35 | // remove all for events
36 | field.sce.removeAll(.touchUpInside) { sender in
37 | // ...
38 | }
39 |
40 | // test didAdd events
41 | let res: Bool = field.sce.didAdd(.touchUpInside)
42 | ```
43 |
44 |
45 | ## UIBarButtonItem
46 |
47 | **use ".SCE" to initialize**
48 |
49 | ```swift
50 | let item = UIBarButtonItem.SCE.initialize(title: "test", style: .plain, { item in
51 | // ...
52 | })
53 |
54 | // ...
55 | ```
56 |
57 | ## UIGestureRecognizer
58 |
59 | **use ".SCE" to initialize**
60 |
61 | ```swift
62 | // init a recognizer,
63 | let tgr = UITapGestureRecognizer.SCE.initialize { tgr in
64 | // tgr is UITapGestureRecognizer
65 | }
66 |
67 | let sgr = UISwipeGestureRecognizer.SCE.initialize { sgr in
68 | // sgr is UISwipeGestureRecognizer
69 | }
70 | ```
71 |
72 | ## UIView
73 |
74 | **use ".sce" to make closure callback**
75 |
76 | ```swift
77 | // fast make tap action
78 | let view = UIView()
79 | let tapGestureRecognizer = view.sce.whenTapped { tgr in
80 | // tgr === tapGestureRecognizer
81 | }
82 | ```
83 |
84 |
85 | ## Convert your own
86 |
87 | ```swift
88 | // you have an instance named obj
89 | let invoker = Invoker(obj) { obj in
90 | // ...
91 | }
92 | let act = invoker.action // convert done, now pass invoker to target, act to action
93 | ```
94 | [swift32-badge]: https://img.shields.io/badge/Swift-3.2-orange.svg?style=flat
95 | [swift4-badge]: https://img.shields.io/badge/Swift-4.0-orange.svg?style=flat
96 | [swift-url]: https://swift.org
97 | [platform-badge]: https://img.shields.io/badge/platform-iOS-lightgrey.svg
98 | [platform-url]: https://developer.apple.com/swift/
99 |
--------------------------------------------------------------------------------
/Source/SCExtension.swift:
--------------------------------------------------------------------------------
1 | //
2 | // Selector-Closureswift
3 | // UIKit.Closures
4 | //
5 | // Created by ray on 2017/12/22.
6 | // Copyright © 2017年 ray. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 |
12 | public class Invoker {
13 |
14 | weak var sender: T?
15 |
16 | var events: UIControl.Event?
17 |
18 | var closure: (T) -> Void
19 |
20 | public var action: Selector {
21 | return #selector(invoke)
22 | }
23 |
24 | public init(_ sender: T, _ closure: @escaping (T) -> Void) {
25 | self.sender = sender
26 | self.closure = closure
27 | }
28 |
29 | @objc func invoke() {
30 | if let sender = self.sender {
31 | self.closure(sender)
32 | }
33 | }
34 | }
35 |
36 | public class SCECls {
37 |
38 | weak public var object: T?
39 |
40 | init(_ object: T) {
41 | self.object = object
42 | }
43 |
44 | func set(_ attachObj: Any?, forKey key: inout Void?) {
45 | objc_setAssociatedObject(self, &key, attachObj, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
46 | }
47 |
48 | func getAttach(forKey key: inout Void?) -> K? {
49 | return objc_getAssociatedObject(self, &key) as? K
50 | }
51 | }
52 |
53 | public protocol SCExtension {
54 |
55 | associatedtype T: AnyObject
56 |
57 | static var SCE: SCECls.Type { get }
58 | var sce: SCECls { get }
59 | }
60 |
61 | fileprivate var sceKey: Void?
62 |
63 | extension SCExtension where Self: AnyObject {
64 |
65 | public static var SCE: SCECls.Type {
66 | return SCECls.self
67 | }
68 |
69 | public var sce: SCECls {
70 | return objc_getAssociatedObject(self, &sceKey) as? SCECls ?? {
71 | let sce = SCECls(self)
72 | objc_setAssociatedObject(self, &sceKey, sce, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
73 | return sce
74 | }()
75 | }
76 | }
77 |
78 |
--------------------------------------------------------------------------------
/Source/UIBarButtonItem+SCExtension.swift:
--------------------------------------------------------------------------------
1 | //
2 | // UIBarButtonItem+Closures.swift
3 | // UIKit.Closures
4 | //
5 | // Created by ray on 2017/12/22.
6 | // Copyright © 2017年 ray. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | fileprivate var invokerKey: Void?
12 |
13 | extension UIBarButtonItem: SCExtension {
14 |
15 | func initInvoker(_ closure: @escaping (T) -> Void) {
16 | let invoker = Invoker(self as! T, closure)
17 | self.sce.set(invoker, forKey: &invokerKey)
18 | self.target = invoker
19 | self.action = invoker.action
20 | }
21 | }
22 |
23 | extension SCECls where T: UIBarButtonItem {
24 |
25 | static public func initialize(image: UIImage?, style: UIBarButtonItem.Style, _ closure: @escaping (T) -> Void) -> T {
26 | let btnItem = T.init(image: image, style: style, target: nil, action: nil)
27 | btnItem.initInvoker(closure)
28 | return btnItem
29 | }
30 |
31 | static public func initialize(title: String?, style: UIBarButtonItem.Style, _ closure: @escaping (T) -> Void) -> T {
32 | let btnItem = T.init(title: title, style: style, target: nil, action: nil)
33 | btnItem.initInvoker(closure)
34 | return btnItem
35 | }
36 |
37 | static public func initialize(barButtonSystemItem systemItem: UIBarButtonItem.SystemItem, _ closure: @escaping (T) -> Void) -> T {
38 | let btnItem = T.init(barButtonSystemItem: systemItem, target: nil, action: nil)
39 | btnItem.initInvoker(closure)
40 | return btnItem
41 | }
42 |
43 | static public func initialize(customView: UIView, _ closure: @escaping (T) -> Void) -> T {
44 | let btnItem = T.init(customView: customView)
45 | btnItem.initInvoker(closure)
46 | return btnItem
47 | }
48 |
49 | public func sendAction() {
50 | if let target = self.object?.target, let action = self.object?.action {
51 | _ = target.perform(action)
52 | }
53 | }
54 | }
55 |
56 |
57 |
--------------------------------------------------------------------------------
/Source/UIControl+SCExtension.swift:
--------------------------------------------------------------------------------
1 | //
2 | // UIControl+Closures.swift
3 | // UIKit.Closures
4 | //
5 | // Created by ray on 2017/12/19.
6 | // Copyright © 2017年 ray. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 |
12 | extension UIControl.Event: Hashable {
13 |
14 | public var hashValue: Int {
15 | return self.rawValue.hashValue
16 | }
17 | }
18 |
19 | class DicWrapper {
20 |
21 | var dic = [K: V]()
22 | }
23 |
24 | class ArrayWrapper {
25 |
26 | var array = [T]()
27 | }
28 |
29 | fileprivate typealias InvokersDicWrapper = DicWrapper>>
30 |
31 | fileprivate var invokersDicWrapperKey: Void?
32 |
33 |
34 | extension SCECls where T: UIControl {
35 |
36 | func invokers(forEvents events: UIControl.Event, createIfNotExist: Bool = true) -> ArrayWrapper>? {
37 | let dicWrapper: InvokersDicWrapper? = self.getAttach(forKey: &invokersDicWrapperKey) ?? {
38 | if !createIfNotExist {
39 | return nil
40 | }
41 | let wrapper = InvokersDicWrapper()
42 | self.set(wrapper, forKey: &invokersDicWrapperKey)
43 | return wrapper
44 | }()
45 | if nil == dicWrapper {
46 | return nil
47 | }
48 | let invokers: ArrayWrapper>? = dicWrapper!.dic[events] ?? {
49 | if !createIfNotExist {
50 | return nil
51 | }
52 | let invokers = ArrayWrapper>()
53 | dicWrapper!.dic[events] = invokers
54 | return invokers
55 | }()
56 | return invokers
57 | }
58 |
59 | @discardableResult
60 | public func add(_ events: UIControl.Event? = nil, _ closure: @escaping (T) -> Void) -> Invoker {
61 | let control = self.object!
62 | let events: UIControl.Event! = events ?? {
63 | switch control {
64 | case is UIButton: return .touchUpInside
65 | case is UISwitch: fallthrough
66 | case is UISlider: return .valueChanged
67 | case is UITextField: return .editingChanged
68 | default: return nil
69 | }
70 | }()
71 | assert(nil != events, "no default events for T")
72 |
73 | let wrapper: ArrayWrapper> = invokers(forEvents: events)!
74 | let invoker = Invoker(control, closure)
75 | invoker.events = events
76 | wrapper.array.append(invoker)
77 | control.addTarget(invoker, action: invoker.action, for: events)
78 | return invoker
79 | }
80 |
81 | public func remove(_ invoker: Invoker) {
82 | guard let control = self.object,
83 | let dicWrapper: InvokersDicWrapper = self.getAttach(forKey: &invokersDicWrapperKey),
84 | let events = invoker.events,
85 | let arrayWrapper = dicWrapper.dic[events] else {
86 | return
87 | }
88 | for (idx, ivk) in arrayWrapper.array.enumerated() {
89 | if ivk === invoker {
90 | control.removeTarget(invoker, action: invoker.action, for: events)
91 | arrayWrapper.array.remove(at: idx)
92 | break
93 | }
94 | }
95 | }
96 |
97 | public func removeAll(for events: UIControl.Event) {
98 | let control = self.object!
99 | guard let wrapper = invokers(forEvents: events, createIfNotExist: false) else {
100 | return
101 | }
102 | for invoker in wrapper.array {
103 | control.removeTarget(invoker, action: invoker.action, for: events)
104 | }
105 | wrapper.array.removeAll()
106 | }
107 |
108 | public func didAdd(_ events: UIControl.Event) -> Bool {
109 | guard let wrapper = invokers(forEvents: events, createIfNotExist: false) else {
110 | return false
111 | }
112 | return wrapper.array.count > 0
113 | }
114 | }
115 |
116 |
--------------------------------------------------------------------------------
/Source/UIGestureRecognizer+SCExtension.swift:
--------------------------------------------------------------------------------
1 | //
2 | // UIGestureRecognizer+Closures.swift
3 | // UIKit.Closures
4 | //
5 | // Created by ray on 2017/12/22.
6 | // Copyright © 2017年 ray. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | fileprivate var invokerKey: Void?
12 |
13 | extension UIGestureRecognizer: SCExtension {}
14 |
15 | extension SCECls where T: UIGestureRecognizer {
16 |
17 | static public func initialize(_ closure: @escaping (T) -> Void) -> T {
18 | let recg = T.init()
19 | let invoker = Invoker(recg, closure)
20 | recg.sce.set(invoker, forKey: &invokerKey)
21 | recg.addTarget(invoker, action: invoker.action)
22 | return recg
23 | }
24 | }
25 |
26 |
27 |
--------------------------------------------------------------------------------
/Source/UIView+SCExtension.swift:
--------------------------------------------------------------------------------
1 | //
2 | // UIView+SCExtension.swift
3 | // Selector-Closure.Demo
4 | //
5 | // Created by ray on 2018/4/13.
6 | // Copyright © 2018年 ray. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | extension UIView: SCExtension {}
12 |
13 | extension SCECls where T: UIView {
14 |
15 | @discardableResult
16 | public func whenTapped(_ enableUserInteraction: Bool = true, _ closure: @escaping (UITapGestureRecognizer) -> Void) -> UITapGestureRecognizer {
17 | let view = self.object!
18 | if enableUserInteraction {
19 | view.isUserInteractionEnabled = true
20 | }
21 | let recg = UITapGestureRecognizer.SCE.initialize(closure)
22 | view.addGestureRecognizer(recg)
23 | return recg
24 | }
25 | }
26 |
--------------------------------------------------------------------------------