├── .gitignore
├── .swiftpm
└── xcode
│ ├── package.xcworkspace
│ └── contents.xcworkspacedata
│ └── xcshareddata
│ └── xcschemes
│ └── PadControl.xcscheme
├── LICENSE
├── Package.swift
├── README.md
├── Sources
├── PadControl Example
│ ├── AppDelegate.swift
│ ├── Assets.xcassets
│ │ ├── AppIcon.appiconset
│ │ │ └── Contents.json
│ │ └── Contents.json
│ ├── Base.lproj
│ │ ├── LaunchScreen.storyboard
│ │ └── Main.storyboard
│ ├── Conveniences.swift
│ ├── Info.plist
│ └── ViewController.swift
└── PadControl
│ ├── Info.plist
│ ├── PadControl.h
│ └── PadControl.swift
├── pad-bl.gif
├── pad-omni.gif
├── pad-x.gif
└── pad-y.gif
/.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 |
--------------------------------------------------------------------------------
/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.swiftpm/xcode/xcshareddata/xcschemes/PadControl.xcscheme:
--------------------------------------------------------------------------------
1 |
2 |
5 |
8 |
9 |
15 |
21 |
22 |
23 |
24 |
25 |
31 |
32 |
42 |
43 |
49 |
50 |
56 |
57 |
58 |
59 |
61 |
62 |
65 |
66 |
67 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Gabriel O'Flaherty-Chan
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 |
--------------------------------------------------------------------------------
/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version: 5.8
2 | // The swift-tools-version declares the minimum version of Swift required to build this package.
3 |
4 | import PackageDescription
5 |
6 | let package = Package(
7 | name: "PadControl",
8 | platforms: [
9 | .iOS(.v11),
10 | .macOS(.v10_13)
11 | ],
12 | products: [
13 | // Products define the executables and libraries a package produces, making them visible to other packages.
14 | .library(
15 | name: "PadControl",
16 | targets: ["PadControl"]),
17 | ],
18 | targets: [
19 | // Targets are the basic building blocks of a package, defining a module or a test suite.
20 | // Targets can depend on other targets in this package and products from dependencies.
21 | .target(
22 | name: "PadControl"),
23 | ]
24 | )
25 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # PadControl
2 | [](https://github.com/Carthage/Carthage)
3 |
4 | A `UIControl` subclass for two-dimension directional input. Supports customization of color and other cosmetic attributes. To use with Carthage, add the following to your `Cartfile`:
5 |
6 | ```
7 | github "gabrieloc/PadControl"
8 | ```
9 |
10 | If you prefer not using Carthage, simply copy `PadControl.swift` into your project.
11 |
12 |
13 | ### Example Usage
14 | Create an instance of `PadControl` by providing direction information (`PadDirections`). To create a unidirectional pad in your View Controller:
15 | ``` swift
16 |
17 | override func viewDidLoad() {
18 | super.viewDidLoad
19 |
20 | let pad = PadControl(directions: [.up])
21 | pad.addTarget(self, action: #selector(padUpdated), for: .valueChanged)
22 | view.addSubview(pad)
23 | }
24 |
25 | @objc func padUpdated(sender: PadControl) {
26 | let value = sender.value(forDirection: .up)
27 | // value is a a Double from 0 - 1
28 | }
29 |
30 | ```
31 |
32 | Passing different directions allows for a variety of uni or bidirectional inputs to be created. For example:
33 | 
34 | 
35 | 
36 | 
37 |
38 |
39 | To view all of these examples, refer to the app target in the main `.xcodeproj` entitled `PadControl Example`.
40 |
41 | ### FYI
42 |
43 | PadControl is used by [SCARAB](https://itunes.apple.com/us/app/scarab-rc-controller-for-quadcopters/id1205279859), a free quadcopter controller for iOS. If you are interested in building quadcopter software, SCARAB is also powered by [QuadKit](https://github.com/gabrieloc/QuadKit), a framework for communicating with generic quadcopters.
--------------------------------------------------------------------------------
/Sources/PadControl Example/AppDelegate.swift:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.swift
3 | // PadControl Example
4 | //
5 | // MIT License
6 | //
7 | // Copyright (c) 2017 Gabriel O'Flaherty-Chan
8 | //
9 | // Permission is hereby granted, free of charge, to any person obtaining a copy
10 | // of this software and associated documentation files (the "Software"), to deal
11 | // in the Software without restriction, including without limitation the rights
12 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 | // copies of the Software, and to permit persons to whom the Software is
14 | // furnished to do so, subject to the following conditions:
15 | //
16 | // The above copyright notice and this permission notice shall be included in all
17 | // copies or substantial portions of the Software.
18 | //
19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 | // SOFTWARE.
26 |
27 | import UIKit
28 |
29 | @UIApplicationMain
30 | class AppDelegate: UIResponder, UIApplicationDelegate {
31 |
32 | var window: UIWindow?
33 |
34 |
35 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
36 | // Override point for customization after application launch.
37 | return true
38 | }
39 |
40 | func applicationWillResignActive(_ application: UIApplication) {
41 | // 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.
42 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
43 | }
44 |
45 | func applicationDidEnterBackground(_ application: UIApplication) {
46 | // 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.
47 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
48 | }
49 |
50 | func applicationWillEnterForeground(_ application: UIApplication) {
51 | // 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.
52 | }
53 |
54 | func applicationDidBecomeActive(_ application: UIApplication) {
55 | // 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.
56 | }
57 |
58 | func applicationWillTerminate(_ application: UIApplication) {
59 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
60 | }
61 |
62 |
63 | }
64 |
65 |
--------------------------------------------------------------------------------
/Sources/PadControl Example/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "scale" : "2x",
6 | "size" : "20x20"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "scale" : "3x",
11 | "size" : "20x20"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "scale" : "2x",
16 | "size" : "29x29"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "scale" : "3x",
21 | "size" : "29x29"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "scale" : "2x",
26 | "size" : "40x40"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "scale" : "3x",
31 | "size" : "40x40"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "scale" : "2x",
36 | "size" : "60x60"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "scale" : "3x",
41 | "size" : "60x60"
42 | },
43 | {
44 | "idiom" : "ipad",
45 | "scale" : "1x",
46 | "size" : "20x20"
47 | },
48 | {
49 | "idiom" : "ipad",
50 | "scale" : "2x",
51 | "size" : "20x20"
52 | },
53 | {
54 | "idiom" : "ipad",
55 | "scale" : "1x",
56 | "size" : "29x29"
57 | },
58 | {
59 | "idiom" : "ipad",
60 | "scale" : "2x",
61 | "size" : "29x29"
62 | },
63 | {
64 | "idiom" : "ipad",
65 | "scale" : "1x",
66 | "size" : "40x40"
67 | },
68 | {
69 | "idiom" : "ipad",
70 | "scale" : "2x",
71 | "size" : "40x40"
72 | },
73 | {
74 | "idiom" : "ipad",
75 | "scale" : "1x",
76 | "size" : "76x76"
77 | },
78 | {
79 | "idiom" : "ipad",
80 | "scale" : "2x",
81 | "size" : "76x76"
82 | },
83 | {
84 | "idiom" : "ipad",
85 | "scale" : "2x",
86 | "size" : "83.5x83.5"
87 | },
88 | {
89 | "idiom" : "ios-marketing",
90 | "scale" : "1x",
91 | "size" : "1024x1024"
92 | }
93 | ],
94 | "info" : {
95 | "author" : "xcode",
96 | "version" : 1
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/Sources/PadControl Example/Assets.xcassets/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "info" : {
3 | "author" : "xcode",
4 | "version" : 1
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/Sources/PadControl Example/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 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/Sources/PadControl Example/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 |
26 |
27 |
33 |
34 |
35 |
36 |
37 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
--------------------------------------------------------------------------------
/Sources/PadControl Example/Conveniences.swift:
--------------------------------------------------------------------------------
1 | //
2 | // Conveniences.swift
3 | // PadControl Example
4 | //
5 | // MIT License
6 | //
7 | // Copyright (c) 2017 Gabriel O'Flaherty-Chan
8 | //
9 | // Permission is hereby granted, free of charge, to any person obtaining a copy
10 | // of this software and associated documentation files (the "Software"), to deal
11 | // in the Software without restriction, including without limitation the rights
12 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 | // copies of the Software, and to permit persons to whom the Software is
14 | // furnished to do so, subject to the following conditions:
15 | //
16 | // The above copyright notice and this permission notice shall be included in all
17 | // copies or substantial portions of the Software.
18 | //
19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 | // SOFTWARE.
26 |
27 | import UIKit
28 |
29 | extension UIView {
30 | func fillSuperview() {
31 | guard let constraints = constraintsFillingSuperview() else {
32 | return
33 | }
34 | constraints.forEach { $0.isActive = true }
35 | }
36 |
37 | func constraintsFillingSuperview() -> [NSLayoutConstraint]? {
38 | guard let superview = self.superview else {
39 | print("\(self) must be in view hierarchy")
40 | return nil
41 | }
42 | translatesAutoresizingMaskIntoConstraints = false
43 | return [topAnchor.constraint(equalTo: superview.topAnchor),
44 | leftAnchor.constraint(equalTo: superview.leftAnchor),
45 | superview.rightAnchor.constraint(equalTo: rightAnchor),
46 | superview.bottomAnchor.constraint(equalTo: bottomAnchor)]
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/Sources/PadControl Example/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 |
--------------------------------------------------------------------------------
/Sources/PadControl Example/ViewController.swift:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.swift
3 | // PadControl Example
4 | //
5 | // MIT License
6 | //
7 | // Copyright (c) 2017 Gabriel O'Flaherty-Chan
8 | //
9 | // Permission is hereby granted, free of charge, to any person obtaining a copy
10 | // of this software and associated documentation files (the "Software"), to deal
11 | // in the Software without restriction, including without limitation the rights
12 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 | // copies of the Software, and to permit persons to whom the Software is
14 | // furnished to do so, subject to the following conditions:
15 | //
16 | // The above copyright notice and this permission notice shall be included in all
17 | // copies or substantial portions of the Software.
18 | //
19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 | // SOFTWARE.
26 |
27 | import UIKit
28 | import PadControl
29 |
30 | class ViewController: UIViewController {
31 |
32 | @IBOutlet weak var omniDirectionalContainer: UIView!
33 | @IBOutlet weak var xBidirectionalContainer: UIStackView!
34 | @IBOutlet weak var yBidirectionalContainer: UIStackView!
35 |
36 | override func viewDidLoad() {
37 | super.viewDidLoad()
38 |
39 | let omniPad = PadControl(directions: .all, planes: 3)
40 | omniPad.addTarget(self, action: #selector(padUpdated), for: .valueChanged)
41 | omniDirectionalContainer.backgroundColor = .clear
42 | omniDirectionalContainer.addSubview(omniPad)
43 | omniPad.fillSuperview()
44 |
45 | xBidirectionalContainer.arrangedSubviews.forEach { view in
46 | let idx = xBidirectionalContainer.arrangedSubviews.index(of: view)
47 | let directions: PadDirections = idx == 0 ? [.left] : idx == 1 ? [.right] : [.left, .right]
48 | let padControl = PadControl(directions: directions)
49 | padControl.addTarget(self, action: #selector(padUpdated), for: .valueChanged)
50 | view.backgroundColor = .clear
51 | view.addSubview(padControl)
52 | padControl.fillSuperview()
53 | }
54 |
55 | yBidirectionalContainer.arrangedSubviews.forEach { view in
56 | let idx = yBidirectionalContainer.arrangedSubviews.index(of: view)
57 | let directions: PadDirections = idx == 0 ? [.up] : idx == 1 ? [.down] : [.up, .down]
58 | let padControl = PadControl(directions: directions)
59 | padControl.addTarget(self, action: #selector(padUpdated), for: .valueChanged)
60 | view.backgroundColor = .clear
61 | view.addSubview(padControl)
62 | padControl.fillSuperview()
63 | }
64 | }
65 |
66 | @objc func padUpdated(sender: PadControl) {
67 |
68 | let u = sender.value(forDirection: .up)
69 | let r = sender.value(forDirection: .right)
70 | let d = sender.value(forDirection: .down)
71 | let l = sender.value(forDirection: .left)
72 |
73 | var description = ""
74 | description += u > 0 ? "⬆️ \(u)" : ""
75 | description += r > 0 ? "➡️ \(r)" : ""
76 | description += d > 0 ? "⬇️ \(d)" : ""
77 | description += l > 0 ? "⬅️ \(l)" : ""
78 | print(description)
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/Sources/PadControl/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 | FMWK
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | $(CURRENT_PROJECT_VERSION)
21 | NSPrincipalClass
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Sources/PadControl/PadControl.h:
--------------------------------------------------------------------------------
1 | //
2 | // PadControl.h
3 | // PadControl
4 | //
5 | // MIT License
6 | //
7 | // Copyright (c) 2017 Gabriel O'Flaherty-Chan
8 | //
9 | // Permission is hereby granted, free of charge, to any person obtaining a copy
10 | // of this software and associated documentation files (the "Software"), to deal
11 | // in the Software without restriction, including without limitation the rights
12 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 | // copies of the Software, and to permit persons to whom the Software is
14 | // furnished to do so, subject to the following conditions:
15 | //
16 | // The above copyright notice and this permission notice shall be included in all
17 | // copies or substantial portions of the Software.
18 | //
19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 | // SOFTWARE.
26 |
27 | #import
28 |
29 | //! Project version number for PadControl.
30 | FOUNDATION_EXPORT double PadControlVersionNumber;
31 |
32 | //! Project version string for PadControl.
33 | FOUNDATION_EXPORT const unsigned char PadControlVersionString[];
34 |
35 | // In this header, you should import all the public headers of your framework using statements like #import
36 |
37 |
38 |
--------------------------------------------------------------------------------
/Sources/PadControl/PadControl.swift:
--------------------------------------------------------------------------------
1 | //
2 | // PadControl.swift
3 | // PadControl
4 | //
5 | // MIT License
6 | //
7 | // Copyright (c) 2017 Gabriel O'Flaherty-Chan
8 | //
9 | // Permission is hereby granted, free of charge, to any person obtaining a copy
10 | // of this software and associated documentation files (the "Software"), to deal
11 | // in the Software without restriction, including without limitation the rights
12 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 | // copies of the Software, and to permit persons to whom the Software is
14 | // furnished to do so, subject to the following conditions:
15 | //
16 | // The above copyright notice and this permission notice shall be included in all
17 | // copies or substantial portions of the Software.
18 | //
19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 | // SOFTWARE.
26 |
27 | import UIKit
28 |
29 | func lerp(_ lhs: CGFloat, _ rhs: CGFloat, _ percentage: CGFloat, _ exponent: CGFloat = 1.0) -> CGFloat {
30 | return (rhs - lhs) * pow(percentage, exponent) + lhs
31 | }
32 |
33 | func clamp(_ x: CGFloat, min: CGFloat, max: CGFloat) -> CGFloat {
34 | return Swift.min(max, Swift.max(min, x))
35 | }
36 |
37 | func clamp(_ x: Double, min: Double, max: Double) -> Double {
38 | return Double(clamp(CGFloat(x), min: CGFloat(min), max: CGFloat(max)))
39 | }
40 |
41 | public struct PadDirections: OptionSet {
42 | public let rawValue: Int
43 |
44 | public init(rawValue: Int) {
45 | self.rawValue = rawValue
46 | }
47 |
48 | public static let up = PadDirections(rawValue: 1 << 0)
49 | public static let left = PadDirections(rawValue: 1 << 1)
50 | public static let down = PadDirections(rawValue: 1 << 2)
51 | public static let right = PadDirections(rawValue: 1 << 3)
52 |
53 | public static let all: PadDirections = [.up, .right, .down, .left]
54 |
55 | public enum Axis {
56 | case x, y
57 | }
58 |
59 | var axis: Axis? {
60 | if contains(.left) || contains(.right) {
61 | return .x
62 | } else if contains(.up) || contains(.down) {
63 | return .y
64 | }
65 | return nil
66 | }
67 |
68 | public func bidirectional(on axis: Axis) -> Bool {
69 | switch axis {
70 | case .x:
71 | return contains(.left) && contains(.right)
72 | case .y:
73 | return contains(.up) && contains(.down)
74 | }
75 | }
76 |
77 | public func unidirectional(on axis: Axis) -> Bool {
78 | return !bidirectional(on: axis) && !nondirectional(on: axis)
79 | }
80 |
81 | public func nondirectional(on axis: Axis) -> Bool {
82 | switch axis {
83 | case .x:
84 | return !contains(.left) && !contains(.right)
85 | case .y:
86 | return !contains(.up) && !contains(.down)
87 | }
88 | }
89 | }
90 |
91 | public class PadControl: UIControl {
92 |
93 | typealias Elevation = Double
94 |
95 | var touchPoint: CGPoint?
96 |
97 | let directions: PadDirections
98 | let preferredEdgeLength = 88.0
99 | let dotRadius = 8.0
100 | let selectionGrowthMultiplier = 1.15
101 | let selectionGrowthExponent = 2.0
102 | static let planeCornerRadii = 8.0
103 | let planeCount: Int
104 |
105 | var strokeColor: UIColor {
106 | if #available(iOS 13.0, *) {
107 | return UIColor.systemBackground
108 | }
109 | return .white
110 | }
111 |
112 | var actionColor: UIColor {
113 | return tintColor
114 | }
115 |
116 | let containerLayer = CAShapeLayer()
117 | var planes: [CAShapeLayer]? {
118 | return containerLayer.sublayers as? [CAShapeLayer]
119 | }
120 |
121 | public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
122 | super.traitCollectionDidChange(previousTraitCollection)
123 | self.setSelected(isSelected)
124 | }
125 |
126 | public init(directions: PadDirections, planes: Int = 4) {
127 | self.directions = directions
128 | self.planeCount = planes
129 |
130 | super.init(frame: .zero)
131 |
132 | preparePlanes(planeCount)
133 | tintColorDidChange()
134 | }
135 |
136 | override public func tintColorDidChange() {
137 | isSelected = false
138 | }
139 |
140 | override public func layoutSubviews() {
141 | movePlanes(to: peakOrigin)
142 | }
143 |
144 | override public var isSelected: Bool {
145 | didSet {
146 | setSelected(isSelected)
147 | }
148 | }
149 |
150 | func setSelected(_ isSelected: Bool) {
151 | planes?.forEach { planeLayer in
152 |
153 | let elevation = planeElevation(for: planeLayer)
154 | let opacity = isSelected ? CGFloat(planeOpacity(for: elevation)) : CGFloat(1.0)
155 |
156 | let isBaseLayer = planeElevation(for: planeLayer) == 0
157 | let unselectedAlpha: CGFloat = isBaseLayer ? 0.1 : 0.0
158 | let fillColor = actionColor.withAlphaComponent(isSelected ? opacity : unselectedAlpha)
159 |
160 | planeLayer.lineWidth = 2.0
161 | planeLayer.strokeColor = strokeColor.cgColor
162 | planeLayer.fillColor = fillColor.cgColor
163 |
164 | if let sublayers = planeLayer.sublayers,
165 | let dotLayer = sublayers.first as? CAShapeLayer {
166 | dotLayer.fillColor = isSelected ? strokeColor.cgColor : actionColor.cgColor
167 | }
168 | }
169 | }
170 |
171 | @objc (planeElevationForIndex:)
172 | func planeElevation(for index: Int) -> Elevation {
173 | return Double(index) / max(1, Double(planeCount) - 1)
174 | }
175 |
176 | @objc (planeElevationForPlane:)
177 | func planeElevation(for plane: CAShapeLayer) -> Elevation {
178 |
179 | guard let index = planes?.firstIndex(of: plane) else {
180 | return 0.0
181 | }
182 | return planeElevation(for: index)
183 | }
184 |
185 | func planeOpacity(for elevation: Elevation) -> Float {
186 | let opacity = Float(max(0.1, elevation))
187 | return opacity
188 | }
189 |
190 | public func value(forAxis axis: PadDirections.Axis) -> Double {
191 | switch axis {
192 | case .y:
193 | return abs(value(forDirection: .up) - value(forDirection: .down))
194 | case .x:
195 | return abs(value(forDirection: .right) - value(forDirection: .left))
196 | }
197 | }
198 |
199 | public func value(forDirection direction: PadDirections) -> Double {
200 |
201 | guard
202 | let touchPoint = touchPoint,
203 | directions.contains(direction),
204 | let axis = direction.axis
205 | else {
206 | return 0.0
207 | }
208 |
209 | let size = bounds.size
210 | var value = 0.0
211 |
212 | switch axis {
213 | case .x:
214 | let width = Double(size.width)
215 | let bi = directions.bidirectional(on: .x)
216 | let widthMultiplier = bi ? 0.5 : 1.0
217 | let xSubtract: Double = bi ? 1 : 0
218 | let totalValue = Double(touchPoint.x) / (width * widthMultiplier)
219 | value = direction.contains(.right) ? totalValue - xSubtract: 1 - totalValue
220 | case .y:
221 | let height = Double(size.height)
222 | let bi = directions.bidirectional(on: .y)
223 | let heightMultiplier = bi ? 0.5 : 1.0
224 | let ySubtract: Double = bi ? 1 : 0
225 | let totalValue = Double(touchPoint.y) / (height * heightMultiplier)
226 | value = direction.contains(.down) ? totalValue - ySubtract: 1 - totalValue
227 | }
228 | return clamp(value, min: 0, max: 1)
229 | }
230 |
231 | func preparePlanes(_ count: Int) {
232 |
233 | backgroundColor = strokeColor
234 |
235 | for i in 0.. CAShapeLayer {
245 |
246 | let path = planePath(for: elevation)
247 | let planeLayer = CAShapeLayer()
248 | planeLayer.path = path.cgPath
249 |
250 | if elevation == 1.0 {
251 | let dotLayer = CAShapeLayer()
252 | planeLayer.addSublayer(dotLayer)
253 | }
254 |
255 | return planeLayer
256 | }
257 |
258 | func movePlanes(to point: CGPoint, animated: Bool = false) {
259 |
260 | sendActions(for: .valueChanged)
261 |
262 | guard let planes = self.planes else {
263 | return
264 | }
265 | planes.forEach { plane in
266 | let elevation = planeElevation(for: plane)
267 | let newPath = self.planePath(for: elevation).cgPath
268 |
269 | if animated {
270 | animatePath(for: plane, to: newPath)
271 | } else {
272 | plane.path = newPath
273 | }
274 |
275 | if let sublayers = plane.sublayers, let dotLayer = sublayers.first as? CAShapeLayer {
276 | let r = CGFloat(dotRadius)
277 | let dotRect = CGRect(origin: CGPoint(x: peakOrigin.x + peakSize.width * 0.5 - r,
278 | y: peakOrigin.y + peakSize.height * 0.5 - r),
279 | size: CGSize(width: dotRadius * 2.0,
280 | height: dotRadius * 2.0))
281 | let dotPath = UIBezierPath(ovalIn: dotRect).cgPath
282 |
283 | if animated {
284 | animatePath(for: dotLayer, to: dotPath)
285 | } else {
286 | dotLayer.path = dotPath
287 | }
288 | }
289 | }
290 | }
291 |
292 | func animatePath(for shapeLayer: CAShapeLayer, to path: CGPath, withDuration duration: TimeInterval = 0.05) {
293 |
294 | let animation = CABasicAnimation(keyPath: "path")
295 | animation.toValue = path
296 | animation.duration = duration
297 | animation.fillMode = CAMediaTimingFillMode.forwards
298 | shapeLayer.add(animation, forKey: nil)
299 | shapeLayer.path = path
300 | }
301 |
302 | public override func touchesBegan(_ touches: Set, with event: UIEvent?) {
303 |
304 | guard let touchPoint = touches.first?.location(in: self) else {
305 | return
306 | }
307 |
308 | isSelected = true
309 | let point = clampTouchPoint(touchPoint)
310 | self.touchPoint = point
311 | movePlanes(to: point, animated: false)
312 | }
313 |
314 | public override func touchesMoved(_ touches: Set, with event: UIEvent?) {
315 |
316 | if let touchPoint = touches.first?.location(in: self) {
317 | let point = clampTouchPoint(touchPoint)
318 | self.touchPoint = point
319 | movePlanes(to: point)
320 | }
321 | }
322 |
323 | public override func touchesEnded(_ touches: Set, with event: UIEvent?) {
324 |
325 | isSelected = false
326 | touchPoint = nil
327 | movePlanes(to: restingPoint, animated: true)
328 | }
329 |
330 | func planeRect(for elevation: Elevation) -> CGRect {
331 |
332 | let baseRect = bounds
333 | let baseOrigin = baseRect.origin
334 | let baseSize = baseRect.size
335 |
336 | let elevation = CGFloat(elevation)
337 | let exponent = CGFloat(isSelected ? selectionGrowthExponent : 1.0)
338 |
339 | let origin = CGPoint(x: lerp(baseOrigin.x, peakOrigin.x, elevation, exponent),
340 | y: lerp(baseOrigin.y, peakOrigin.y, elevation, exponent))
341 | let size = CGSize(width: lerp(baseSize.width, peakSize.width, elevation, exponent),
342 | height: lerp(baseSize.height, peakSize.height, elevation, exponent))
343 | return CGRect(origin: origin, size: size)
344 | }
345 |
346 | func planePath(for elevation: Elevation) -> UIBezierPath {
347 | let rect = planeRect(for: elevation)
348 | return UIBezierPath(roundedRect: rect, cornerRadius: CGFloat(PadControl.planeCornerRadii))
349 | }
350 |
351 | var peakOrigin: CGPoint {
352 |
353 | let size = bounds.size
354 | let center = touchPoint ?? restingPoint
355 |
356 | let d = directions
357 |
358 | let xOffset = d.bidirectional(on: .x) ? 0.5 : d.contains(.right) ? 1.0 : 0.0
359 | let yOffset = d.bidirectional(on: .y) ? 0.5 : d.contains(.down) ? 1.0 : 0.0
360 |
361 | let x = center.x - peakSize.width * CGFloat(xOffset)
362 | let y = center.y - peakSize.height * CGFloat(yOffset)
363 |
364 | let boundedX = max(0.0, min(x, size.width - peakSize.width))
365 | let boundedY = max(0.0, min(y, size.height - peakSize.height))
366 |
367 | return CGPoint(x: boundedX, y: boundedY)
368 | }
369 |
370 | var peakSize: CGSize {
371 |
372 | let size = bounds.size
373 | let fullWidth = Double(size.width)
374 | let fullHeight = Double(size.height)
375 | let d = directions
376 |
377 | let xValue = value(forAxis: .x)
378 | let minWidth = min(preferredEdgeLength, fullWidth * 0.25)
379 | let xEdgeLength = isSelected ? minWidth * selectionGrowthMultiplier : minWidth
380 | let dynamicWidth = max(xEdgeLength, fullWidth * xValue)
381 |
382 | let yValue = value(forAxis: .y)
383 | let minHeight = min(preferredEdgeLength, fullHeight * 0.25)
384 | let yEdgeLength = isSelected ? minHeight * selectionGrowthMultiplier : minHeight
385 | let dynamicHeight = max(yEdgeLength, fullHeight * yValue)
386 |
387 | let width = d.nondirectional(on: .x) ? fullWidth : (d.unidirectional(on: .x) ? dynamicWidth : minWidth)
388 | let height = d.nondirectional(on: .y) ? fullHeight : (d.unidirectional(on: .y) ? dynamicHeight : minHeight)
389 |
390 | return CGSize(width: width, height: height)
391 | }
392 |
393 | var restingPoint: CGPoint {
394 |
395 | let rect = bounds
396 |
397 | let fullWidth = Double(rect.size.width)
398 | let fullHeight = Double(rect.size.height)
399 |
400 | let d = directions
401 |
402 | let percentX = d.contains(.left) ? (d.contains(.right) ? 0.5 : 1.0) : 0.0
403 | let percentY = d.contains(.up) ? (d.contains(.down) ? 0.5 : 1.0) : 0.0
404 |
405 | let x = (fullWidth * percentX)
406 | let y = (fullHeight * percentY)
407 |
408 | return CGPoint(x: x, y: y)
409 | }
410 |
411 | func clampTouchPoint(_ touchPoint: CGPoint) -> CGPoint {
412 |
413 | let d = directions
414 |
415 | let size = bounds.size
416 |
417 | let (minX, maxX) = (CGFloat(0.0), size.width)
418 | let (minY, maxY) = (CGFloat(0.0), size.height)
419 |
420 | let x = d.nondirectional(on: .x) ? size.width * 0.5 : clamp(touchPoint.x, min: minX, max: maxX)
421 | let y = d.nondirectional(on: .y) ? size.height * 0.5 : clamp(touchPoint.y, min: minY, max: maxY)
422 |
423 | return CGPoint(x: x, y: y)
424 | }
425 |
426 | public required init?(coder aDecoder: NSCoder) {
427 | fatalError("init(directions:) must be used")
428 | }
429 | }
430 |
431 |
--------------------------------------------------------------------------------
/pad-bl.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gabrieloc/PadControl/d2292b4ff6ceae798719964a62b1bf413364a3a7/pad-bl.gif
--------------------------------------------------------------------------------
/pad-omni.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gabrieloc/PadControl/d2292b4ff6ceae798719964a62b1bf413364a3a7/pad-omni.gif
--------------------------------------------------------------------------------
/pad-x.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gabrieloc/PadControl/d2292b4ff6ceae798719964a62b1bf413364a3a7/pad-x.gif
--------------------------------------------------------------------------------
/pad-y.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gabrieloc/PadControl/d2292b4ff6ceae798719964a62b1bf413364a3a7/pad-y.gif
--------------------------------------------------------------------------------