├── .swift-version ├── LICENSE ├── README.md ├── SizeSlideButton-Demo.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ ├── John.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── jcardasi.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ ├── John.xcuserdatad │ └── xcschemes │ │ ├── ColorSizeSlider.xcscheme │ │ └── xcschememanagement.plist │ └── jcardasi.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── SizeSlideButton-Demo.xcscheme │ └── xcschememanagement.plist ├── SizeSlideButton-Demo ├── AppDelegate.swift ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── Info.plist └── ViewController.swift ├── SizeSlideButton.podspec └── SizeSlideButton.swift /.swift-version: -------------------------------------------------------------------------------- 1 | 3.0 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Jonathan Cardasis 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 | # SizeSlideButton 2 | ![Supported Version](https://img.shields.io/badge/Swift-3-yellow.svg) 3 | ![Platform](https://img.shields.io/badge/platform-iOS-lightgray.svg) 4 | ![License](https://img.shields.io/badge/license-MIT-blue.svg) 5 | ![CocoaPods](https://img.shields.io/badge/CocoaPods-1.5-green.svg) 6 | 7 | A fun Swift UIControl for picking a size. 8 | ![Demo Animation](../assets/demo.gif?raw=true) 9 | 10 | ## Installation 11 | ### Cocoapods 12 | Add `pod 'SizeSlideButton'` to your Podfile. 13 | ### Manually 14 | Add the `SizeSlideButton.swift` class to your project. 15 | 16 | ## Examples 17 | ```Swift 18 | let condensedFrame = CGRect(x: 280, y: 70, width: 32, height: 32) //Width and Height should be equal 19 | let fancyControl = SizeSlideButton(condensedFrame: condensedFrame) 20 | 21 | fancyControl.trackColor = UIColor(red: 38/255, green: 50/255, blue: 56/255, alpha: 1) 22 | fancyControl.handle.color = UIColor.white 23 | fancyControl.addTarget(self, action: #selector(newSizeSelected), for: .touchDragFinished) 24 | fancyControl.addTarget(self, action: #selector(sizeSliderTapped), for: .touchUpInside) 25 | self.view.addSubview(fancyControl) 26 | 27 | 28 | func newSizeSelected(_ sender: SizeSlideButton){ 29 | //Do something once a size is selected and the control let go 30 | // You can now use the senders .value for a value between 0 and 1 31 | // Or use the handle's height (handle.height) as a multiplier for size 32 | print("Value: \(sender.value)") 33 | print("Size multiplier: \(sender.handle.height)") 34 | 35 | // Ex: self.paintbrush.brushSize = kDefaultBrushSize * sender.handle.height 36 | } 37 | 38 | func sizeSliderTapped(_ sender: SizeSlideButton){ 39 | //Do something when the button is tapped 40 | print("Tip Tap") 41 | } 42 | ``` 43 | Output: 44 | 45 | 46 | 47 | ### Via Interface Builder 48 | SizeSlideButton can also be created right from the Interface Builder and allows basic properties to be edited. 49 | 50 | 51 | 52 | ## Documentation 53 | + `init(condensedFrame: CGRect)` will set the frame of where the condensed frame should lie (width and height should be equal) 54 | + `init(frame: CGRect)` will set the frame of where the frame should lie when fully expanded 55 | + `currentSize` to obtain the size the person has selected 56 | + `handlePadding` sets the padding around the handle (default is the left side's radius). Works best as <= leftSideRadius 57 | + `handle.color` is the slider's handle color 58 | + `handle.height`/`handle.width` gets the sizes of the handle and can be best used as a multiplier against a constant default size 59 | + `trackColor` is the color of the track 60 | + `value` is a value between 0 and 1.0 of where the handle was positioned relative on the track 61 | + `leftSideRadius` and `rightSideRadius` return the radii of the left and right sides of the frame when expanded 62 | + `currentState` returns if the control is condensed or expanded 63 | + `animationType` specifies the animation type used when letting go of the control 64 | 65 | ## Supported UIControlEvents 66 | + `touchUpInside` is called when a tap on the control is released 67 | + `valueChanged` is called when the slider is moved 68 | + `touchDown` is called both when a long press is detected or a regular touch is detected 69 | + `touchDragFinished` (A custom UIControlEvent) is called when the slider has selected a new value and been released 70 | 71 | 72 | ## License 73 | SizeSlideButton is available under the MIT license. See the LICENSE file for more info. 74 | -------------------------------------------------------------------------------- /SizeSlideButton-Demo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 35B116E41D25C44000B0C925 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35B116E31D25C44000B0C925 /* AppDelegate.swift */; }; 11 | 35B116E61D25C44000B0C925 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35B116E51D25C44000B0C925 /* ViewController.swift */; }; 12 | 35B116E91D25C44000B0C925 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 35B116E71D25C44000B0C925 /* Main.storyboard */; }; 13 | 35B116EB1D25C44000B0C925 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 35B116EA1D25C44000B0C925 /* Assets.xcassets */; }; 14 | 35B116EE1D25C44000B0C925 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 35B116EC1D25C44000B0C925 /* LaunchScreen.storyboard */; }; 15 | 35B117121D25C46600B0C925 /* SizeSlideButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35B117111D25C46600B0C925 /* SizeSlideButton.swift */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | 35B116E01D25C44000B0C925 /* SizeSlideButton-Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "SizeSlideButton-Demo.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 20 | 35B116E31D25C44000B0C925 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 21 | 35B116E51D25C44000B0C925 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 22 | 35B116E81D25C44000B0C925 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 23 | 35B116EA1D25C44000B0C925 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 24 | 35B116ED1D25C44000B0C925 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 25 | 35B116EF1D25C44000B0C925 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 26 | 35B117111D25C46600B0C925 /* SizeSlideButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SizeSlideButton.swift; path = ../SizeSlideButton.swift; sourceTree = ""; }; 27 | /* End PBXFileReference section */ 28 | 29 | /* Begin PBXFrameworksBuildPhase section */ 30 | 35B116DD1D25C44000B0C925 /* Frameworks */ = { 31 | isa = PBXFrameworksBuildPhase; 32 | buildActionMask = 2147483647; 33 | files = ( 34 | ); 35 | runOnlyForDeploymentPostprocessing = 0; 36 | }; 37 | /* End PBXFrameworksBuildPhase section */ 38 | 39 | /* Begin PBXGroup section */ 40 | 35B116D71D25C44000B0C925 = { 41 | isa = PBXGroup; 42 | children = ( 43 | 35B116E21D25C44000B0C925 /* SizeSlideButton-Demo */, 44 | 35B116E11D25C44000B0C925 /* Products */, 45 | ); 46 | sourceTree = ""; 47 | }; 48 | 35B116E11D25C44000B0C925 /* Products */ = { 49 | isa = PBXGroup; 50 | children = ( 51 | 35B116E01D25C44000B0C925 /* SizeSlideButton-Demo.app */, 52 | ); 53 | name = Products; 54 | sourceTree = ""; 55 | }; 56 | 35B116E21D25C44000B0C925 /* SizeSlideButton-Demo */ = { 57 | isa = PBXGroup; 58 | children = ( 59 | 35B116E31D25C44000B0C925 /* AppDelegate.swift */, 60 | 35B116E51D25C44000B0C925 /* ViewController.swift */, 61 | 35B117111D25C46600B0C925 /* SizeSlideButton.swift */, 62 | 35B116E71D25C44000B0C925 /* Main.storyboard */, 63 | 35B116EA1D25C44000B0C925 /* Assets.xcassets */, 64 | 35B116EC1D25C44000B0C925 /* LaunchScreen.storyboard */, 65 | 35B116EF1D25C44000B0C925 /* Info.plist */, 66 | ); 67 | path = "SizeSlideButton-Demo"; 68 | sourceTree = ""; 69 | }; 70 | /* End PBXGroup section */ 71 | 72 | /* Begin PBXNativeTarget section */ 73 | 35B116DF1D25C44000B0C925 /* SizeSlideButton-Demo */ = { 74 | isa = PBXNativeTarget; 75 | buildConfigurationList = 35B117081D25C44000B0C925 /* Build configuration list for PBXNativeTarget "SizeSlideButton-Demo" */; 76 | buildPhases = ( 77 | 35B116DC1D25C44000B0C925 /* Sources */, 78 | 35B116DD1D25C44000B0C925 /* Frameworks */, 79 | 35B116DE1D25C44000B0C925 /* Resources */, 80 | ); 81 | buildRules = ( 82 | ); 83 | dependencies = ( 84 | ); 85 | name = "SizeSlideButton-Demo"; 86 | productName = "SizeSlideButton-Demo"; 87 | productReference = 35B116E01D25C44000B0C925 /* SizeSlideButton-Demo.app */; 88 | productType = "com.apple.product-type.application"; 89 | }; 90 | /* End PBXNativeTarget section */ 91 | 92 | /* Begin PBXProject section */ 93 | 35B116D81D25C44000B0C925 /* Project object */ = { 94 | isa = PBXProject; 95 | attributes = { 96 | LastSwiftUpdateCheck = 0730; 97 | LastUpgradeCheck = 0820; 98 | ORGANIZATIONNAME = "Cardasis, Jonathan (J.)"; 99 | TargetAttributes = { 100 | 35B116DF1D25C44000B0C925 = { 101 | CreatedOnToolsVersion = 7.3; 102 | LastSwiftMigration = 0820; 103 | }; 104 | }; 105 | }; 106 | buildConfigurationList = 35B116DB1D25C44000B0C925 /* Build configuration list for PBXProject "SizeSlideButton-Demo" */; 107 | compatibilityVersion = "Xcode 3.2"; 108 | developmentRegion = English; 109 | hasScannedForEncodings = 0; 110 | knownRegions = ( 111 | en, 112 | Base, 113 | ); 114 | mainGroup = 35B116D71D25C44000B0C925; 115 | productRefGroup = 35B116E11D25C44000B0C925 /* Products */; 116 | projectDirPath = ""; 117 | projectRoot = ""; 118 | targets = ( 119 | 35B116DF1D25C44000B0C925 /* SizeSlideButton-Demo */, 120 | ); 121 | }; 122 | /* End PBXProject section */ 123 | 124 | /* Begin PBXResourcesBuildPhase section */ 125 | 35B116DE1D25C44000B0C925 /* Resources */ = { 126 | isa = PBXResourcesBuildPhase; 127 | buildActionMask = 2147483647; 128 | files = ( 129 | 35B116EE1D25C44000B0C925 /* LaunchScreen.storyboard in Resources */, 130 | 35B116EB1D25C44000B0C925 /* Assets.xcassets in Resources */, 131 | 35B116E91D25C44000B0C925 /* Main.storyboard in Resources */, 132 | ); 133 | runOnlyForDeploymentPostprocessing = 0; 134 | }; 135 | /* End PBXResourcesBuildPhase section */ 136 | 137 | /* Begin PBXSourcesBuildPhase section */ 138 | 35B116DC1D25C44000B0C925 /* Sources */ = { 139 | isa = PBXSourcesBuildPhase; 140 | buildActionMask = 2147483647; 141 | files = ( 142 | 35B116E61D25C44000B0C925 /* ViewController.swift in Sources */, 143 | 35B117121D25C46600B0C925 /* SizeSlideButton.swift in Sources */, 144 | 35B116E41D25C44000B0C925 /* AppDelegate.swift in Sources */, 145 | ); 146 | runOnlyForDeploymentPostprocessing = 0; 147 | }; 148 | /* End PBXSourcesBuildPhase section */ 149 | 150 | /* Begin PBXVariantGroup section */ 151 | 35B116E71D25C44000B0C925 /* Main.storyboard */ = { 152 | isa = PBXVariantGroup; 153 | children = ( 154 | 35B116E81D25C44000B0C925 /* Base */, 155 | ); 156 | name = Main.storyboard; 157 | sourceTree = ""; 158 | }; 159 | 35B116EC1D25C44000B0C925 /* LaunchScreen.storyboard */ = { 160 | isa = PBXVariantGroup; 161 | children = ( 162 | 35B116ED1D25C44000B0C925 /* Base */, 163 | ); 164 | name = LaunchScreen.storyboard; 165 | sourceTree = ""; 166 | }; 167 | /* End PBXVariantGroup section */ 168 | 169 | /* Begin XCBuildConfiguration section */ 170 | 35B117061D25C44000B0C925 /* Debug */ = { 171 | isa = XCBuildConfiguration; 172 | buildSettings = { 173 | ALWAYS_SEARCH_USER_PATHS = NO; 174 | CLANG_ANALYZER_NONNULL = YES; 175 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 176 | CLANG_CXX_LIBRARY = "libc++"; 177 | CLANG_ENABLE_MODULES = YES; 178 | CLANG_ENABLE_OBJC_ARC = YES; 179 | CLANG_WARN_BOOL_CONVERSION = YES; 180 | CLANG_WARN_CONSTANT_CONVERSION = YES; 181 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 182 | CLANG_WARN_EMPTY_BODY = YES; 183 | CLANG_WARN_ENUM_CONVERSION = YES; 184 | CLANG_WARN_INFINITE_RECURSION = YES; 185 | CLANG_WARN_INT_CONVERSION = YES; 186 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 187 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 188 | CLANG_WARN_UNREACHABLE_CODE = YES; 189 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 190 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 191 | COPY_PHASE_STRIP = NO; 192 | DEBUG_INFORMATION_FORMAT = dwarf; 193 | ENABLE_STRICT_OBJC_MSGSEND = YES; 194 | ENABLE_TESTABILITY = YES; 195 | GCC_C_LANGUAGE_STANDARD = gnu99; 196 | GCC_DYNAMIC_NO_PIC = NO; 197 | GCC_NO_COMMON_BLOCKS = YES; 198 | GCC_OPTIMIZATION_LEVEL = 0; 199 | GCC_PREPROCESSOR_DEFINITIONS = ( 200 | "DEBUG=1", 201 | "$(inherited)", 202 | ); 203 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 204 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 205 | GCC_WARN_UNDECLARED_SELECTOR = YES; 206 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 207 | GCC_WARN_UNUSED_FUNCTION = YES; 208 | GCC_WARN_UNUSED_VARIABLE = YES; 209 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 210 | MTL_ENABLE_DEBUG_INFO = YES; 211 | ONLY_ACTIVE_ARCH = YES; 212 | SDKROOT = iphoneos; 213 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 214 | }; 215 | name = Debug; 216 | }; 217 | 35B117071D25C44000B0C925 /* Release */ = { 218 | isa = XCBuildConfiguration; 219 | buildSettings = { 220 | ALWAYS_SEARCH_USER_PATHS = NO; 221 | CLANG_ANALYZER_NONNULL = YES; 222 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 223 | CLANG_CXX_LIBRARY = "libc++"; 224 | CLANG_ENABLE_MODULES = YES; 225 | CLANG_ENABLE_OBJC_ARC = YES; 226 | CLANG_WARN_BOOL_CONVERSION = YES; 227 | CLANG_WARN_CONSTANT_CONVERSION = YES; 228 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 229 | CLANG_WARN_EMPTY_BODY = YES; 230 | CLANG_WARN_ENUM_CONVERSION = YES; 231 | CLANG_WARN_INFINITE_RECURSION = YES; 232 | CLANG_WARN_INT_CONVERSION = YES; 233 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 234 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 235 | CLANG_WARN_UNREACHABLE_CODE = YES; 236 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 237 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 238 | COPY_PHASE_STRIP = NO; 239 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 240 | ENABLE_NS_ASSERTIONS = NO; 241 | ENABLE_STRICT_OBJC_MSGSEND = YES; 242 | GCC_C_LANGUAGE_STANDARD = gnu99; 243 | GCC_NO_COMMON_BLOCKS = YES; 244 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 245 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 246 | GCC_WARN_UNDECLARED_SELECTOR = YES; 247 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 248 | GCC_WARN_UNUSED_FUNCTION = YES; 249 | GCC_WARN_UNUSED_VARIABLE = YES; 250 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 251 | MTL_ENABLE_DEBUG_INFO = NO; 252 | SDKROOT = iphoneos; 253 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 254 | VALIDATE_PRODUCT = YES; 255 | }; 256 | name = Release; 257 | }; 258 | 35B117091D25C44000B0C925 /* Debug */ = { 259 | isa = XCBuildConfiguration; 260 | buildSettings = { 261 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 262 | INFOPLIST_FILE = "SizeSlideButton-Demo/Info.plist"; 263 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 264 | PRODUCT_BUNDLE_IDENTIFIER = "com.jonathancardasis.SizeSlideButton-Demo"; 265 | PRODUCT_NAME = "$(TARGET_NAME)"; 266 | SWIFT_VERSION = 3.0; 267 | }; 268 | name = Debug; 269 | }; 270 | 35B1170A1D25C44000B0C925 /* Release */ = { 271 | isa = XCBuildConfiguration; 272 | buildSettings = { 273 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 274 | INFOPLIST_FILE = "SizeSlideButton-Demo/Info.plist"; 275 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 276 | PRODUCT_BUNDLE_IDENTIFIER = "com.jonathancardasis.SizeSlideButton-Demo"; 277 | PRODUCT_NAME = "$(TARGET_NAME)"; 278 | SWIFT_VERSION = 3.0; 279 | }; 280 | name = Release; 281 | }; 282 | /* End XCBuildConfiguration section */ 283 | 284 | /* Begin XCConfigurationList section */ 285 | 35B116DB1D25C44000B0C925 /* Build configuration list for PBXProject "SizeSlideButton-Demo" */ = { 286 | isa = XCConfigurationList; 287 | buildConfigurations = ( 288 | 35B117061D25C44000B0C925 /* Debug */, 289 | 35B117071D25C44000B0C925 /* Release */, 290 | ); 291 | defaultConfigurationIsVisible = 0; 292 | defaultConfigurationName = Release; 293 | }; 294 | 35B117081D25C44000B0C925 /* Build configuration list for PBXNativeTarget "SizeSlideButton-Demo" */ = { 295 | isa = XCConfigurationList; 296 | buildConfigurations = ( 297 | 35B117091D25C44000B0C925 /* Debug */, 298 | 35B1170A1D25C44000B0C925 /* Release */, 299 | ); 300 | defaultConfigurationIsVisible = 0; 301 | defaultConfigurationName = Release; 302 | }; 303 | /* End XCConfigurationList section */ 304 | }; 305 | rootObject = 35B116D81D25C44000B0C925 /* Project object */; 306 | } 307 | -------------------------------------------------------------------------------- /SizeSlideButton-Demo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SizeSlideButton-Demo.xcodeproj/project.xcworkspace/xcuserdata/John.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joncardasis/SizeSlideButton/c3c848f51b283a6d5aca1f71a520834ca1267357/SizeSlideButton-Demo.xcodeproj/project.xcworkspace/xcuserdata/John.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /SizeSlideButton-Demo.xcodeproj/project.xcworkspace/xcuserdata/jcardasi.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joncardasis/SizeSlideButton/c3c848f51b283a6d5aca1f71a520834ca1267357/SizeSlideButton-Demo.xcodeproj/project.xcworkspace/xcuserdata/jcardasi.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /SizeSlideButton-Demo.xcodeproj/xcuserdata/John.xcuserdatad/xcschemes/ColorSizeSlider.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 43 | 49 | 50 | 51 | 52 | 53 | 59 | 60 | 61 | 62 | 63 | 64 | 74 | 76 | 82 | 83 | 84 | 85 | 86 | 87 | 93 | 95 | 101 | 102 | 103 | 104 | 106 | 107 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /SizeSlideButton-Demo.xcodeproj/xcuserdata/John.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | ColorSizeSlider.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 35B116DF1D25C44000B0C925 16 | 17 | primary 18 | 19 | 20 | 35B116F31D25C44000B0C925 21 | 22 | primary 23 | 24 | 25 | 35B116FE1D25C44000B0C925 26 | 27 | primary 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /SizeSlideButton-Demo.xcodeproj/xcuserdata/jcardasi.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /SizeSlideButton-Demo.xcodeproj/xcuserdata/jcardasi.xcuserdatad/xcschemes/SizeSlideButton-Demo.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /SizeSlideButton-Demo.xcodeproj/xcuserdata/jcardasi.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | SizeSlideButton-Demo.xcscheme 8 | 9 | orderHint 10 | 1 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 35B116DF1D25C44000B0C925 16 | 17 | primary 18 | 19 | 20 | 35B116F31D25C44000B0C925 21 | 22 | primary 23 | 24 | 25 | 35B116FE1D25C44000B0C925 26 | 27 | primary 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /SizeSlideButton-Demo/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // 4 | // Created by Jonathan Cardasis on 6/30/16. 5 | // Copyright © 2016 Jonathan Cardasis. All rights reserved. 6 | // 7 | 8 | import UIKit 9 | 10 | @UIApplicationMain 11 | class AppDelegate: UIResponder, UIApplicationDelegate { 12 | 13 | var window: UIWindow? 14 | 15 | 16 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 17 | // Override point for customization after application launch. 18 | return true 19 | } 20 | 21 | func applicationWillResignActive(_ application: UIApplication) { 22 | // 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. 23 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 24 | } 25 | 26 | func applicationDidEnterBackground(_ application: UIApplication) { 27 | // 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. 28 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 29 | } 30 | 31 | func applicationWillEnterForeground(_ application: UIApplication) { 32 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 33 | } 34 | 35 | func applicationDidBecomeActive(_ application: UIApplication) { 36 | // 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. 37 | } 38 | 39 | func applicationWillTerminate(_ application: UIApplication) { 40 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 41 | } 42 | 43 | 44 | } 45 | 46 | -------------------------------------------------------------------------------- /SizeSlideButton-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 | "info" : { 45 | "version" : 1, 46 | "author" : "xcode" 47 | } 48 | } -------------------------------------------------------------------------------- /SizeSlideButton-Demo/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /SizeSlideButton-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 | 27 | 28 | -------------------------------------------------------------------------------- /SizeSlideButton-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 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /SizeSlideButton-Demo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 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 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /SizeSlideButton-Demo/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // 4 | // Created by Jonathan Cardasis on 6/30/16. 5 | // Copyright © 2016 Jonathan Cardasis. All rights reserved. 6 | // 7 | 8 | import UIKit 9 | 10 | /* A test viewcontroller */ 11 | class ViewController: UIViewController { 12 | 13 | var debugBtn: UIButton! 14 | 15 | override func viewDidLoad() { 16 | super.viewDidLoad() 17 | 18 | let condensedFrame = CGRect(x: 280, y: 70, width: 32, height: 32) //Width and Height should be equal 19 | let fancyControl = SizeSlideButton(condensedFrame: condensedFrame) 20 | 21 | //let fancyControl = SizeSlideButton(frame: CGRect(x: 10, y: 30, width: 352, height: 32)) 22 | 23 | /* Additional Setup */ 24 | fancyControl.trackColor = UIColor.white 25 | fancyControl.handle.color = UIColor(red: 255/255.0, green: 111/255.0, blue: 0, alpha: 1) 26 | //fancyControl.handlePadding = 0.0 //Add no extra padding around the handle 27 | //fancyControl.animationType = .linear 28 | 29 | fancyControl.addTarget(self, action: #selector(newSizeSelected), for: .touchDragFinished) 30 | fancyControl.addTarget(self, action: #selector(sizeSliderTapped), for: .touchUpInside) 31 | self.view.addSubview(fancyControl) 32 | 33 | /* Test for moving frame and changing padding */ 34 | //fancyControl.frame = CGRect(x: 50, y: 130, width: 200, height: 50) 35 | //fancyControl.handlePadding = 0.0 36 | 37 | 38 | /* A button to test for click-through on the fancyControl as well as appear when tapped */ 39 | debugBtn = UIButton(frame: CGRect(x: 165, y: 75, width: 100, height: 22)) 40 | debugBtn.setTitle("Tapped", for: UIControlState()) 41 | debugBtn.alpha = 0 42 | debugBtn.addTarget(self, action: #selector(test), for: .touchUpInside) //For testing purposes 43 | self.view.insertSubview(debugBtn, belowSubview: fancyControl) 44 | } 45 | 46 | func newSizeSelected(_ sender: SizeSlideButton){ 47 | //Do something once a size is selected and the control let go 48 | let multipler = sender.handle.height 49 | 50 | print("Value: \(sender.value)") 51 | print("Multiplier: \(multipler)") 52 | } 53 | 54 | func sizeSliderTapped(_ sender: SizeSlideButton){ 55 | //Do something when the button is tapped 56 | 57 | UIView.animate(withDuration: 0.3, animations: { 58 | self.debugBtn.alpha = 1 59 | }, completion: { (done) in 60 | UIView.animate(withDuration: 0.3, delay: 0.65, options: .curveEaseIn, animations: { 61 | self.debugBtn.alpha = 0 62 | }, completion: nil) 63 | }) 64 | } 65 | 66 | func test(){ 67 | print("Clickarooo!") 68 | } 69 | } 70 | 71 | -------------------------------------------------------------------------------- /SizeSlideButton.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = "SizeSlideButton" 3 | s.version = "1.5" 4 | s.summary = "A Swift UI Component for picking a size." 5 | 6 | s.description = <<-DESC 7 | SizeSlideButton is a Swift UIControl which allows for picking a size on the control. It recognizes long presses to activate the slider and a tap for a button TouchUp event. 8 | DESC 9 | 10 | s.homepage = "https://github.com/joncardasis/SizeSlideButton.git" 11 | s.license = { :type => "MIT", :file => "LICENSE" } 12 | s.author = "Jonathan Cardasis" 13 | s.platform = :ios, "9.0" 14 | s.source = { :git => "https://github.com/joncardasis/SizeSlideButton.git", :tag => "1.5" } 15 | s.source_files = "SizeSlideButton.swift" 16 | end 17 | -------------------------------------------------------------------------------- /SizeSlideButton.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SizeSlideButton.swift 3 | // 4 | // Created by Jonathan Cardasis on 6/30/16. 5 | // Copyright © 2016 Jonathan Cardasis. All rights reserved. 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | 26 | import UIKit 27 | 28 | //MARK: - SizeSlideButton Subcomponents 29 | open class SizeSlideHandle: CAShapeLayer { 30 | open var color: UIColor = UIColor(red: 0, green: 122/255, blue: 255/255, alpha: 1){ 31 | didSet{ self.fillColor = color.cgColor } 32 | } 33 | 34 | override open var frame: CGRect { 35 | didSet{ 36 | self.path = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: self.frame.height, height: self.frame.height)).cgPath 37 | } 38 | } 39 | 40 | open var height: CGFloat { 41 | return frame.size.height 42 | } 43 | 44 | open var width: CGFloat { 45 | return frame.size.width 46 | } 47 | 48 | override init() { 49 | super.init() 50 | self.path = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: self.frame.height, height: self.frame.height)).cgPath 51 | self.fillColor = color.cgColor 52 | } 53 | 54 | required public init?(coder aDecoder: NSCoder) { 55 | fatalError("SizeSlideHandle should not be implemnted alone.") 56 | } 57 | } 58 | 59 | //MARK: - SizeSlideButton 60 | public enum SizeSlideButtonState { 61 | case condensed 62 | case expanded 63 | } 64 | 65 | public extension UIControlEvents{ //Add extra control event 66 | /* Note that Apple only reserves 4 values: 0x01000000, 0x02000000, 0x04000000 and 0x08000000 for use by UIControlEventApplicationReserved */ 67 | static public let touchDragFinished = UIControlEvents(rawValue: 0x01000000) 68 | } 69 | 70 | @IBDesignable open class SizeSlideButton: UIControl { 71 | public enum AnimationType { 72 | case spring 73 | case linear 74 | } 75 | 76 | open let shapeMask = CAShapeLayer() 77 | open var handle = SizeSlideHandle() 78 | open var animationType: AnimationType = .spring 79 | open fileprivate(set) var currentState: SizeSlideButtonState = .condensed //default state 80 | 81 | override open var frame: CGRect { 82 | didSet{ 83 | if currentState == .condensed { 84 | shapeMask.path = condensedLayerMaskPath 85 | } else{ 86 | shapeMask.path = expandedLayerMaskPath 87 | } 88 | handle.frame.size = CGSize(width: frame.height, height: frame.height) 89 | handle.position = CGPoint(x: frame.width-rightSideRadius, y: frame.height/2) 90 | } 91 | } 92 | 93 | @IBInspectable open var handlePadding: CGFloat = 0.0 { //padding on sides of the handle 94 | didSet{ 95 | //Adjust size position for delta value 96 | handle.frame.size = CGSize(width: handle.frame.width + (oldValue-handlePadding), height: handle.frame.height + (oldValue-handlePadding)) 97 | handle.position = CGPoint(x: handle.position.x + (handlePadding-oldValue)/2, y: frame.height/2) 98 | } 99 | } 100 | @IBInspectable open var handleColor: UIColor { 101 | get { return handle.color } 102 | set { handle.color = newValue } 103 | } 104 | 105 | open var value: Float { //value which displays between 0 and 1.0 for position on control 106 | get{ 107 | /* Return a value between 0 and 1.0 */ 108 | let input = (((handle.frame.height+handlePadding)/2)/leftSideRadius) 109 | let result = (1/3)*(input - 1) // 1/3 is m(x) 110 | //let result = map((handle.frame.height+handlePadding)/2, leftMin: leftSideRadius, leftMax: rightSideRadius, rightMin: 0, rightMax: frame.width) / frame.width 111 | return Float(result) 112 | } 113 | set{ 114 | /* Adjust view to fit the new value */ 115 | let boundValue = max(0, min(CGFloat(newValue), 1)) //clip value between 0 and 1 116 | 117 | var multiplier: CGFloat 118 | if newValue <= 0.5 { 119 | multiplier = map(boundValue, leftMin: 0, leftMax: 0.5, rightMin: (1/4), rightMax: (5/8)) 120 | }else{ 121 | multiplier = map(boundValue, leftMin: 0.5, leftMax: 1, rightMin: (5/8), rightMax: 1) 122 | } 123 | 124 | let height = frame.height * multiplier 125 | let newSize = CGSize(width: height - handlePadding, height: height - handlePadding) 126 | 127 | handle.frame = CGRect(x: frame.width - rightSideRadius - newSize.width/2, y: self.frame.height/2 - newSize.height/2, width: newSize.width, height: newSize.height) 128 | } 129 | } 130 | 131 | @IBInspectable open var trackColor: UIColor = UIColor.white { 132 | didSet{ backgroundColor = trackColor } 133 | } 134 | open var currentSize: CGFloat { //return the height of the displayed handle indicator 135 | get { return handle.frame.size.height } 136 | } 137 | open var leftSideRadius: CGFloat { 138 | get{ return frame.size.height/8 } 139 | } 140 | open var rightSideRadius: CGFloat { 141 | get{ return frame.size.height/2 } 142 | } 143 | 144 | /* Layer Masks - must share same points for animations */ 145 | var expandedLayerMaskPath: CGPath{ 146 | let path = UIBezierPath() 147 | path.addArc(withCenter: CGPoint(x: leftSideRadius, y: frame.height/2), radius: leftSideRadius, startAngle: CGFloat(M_PI), endAngle: CGFloat(1.5*M_PI), clockwise: true) 148 | path.addArc(withCenter: CGPoint(x: frame.width - rightSideRadius, y: rightSideRadius), radius: rightSideRadius, startAngle: CGFloat(1.5*M_PI), endAngle: 0, clockwise: true) 149 | path.addArc(withCenter: CGPoint(x: frame.width - rightSideRadius, y: frame.height - rightSideRadius), radius: rightSideRadius, startAngle: 0, endAngle: CGFloat(M_PI_2), clockwise: true) 150 | path.addArc(withCenter: CGPoint(x: leftSideRadius, y: frame.height/2), radius: leftSideRadius, startAngle: CGFloat(M_PI_2), endAngle: CGFloat(M_PI), clockwise: true) 151 | path.close() 152 | return path.cgPath 153 | } 154 | 155 | var condensedLayerMaskPath: CGPath{ 156 | let path = UIBezierPath() 157 | path.addArc(withCenter: CGPoint(x: frame.width - rightSideRadius, y: rightSideRadius), radius: rightSideRadius, startAngle: CGFloat(M_PI), endAngle: CGFloat(1.5*M_PI), clockwise: true) 158 | path.addArc(withCenter: CGPoint(x: frame.width - rightSideRadius, y: rightSideRadius), radius: rightSideRadius, startAngle: CGFloat(1.5*M_PI), endAngle: 0, clockwise: true) 159 | path.addArc(withCenter: CGPoint(x: frame.width - rightSideRadius, y: frame.height - rightSideRadius), radius: rightSideRadius, startAngle: 0, endAngle: CGFloat(M_PI_2), clockwise: true) 160 | path.addArc(withCenter: CGPoint(x: frame.width - rightSideRadius, y: frame.height - rightSideRadius), radius: rightSideRadius, startAngle: CGFloat(M_PI_2), endAngle: CGFloat(M_PI), clockwise: true) 161 | path.close() 162 | return path.cgPath 163 | } 164 | 165 | /* Convenience variables */ 166 | var condensedFrame: CGRect{ //Frame representing the condensed view 167 | var properBounds = condensedLayerMaskPath.boundingBox 168 | properBounds.origin.x += frame.origin.x //remap inner frame coords to world coords 169 | properBounds.origin.y += frame.origin.y 170 | return properBounds 171 | } 172 | 173 | 174 | //MARK: Implementation 175 | override public init(frame: CGRect){ 176 | super.init(frame: frame) 177 | commonInit() 178 | } 179 | 180 | public init(condensedFrame: CGRect){ 181 | let dimensionSize = min(condensedFrame.width, condensedFrame.height) 182 | let defaultTrackSize = CGSize(width: dimensionSize * 8, height: dimensionSize) //give a default size if its not specified 183 | super.init(frame: CGRect(x: condensedFrame.origin.x - defaultTrackSize.width + dimensionSize, y: condensedFrame.origin.y, width: defaultTrackSize.width, height: dimensionSize)) 184 | commonInit() 185 | } 186 | 187 | fileprivate func commonInit(){ 188 | trackColor = UIColor.white //default track color 189 | 190 | /* Setup mask layer */ 191 | shapeMask.path = condensedLayerMaskPath 192 | shapeMask.rasterizationScale = UIScreen.main.scale 193 | shapeMask.shouldRasterize = true 194 | self.layer.mask = shapeMask 195 | 196 | /* Set a default handle padding */ 197 | handlePadding = leftSideRadius 198 | 199 | /* Setup Handle */ 200 | handle.frame = CGRect(x: 0, y: 0, width: frame.height - handlePadding, height: frame.height - handlePadding) 201 | handle.position = CGPoint(x: frame.width-rightSideRadius, y: frame.height/2) 202 | handle.actions = ["position" : NSNull(), "bounds" : NSNull(), "path" : NSNull()] //disable implicit animations 203 | self.layer.addSublayer(handle) 204 | 205 | /* Setup Gesture Recognizers */ 206 | let holdGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPressGesture)) 207 | holdGesture.minimumPressDuration = 0.3 208 | 209 | let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture)) 210 | 211 | self.addGestureRecognizer(holdGesture) 212 | self.addGestureRecognizer(tapGesture) 213 | } 214 | 215 | required public init?(coder aDecoder: NSCoder) { 216 | super.init(coder: aDecoder)! 217 | commonInit() 218 | } 219 | 220 | //MARK: Gesture/Touch Controls 221 | func handleLongPressGesture(_ gesture: UILongPressGestureRecognizer){ 222 | 223 | if currentState == .condensed{ 224 | self.sendActions(for: .touchDown) 225 | 226 | /* Animate mask to full glory and change state when completed */ 227 | currentState = .expanded //promise the state will be expanded 228 | self.animateTrack(to: .expanded, velocity: 20, damping: 40) 229 | } 230 | 231 | if gesture.state == .changed{ 232 | let touchLocation = gesture.location(in: self) 233 | self.moveHandle(to: touchLocation) 234 | 235 | /* Trigger event actions */ 236 | self.sendActions(for: .valueChanged) 237 | } 238 | 239 | if gesture.state == .ended{ 240 | let damping: CGFloat = 20 241 | 242 | /* Animate handle to right side position */ 243 | let spring = CASpringAnimation(keyPath: "position.x") 244 | spring.initialVelocity = CGFloat(((1-value) * 2) + 13) //tweaked speed algorithm (faster velocity further to 0) 245 | spring.damping = damping 246 | spring.fromValue = handle.position.x 247 | spring.toValue = frame.width-rightSideRadius 248 | spring.duration = spring.settlingDuration 249 | handle.position = CGPoint(x: frame.width-rightSideRadius, y: handle.position.y) //set final state 250 | if animationType == .linear { 251 | //Simulate a linear movement with a modified mass 252 | spring.mass = 0.2 253 | } 254 | handle.add(spring, forKey: nil) 255 | 256 | /* Animate mask back and change enum state */ 257 | currentState = .condensed //promise the state will become condensed 258 | self.animateTrack(to: .condensed, velocity: spring.initialVelocity, damping: spring.damping) 259 | 260 | /* Trigger event actions */ 261 | self.sendActions(for: .touchDragFinished) 262 | } 263 | } 264 | 265 | 266 | func springAnimateHandle(to position: CGPoint, with damping: CGFloat = 20) { } 267 | 268 | func handleTapGesture(_ gesture: UITapGestureRecognizer){ 269 | if gesture.state == .began{ 270 | self.sendActions(for: .touchDown) 271 | } 272 | else if gesture.state == .ended{ 273 | self.sendActions(for: .touchUpInside) 274 | } 275 | } 276 | 277 | //Override to allow touches through the frame when the extended state is not active 278 | override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool { 279 | //Set so only hit-box area is the condensed frame 280 | let condensedHitBox = CGRect(origin: CGPoint(x: condensedFrame.origin.x - frame.origin.x, y:0), size: condensedFrame.size) //recalculate proper hitbox for condensedframe 281 | return condensedHitBox.contains(point) 282 | } 283 | 284 | // Moves the handle's center to the point in the frame 285 | fileprivate func moveHandle(to touchPoint: CGPoint){ 286 | 287 | /* Recalculate for outside points */ 288 | var point = touchPoint 289 | if point.x < leftSideRadius { 290 | point.x = leftSideRadius 291 | } 292 | else if point.x > self.frame.width-rightSideRadius { 293 | point.x = self.frame.width-rightSideRadius 294 | } 295 | 296 | /* Get a proper multipler for the height */ 297 | // We use 1/8 + 4/8 (5/8) as the median multiplier because the caps are different sizes 298 | // We start at 1/4 for the smallest multiper so it is equal to leftsideradius on the left 299 | var multiplier: CGFloat = 0 300 | if point.x <= frame.width/2 { 301 | multiplier = map(point.x, leftMin: leftSideRadius, leftMax: frame.width/2, rightMin: (1/4), rightMax: (5/8)) 302 | } 303 | else if point.x > frame.width/2 { 304 | multiplier = map(point.x, leftMin: frame.width/2, leftMax: frame.width-rightSideRadius, rightMin: (5/8), rightMax: 1) 305 | } 306 | 307 | let newHandleSize = CGSize(width: frame.height * CGFloat(multiplier) - handlePadding, height: frame.height * CGFloat(multiplier) - handlePadding) 308 | 309 | /* Apply for new size and location */ 310 | handle.frame = CGRect(x: point.x - newHandleSize.width/2, y: self.frame.height/2 - newHandleSize.height/2, width: newHandleSize.width, height: newHandleSize.height) 311 | } 312 | 313 | func animateTrack(to state: SizeSlideButtonState, velocity: CGFloat, damping: CGFloat , completion: ((_ done: Bool) -> ())? = nil) { 314 | guard let layerMask = self.layer.mask else{//protect 315 | completion?(false) 316 | return 317 | } 318 | 319 | let newMaskPath: CGPath 320 | if state == .condensed{ 321 | newMaskPath = condensedLayerMaskPath 322 | }else{ 323 | newMaskPath = expandedLayerMaskPath 324 | } 325 | 326 | CATransaction.begin() 327 | CATransaction.setCompletionBlock({ completion?(true) }) 328 | 329 | let revealAnimation = CASpringAnimation(keyPath: "path") 330 | revealAnimation.initialVelocity = velocity 331 | revealAnimation.damping = damping 332 | revealAnimation.fromValue = shapeMask.path 333 | revealAnimation.toValue = newMaskPath 334 | revealAnimation.duration = revealAnimation.settlingDuration 335 | revealAnimation.isRemovedOnCompletion = false 336 | revealAnimation.fillMode = kCAFillModeForwards 337 | 338 | shapeMask.path = newMaskPath //set final state 339 | 340 | //CATrasaction will not account for self.mask having an animation added even though self.layer.mask points to this variable. We must animate directly on the layer mask. 341 | layerMask.add(revealAnimation, forKey: nil) 342 | 343 | CATransaction.commit() 344 | } 345 | 346 | //MARK: Helper Functions 347 | fileprivate func map(_ value: CGFloat, leftMin:CGFloat, leftMax:CGFloat, rightMin:CGFloat, rightMax: CGFloat) -> CGFloat{ 348 | let leftSpan = leftMax - leftMin 349 | let rightSpan = rightMax - rightMin 350 | let valueScaled = (value - leftMin) / (leftSpan) 351 | 352 | return (valueScaled * rightSpan) + rightMin 353 | } 354 | } 355 | --------------------------------------------------------------------------------