├── .gitignore ├── DVSwitch.podspec ├── DVSwitcherExample.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── xcuserdata │ └── dmitryvolevodz.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── DVSwitcherExample.xcscheme │ └── xcschememanagement.plist ├── DVSwitcherExample ├── AppDelegate.h ├── AppDelegate.m ├── Base.lproj │ ├── LaunchScreen.xib │ └── Main.storyboard ├── DVSwitch.h ├── DVSwitch.m ├── Images.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Info.plist ├── ViewController.h ├── ViewController.m └── main.m ├── DVSwitcherExampleTests ├── DVSwitcherExampleTests.m └── Info.plist ├── LICENSE ├── README.md └── Source ├── DVSwitch.h └── DVSwitch.m /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | build/ 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | *.xccheckout 14 | *.moved-aside 15 | DerivedData 16 | *.hmap 17 | *.ipa 18 | *.xcuserstate 19 | 20 | # CocoaPods 21 | # 22 | # We recommend against adding the Pods directory to your .gitignore. However 23 | # you should judge for yourself, the pros and cons are mentioned at: 24 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 25 | # 26 | # Pods/ 27 | -------------------------------------------------------------------------------- /DVSwitch.podspec: -------------------------------------------------------------------------------- 1 | 2 | Pod::Spec.new do |s| 3 | 4 | s.name = "DVSwitch" 5 | s.version = "1.0.0" 6 | s.summary = "Customizable control based on UISwitch and UISegmentedControl written in Objective-C" 7 | 8 | s.description = <<-DESC 9 | 10 | * Easily customizable control with nice animations 11 | * Supporting pan or swipe interactions 12 | * Requires very little setup - images are not needed 13 | * Beautiful font color inversion effect - label color changes per pixel: 14 | * Automatic adjustment based on number of items 15 | * Inspired by UISegmentedControl and UISwitch 16 | 17 | DESC 18 | 19 | s.homepage = "https://github.com/Voley/DVSwitch" 20 | s.screenshots = "http://i.imgur.com/ZrTCGfd.png", "http://i.imgur.com/rX0O15a.png" 21 | s.license = { :type => "MIT", :file => "LICENSE" } 22 | s.author = { "Dmitry Volevodz" => "dimavolevodz@gmail.com" } 23 | s.platform = :ios, "6.0" 24 | s.source = { :git => "https://github.com/Voley/DVSwitch.git", :tag => "1.0.0" } 25 | 26 | s.source_files = "Source", "Source/**/*.{h,m}" 27 | s.requires_arc = true 28 | 29 | end 30 | -------------------------------------------------------------------------------- /DVSwitcherExample.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | E55DF54919E500F10099D8AC /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = E55DF54819E500F10099D8AC /* main.m */; }; 11 | E55DF54C19E500F10099D8AC /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = E55DF54B19E500F10099D8AC /* AppDelegate.m */; }; 12 | E55DF54F19E500F10099D8AC /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E55DF54E19E500F10099D8AC /* ViewController.m */; }; 13 | E55DF55219E500F10099D8AC /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E55DF55019E500F10099D8AC /* Main.storyboard */; }; 14 | E55DF55419E500F10099D8AC /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E55DF55319E500F10099D8AC /* Images.xcassets */; }; 15 | E55DF55719E500F10099D8AC /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = E55DF55519E500F10099D8AC /* LaunchScreen.xib */; }; 16 | E55DF56319E500F10099D8AC /* DVSwitcherExampleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E55DF56219E500F10099D8AC /* DVSwitcherExampleTests.m */; }; 17 | E55DF57219E5022A0099D8AC /* DVSwitch.m in Sources */ = {isa = PBXBuildFile; fileRef = E55DF57119E5022A0099D8AC /* DVSwitch.m */; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXContainerItemProxy section */ 21 | E55DF55D19E500F10099D8AC /* PBXContainerItemProxy */ = { 22 | isa = PBXContainerItemProxy; 23 | containerPortal = E55DF53B19E500F10099D8AC /* Project object */; 24 | proxyType = 1; 25 | remoteGlobalIDString = E55DF54219E500F10099D8AC; 26 | remoteInfo = DVSwitcherExample; 27 | }; 28 | /* End PBXContainerItemProxy section */ 29 | 30 | /* Begin PBXFileReference section */ 31 | E55DF54319E500F10099D8AC /* DVSwitcherExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DVSwitcherExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; 32 | E55DF54719E500F10099D8AC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 33 | E55DF54819E500F10099D8AC /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 34 | E55DF54A19E500F10099D8AC /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 35 | E55DF54B19E500F10099D8AC /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 36 | E55DF54D19E500F10099D8AC /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 37 | E55DF54E19E500F10099D8AC /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 38 | E55DF55119E500F10099D8AC /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 39 | E55DF55319E500F10099D8AC /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 40 | E55DF55619E500F10099D8AC /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; 41 | E55DF55C19E500F10099D8AC /* DVSwitcherExampleTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = DVSwitcherExampleTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 42 | E55DF56119E500F10099D8AC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 43 | E55DF56219E500F10099D8AC /* DVSwitcherExampleTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DVSwitcherExampleTests.m; sourceTree = ""; }; 44 | E55DF57019E5022A0099D8AC /* DVSwitch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DVSwitch.h; sourceTree = ""; }; 45 | E55DF57119E5022A0099D8AC /* DVSwitch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DVSwitch.m; sourceTree = ""; }; 46 | /* End PBXFileReference section */ 47 | 48 | /* Begin PBXFrameworksBuildPhase section */ 49 | E55DF54019E500F10099D8AC /* Frameworks */ = { 50 | isa = PBXFrameworksBuildPhase; 51 | buildActionMask = 2147483647; 52 | files = ( 53 | ); 54 | runOnlyForDeploymentPostprocessing = 0; 55 | }; 56 | E55DF55919E500F10099D8AC /* Frameworks */ = { 57 | isa = PBXFrameworksBuildPhase; 58 | buildActionMask = 2147483647; 59 | files = ( 60 | ); 61 | runOnlyForDeploymentPostprocessing = 0; 62 | }; 63 | /* End PBXFrameworksBuildPhase section */ 64 | 65 | /* Begin PBXGroup section */ 66 | E55DF53A19E500F10099D8AC = { 67 | isa = PBXGroup; 68 | children = ( 69 | E55DF54519E500F10099D8AC /* DVSwitcherExample */, 70 | E55DF55F19E500F10099D8AC /* DVSwitcherExampleTests */, 71 | E55DF54419E500F10099D8AC /* Products */, 72 | ); 73 | sourceTree = ""; 74 | }; 75 | E55DF54419E500F10099D8AC /* Products */ = { 76 | isa = PBXGroup; 77 | children = ( 78 | E55DF54319E500F10099D8AC /* DVSwitcherExample.app */, 79 | E55DF55C19E500F10099D8AC /* DVSwitcherExampleTests.xctest */, 80 | ); 81 | name = Products; 82 | sourceTree = ""; 83 | }; 84 | E55DF54519E500F10099D8AC /* DVSwitcherExample */ = { 85 | isa = PBXGroup; 86 | children = ( 87 | E55DF56C19E501030099D8AC /* Switcher */, 88 | E55DF54A19E500F10099D8AC /* AppDelegate.h */, 89 | E55DF54B19E500F10099D8AC /* AppDelegate.m */, 90 | E55DF54D19E500F10099D8AC /* ViewController.h */, 91 | E55DF54E19E500F10099D8AC /* ViewController.m */, 92 | E55DF55019E500F10099D8AC /* Main.storyboard */, 93 | E55DF55319E500F10099D8AC /* Images.xcassets */, 94 | E55DF55519E500F10099D8AC /* LaunchScreen.xib */, 95 | E55DF54619E500F10099D8AC /* Supporting Files */, 96 | ); 97 | path = DVSwitcherExample; 98 | sourceTree = ""; 99 | }; 100 | E55DF54619E500F10099D8AC /* Supporting Files */ = { 101 | isa = PBXGroup; 102 | children = ( 103 | E55DF54719E500F10099D8AC /* Info.plist */, 104 | E55DF54819E500F10099D8AC /* main.m */, 105 | ); 106 | name = "Supporting Files"; 107 | sourceTree = ""; 108 | }; 109 | E55DF55F19E500F10099D8AC /* DVSwitcherExampleTests */ = { 110 | isa = PBXGroup; 111 | children = ( 112 | E55DF56219E500F10099D8AC /* DVSwitcherExampleTests.m */, 113 | E55DF56019E500F10099D8AC /* Supporting Files */, 114 | ); 115 | path = DVSwitcherExampleTests; 116 | sourceTree = ""; 117 | }; 118 | E55DF56019E500F10099D8AC /* Supporting Files */ = { 119 | isa = PBXGroup; 120 | children = ( 121 | E55DF56119E500F10099D8AC /* Info.plist */, 122 | ); 123 | name = "Supporting Files"; 124 | sourceTree = ""; 125 | }; 126 | E55DF56C19E501030099D8AC /* Switcher */ = { 127 | isa = PBXGroup; 128 | children = ( 129 | E55DF57019E5022A0099D8AC /* DVSwitch.h */, 130 | E55DF57119E5022A0099D8AC /* DVSwitch.m */, 131 | ); 132 | name = Switcher; 133 | sourceTree = ""; 134 | }; 135 | /* End PBXGroup section */ 136 | 137 | /* Begin PBXNativeTarget section */ 138 | E55DF54219E500F10099D8AC /* DVSwitcherExample */ = { 139 | isa = PBXNativeTarget; 140 | buildConfigurationList = E55DF56619E500F10099D8AC /* Build configuration list for PBXNativeTarget "DVSwitcherExample" */; 141 | buildPhases = ( 142 | E55DF53F19E500F10099D8AC /* Sources */, 143 | E55DF54019E500F10099D8AC /* Frameworks */, 144 | E55DF54119E500F10099D8AC /* Resources */, 145 | ); 146 | buildRules = ( 147 | ); 148 | dependencies = ( 149 | ); 150 | name = DVSwitcherExample; 151 | productName = DVSwitcherExample; 152 | productReference = E55DF54319E500F10099D8AC /* DVSwitcherExample.app */; 153 | productType = "com.apple.product-type.application"; 154 | }; 155 | E55DF55B19E500F10099D8AC /* DVSwitcherExampleTests */ = { 156 | isa = PBXNativeTarget; 157 | buildConfigurationList = E55DF56919E500F10099D8AC /* Build configuration list for PBXNativeTarget "DVSwitcherExampleTests" */; 158 | buildPhases = ( 159 | E55DF55819E500F10099D8AC /* Sources */, 160 | E55DF55919E500F10099D8AC /* Frameworks */, 161 | E55DF55A19E500F10099D8AC /* Resources */, 162 | ); 163 | buildRules = ( 164 | ); 165 | dependencies = ( 166 | E55DF55E19E500F10099D8AC /* PBXTargetDependency */, 167 | ); 168 | name = DVSwitcherExampleTests; 169 | productName = DVSwitcherExampleTests; 170 | productReference = E55DF55C19E500F10099D8AC /* DVSwitcherExampleTests.xctest */; 171 | productType = "com.apple.product-type.bundle.unit-test"; 172 | }; 173 | /* End PBXNativeTarget section */ 174 | 175 | /* Begin PBXProject section */ 176 | E55DF53B19E500F10099D8AC /* Project object */ = { 177 | isa = PBXProject; 178 | attributes = { 179 | LastUpgradeCheck = 0600; 180 | ORGANIZATIONNAME = "Dmitry Volevodz"; 181 | TargetAttributes = { 182 | E55DF54219E500F10099D8AC = { 183 | CreatedOnToolsVersion = 6.0.1; 184 | }; 185 | E55DF55B19E500F10099D8AC = { 186 | CreatedOnToolsVersion = 6.0.1; 187 | TestTargetID = E55DF54219E500F10099D8AC; 188 | }; 189 | }; 190 | }; 191 | buildConfigurationList = E55DF53E19E500F10099D8AC /* Build configuration list for PBXProject "DVSwitcherExample" */; 192 | compatibilityVersion = "Xcode 3.2"; 193 | developmentRegion = English; 194 | hasScannedForEncodings = 0; 195 | knownRegions = ( 196 | en, 197 | Base, 198 | ); 199 | mainGroup = E55DF53A19E500F10099D8AC; 200 | productRefGroup = E55DF54419E500F10099D8AC /* Products */; 201 | projectDirPath = ""; 202 | projectRoot = ""; 203 | targets = ( 204 | E55DF54219E500F10099D8AC /* DVSwitcherExample */, 205 | E55DF55B19E500F10099D8AC /* DVSwitcherExampleTests */, 206 | ); 207 | }; 208 | /* End PBXProject section */ 209 | 210 | /* Begin PBXResourcesBuildPhase section */ 211 | E55DF54119E500F10099D8AC /* Resources */ = { 212 | isa = PBXResourcesBuildPhase; 213 | buildActionMask = 2147483647; 214 | files = ( 215 | E55DF55219E500F10099D8AC /* Main.storyboard in Resources */, 216 | E55DF55719E500F10099D8AC /* LaunchScreen.xib in Resources */, 217 | E55DF55419E500F10099D8AC /* Images.xcassets in Resources */, 218 | ); 219 | runOnlyForDeploymentPostprocessing = 0; 220 | }; 221 | E55DF55A19E500F10099D8AC /* Resources */ = { 222 | isa = PBXResourcesBuildPhase; 223 | buildActionMask = 2147483647; 224 | files = ( 225 | ); 226 | runOnlyForDeploymentPostprocessing = 0; 227 | }; 228 | /* End PBXResourcesBuildPhase section */ 229 | 230 | /* Begin PBXSourcesBuildPhase section */ 231 | E55DF53F19E500F10099D8AC /* Sources */ = { 232 | isa = PBXSourcesBuildPhase; 233 | buildActionMask = 2147483647; 234 | files = ( 235 | E55DF54F19E500F10099D8AC /* ViewController.m in Sources */, 236 | E55DF57219E5022A0099D8AC /* DVSwitch.m in Sources */, 237 | E55DF54C19E500F10099D8AC /* AppDelegate.m in Sources */, 238 | E55DF54919E500F10099D8AC /* main.m in Sources */, 239 | ); 240 | runOnlyForDeploymentPostprocessing = 0; 241 | }; 242 | E55DF55819E500F10099D8AC /* Sources */ = { 243 | isa = PBXSourcesBuildPhase; 244 | buildActionMask = 2147483647; 245 | files = ( 246 | E55DF56319E500F10099D8AC /* DVSwitcherExampleTests.m in Sources */, 247 | ); 248 | runOnlyForDeploymentPostprocessing = 0; 249 | }; 250 | /* End PBXSourcesBuildPhase section */ 251 | 252 | /* Begin PBXTargetDependency section */ 253 | E55DF55E19E500F10099D8AC /* PBXTargetDependency */ = { 254 | isa = PBXTargetDependency; 255 | target = E55DF54219E500F10099D8AC /* DVSwitcherExample */; 256 | targetProxy = E55DF55D19E500F10099D8AC /* PBXContainerItemProxy */; 257 | }; 258 | /* End PBXTargetDependency section */ 259 | 260 | /* Begin PBXVariantGroup section */ 261 | E55DF55019E500F10099D8AC /* Main.storyboard */ = { 262 | isa = PBXVariantGroup; 263 | children = ( 264 | E55DF55119E500F10099D8AC /* Base */, 265 | ); 266 | name = Main.storyboard; 267 | sourceTree = ""; 268 | }; 269 | E55DF55519E500F10099D8AC /* LaunchScreen.xib */ = { 270 | isa = PBXVariantGroup; 271 | children = ( 272 | E55DF55619E500F10099D8AC /* Base */, 273 | ); 274 | name = LaunchScreen.xib; 275 | sourceTree = ""; 276 | }; 277 | /* End PBXVariantGroup section */ 278 | 279 | /* Begin XCBuildConfiguration section */ 280 | E55DF56419E500F10099D8AC /* Debug */ = { 281 | isa = XCBuildConfiguration; 282 | buildSettings = { 283 | ALWAYS_SEARCH_USER_PATHS = NO; 284 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 285 | CLANG_CXX_LIBRARY = "libc++"; 286 | CLANG_ENABLE_MODULES = YES; 287 | CLANG_ENABLE_OBJC_ARC = YES; 288 | CLANG_WARN_BOOL_CONVERSION = YES; 289 | CLANG_WARN_CONSTANT_CONVERSION = YES; 290 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 291 | CLANG_WARN_EMPTY_BODY = YES; 292 | CLANG_WARN_ENUM_CONVERSION = YES; 293 | CLANG_WARN_INT_CONVERSION = YES; 294 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 295 | CLANG_WARN_UNREACHABLE_CODE = YES; 296 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 297 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 298 | COPY_PHASE_STRIP = NO; 299 | ENABLE_STRICT_OBJC_MSGSEND = YES; 300 | GCC_C_LANGUAGE_STANDARD = gnu99; 301 | GCC_DYNAMIC_NO_PIC = NO; 302 | GCC_OPTIMIZATION_LEVEL = 0; 303 | GCC_PREPROCESSOR_DEFINITIONS = ( 304 | "DEBUG=1", 305 | "$(inherited)", 306 | ); 307 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 308 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 309 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 310 | GCC_WARN_UNDECLARED_SELECTOR = YES; 311 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 312 | GCC_WARN_UNUSED_FUNCTION = YES; 313 | GCC_WARN_UNUSED_VARIABLE = YES; 314 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 315 | MTL_ENABLE_DEBUG_INFO = YES; 316 | ONLY_ACTIVE_ARCH = YES; 317 | SDKROOT = iphoneos; 318 | }; 319 | name = Debug; 320 | }; 321 | E55DF56519E500F10099D8AC /* Release */ = { 322 | isa = XCBuildConfiguration; 323 | buildSettings = { 324 | ALWAYS_SEARCH_USER_PATHS = NO; 325 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 326 | CLANG_CXX_LIBRARY = "libc++"; 327 | CLANG_ENABLE_MODULES = YES; 328 | CLANG_ENABLE_OBJC_ARC = YES; 329 | CLANG_WARN_BOOL_CONVERSION = YES; 330 | CLANG_WARN_CONSTANT_CONVERSION = YES; 331 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 332 | CLANG_WARN_EMPTY_BODY = YES; 333 | CLANG_WARN_ENUM_CONVERSION = YES; 334 | CLANG_WARN_INT_CONVERSION = YES; 335 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 336 | CLANG_WARN_UNREACHABLE_CODE = YES; 337 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 338 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 339 | COPY_PHASE_STRIP = YES; 340 | ENABLE_NS_ASSERTIONS = NO; 341 | ENABLE_STRICT_OBJC_MSGSEND = YES; 342 | GCC_C_LANGUAGE_STANDARD = gnu99; 343 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 344 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 345 | GCC_WARN_UNDECLARED_SELECTOR = YES; 346 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 347 | GCC_WARN_UNUSED_FUNCTION = YES; 348 | GCC_WARN_UNUSED_VARIABLE = YES; 349 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 350 | MTL_ENABLE_DEBUG_INFO = NO; 351 | SDKROOT = iphoneos; 352 | VALIDATE_PRODUCT = YES; 353 | }; 354 | name = Release; 355 | }; 356 | E55DF56719E500F10099D8AC /* Debug */ = { 357 | isa = XCBuildConfiguration; 358 | buildSettings = { 359 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 360 | INFOPLIST_FILE = DVSwitcherExample/Info.plist; 361 | IPHONEOS_DEPLOYMENT_TARGET = 6.0; 362 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 363 | PRODUCT_NAME = "$(TARGET_NAME)"; 364 | }; 365 | name = Debug; 366 | }; 367 | E55DF56819E500F10099D8AC /* Release */ = { 368 | isa = XCBuildConfiguration; 369 | buildSettings = { 370 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 371 | INFOPLIST_FILE = DVSwitcherExample/Info.plist; 372 | IPHONEOS_DEPLOYMENT_TARGET = 6.0; 373 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 374 | PRODUCT_NAME = "$(TARGET_NAME)"; 375 | }; 376 | name = Release; 377 | }; 378 | E55DF56A19E500F10099D8AC /* Debug */ = { 379 | isa = XCBuildConfiguration; 380 | buildSettings = { 381 | BUNDLE_LOADER = "$(TEST_HOST)"; 382 | FRAMEWORK_SEARCH_PATHS = ( 383 | "$(SDKROOT)/Developer/Library/Frameworks", 384 | "$(inherited)", 385 | ); 386 | GCC_PREPROCESSOR_DEFINITIONS = ( 387 | "DEBUG=1", 388 | "$(inherited)", 389 | ); 390 | INFOPLIST_FILE = DVSwitcherExampleTests/Info.plist; 391 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 392 | PRODUCT_NAME = "$(TARGET_NAME)"; 393 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/DVSwitcherExample.app/DVSwitcherExample"; 394 | }; 395 | name = Debug; 396 | }; 397 | E55DF56B19E500F10099D8AC /* Release */ = { 398 | isa = XCBuildConfiguration; 399 | buildSettings = { 400 | BUNDLE_LOADER = "$(TEST_HOST)"; 401 | FRAMEWORK_SEARCH_PATHS = ( 402 | "$(SDKROOT)/Developer/Library/Frameworks", 403 | "$(inherited)", 404 | ); 405 | INFOPLIST_FILE = DVSwitcherExampleTests/Info.plist; 406 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 407 | PRODUCT_NAME = "$(TARGET_NAME)"; 408 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/DVSwitcherExample.app/DVSwitcherExample"; 409 | }; 410 | name = Release; 411 | }; 412 | /* End XCBuildConfiguration section */ 413 | 414 | /* Begin XCConfigurationList section */ 415 | E55DF53E19E500F10099D8AC /* Build configuration list for PBXProject "DVSwitcherExample" */ = { 416 | isa = XCConfigurationList; 417 | buildConfigurations = ( 418 | E55DF56419E500F10099D8AC /* Debug */, 419 | E55DF56519E500F10099D8AC /* Release */, 420 | ); 421 | defaultConfigurationIsVisible = 0; 422 | defaultConfigurationName = Release; 423 | }; 424 | E55DF56619E500F10099D8AC /* Build configuration list for PBXNativeTarget "DVSwitcherExample" */ = { 425 | isa = XCConfigurationList; 426 | buildConfigurations = ( 427 | E55DF56719E500F10099D8AC /* Debug */, 428 | E55DF56819E500F10099D8AC /* Release */, 429 | ); 430 | defaultConfigurationIsVisible = 0; 431 | }; 432 | E55DF56919E500F10099D8AC /* Build configuration list for PBXNativeTarget "DVSwitcherExampleTests" */ = { 433 | isa = XCConfigurationList; 434 | buildConfigurations = ( 435 | E55DF56A19E500F10099D8AC /* Debug */, 436 | E55DF56B19E500F10099D8AC /* Release */, 437 | ); 438 | defaultConfigurationIsVisible = 0; 439 | }; 440 | /* End XCConfigurationList section */ 441 | }; 442 | rootObject = E55DF53B19E500F10099D8AC /* Project object */; 443 | } 444 | -------------------------------------------------------------------------------- /DVSwitcherExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DVSwitcherExample.xcodeproj/xcuserdata/dmitryvolevodz.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /DVSwitcherExample.xcodeproj/xcuserdata/dmitryvolevodz.xcuserdatad/xcschemes/DVSwitcherExample.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 29 | 35 | 36 | 37 | 38 | 39 | 44 | 45 | 47 | 53 | 54 | 55 | 56 | 57 | 63 | 64 | 65 | 66 | 75 | 76 | 82 | 83 | 84 | 85 | 86 | 87 | 93 | 94 | 100 | 101 | 102 | 103 | 105 | 106 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /DVSwitcherExample.xcodeproj/xcuserdata/dmitryvolevodz.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | DVSwitcherExample.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | E55DF54219E500F10099D8AC 16 | 17 | primary 18 | 19 | 20 | E55DF55B19E500F10099D8AC 21 | 22 | primary 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /DVSwitcherExample/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // DVSwitcherExample 4 | // 5 | // Created by Dmitry Volevodz on 08.10.14. 6 | // Copyright (c) 2014 Dmitry Volevodz. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /DVSwitcherExample/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // DVSwitcherExample 4 | // 5 | // Created by Dmitry Volevodz on 08.10.14. 6 | // Copyright (c) 2014 Dmitry Volevodz. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | - (void)applicationWillResignActive:(UIApplication *)application { 24 | // 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. 25 | // 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. 26 | } 27 | 28 | - (void)applicationDidEnterBackground:(UIApplication *)application { 29 | // 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. 30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 31 | } 32 | 33 | - (void)applicationWillEnterForeground:(UIApplication *)application { 34 | // 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. 35 | } 36 | 37 | - (void)applicationDidBecomeActive:(UIApplication *)application { 38 | // 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. 39 | } 40 | 41 | - (void)applicationWillTerminate:(UIApplication *)application { 42 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /DVSwitcherExample/Base.lproj/LaunchScreen.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 20 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /DVSwitcherExample/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 | -------------------------------------------------------------------------------- /DVSwitcherExample/DVSwitch.h: -------------------------------------------------------------------------------- 1 | // 2 | // DVSwitch.h 3 | // DVSwitcherExample 4 | // 5 | // Created by Dmitry Volevodz on 08.10.14. 6 | // Copyright (c) 2014 Dmitry Volevodz. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface DVSwitch : UIControl 12 | 13 | @property (strong, nonatomic) UIColor *backgroundColor; // defaults to gray 14 | @property (strong, nonatomic) UIColor *sliderColor; // defaults to white 15 | @property (strong, nonatomic) UIColor *labelTextColorInsideSlider; // defaults to black 16 | @property (strong, nonatomic) UIColor *labelTextColorOutsideSlider; // defaults to white 17 | @property (strong, nonatomic) UIFont *font; // default is nil 18 | @property (nonatomic) CGFloat cornerRadius; // defaults to 12 19 | @property (nonatomic) CGFloat sliderOffset; // slider offset from background, top, bottom, left, right 20 | 21 | + (instancetype)switchWithStringsArray:(NSArray *)strings; 22 | - (instancetype)initWithStringsArray:(NSArray *)strings; 23 | - (instancetype)initWithAttributedStringsArray:(NSArray *)strings; 24 | 25 | - (void)forceSelectedIndex:(NSInteger)index animated:(BOOL)animated; // sets the index, also calls handler block 26 | 27 | // This method sets handler block that is getting called after the switcher is done animating the transition 28 | 29 | - (void)setPressedHandler:(void (^)(NSUInteger index))handler; 30 | 31 | // This method sets handler block that is getting called right before the switcher starts animating the transition 32 | 33 | - (void)setWillBePressedHandler:(void (^)(NSUInteger index))handler; 34 | 35 | - (void)selectIndex:(NSInteger)index animated:(BOOL)animated; // sets the index without calling the handler block 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /DVSwitcherExample/DVSwitch.m: -------------------------------------------------------------------------------- 1 | // 2 | // DVSwitch.m 3 | // DVSwitcherExample 4 | // 5 | // Created by Dmitry Volevodz on 08.10.14. 6 | // Copyright (c) 2014 Dmitry Volevodz. All rights reserved. 7 | // 8 | 9 | #import "DVSwitch.h" 10 | 11 | @interface DVSwitch () 12 | 13 | @property (strong, nonatomic) NSMutableArray *labels; 14 | @property (strong, nonatomic) NSMutableArray *onTopLabels; 15 | @property (strong, nonatomic) NSArray *strings; 16 | 17 | @property (strong, nonatomic) void (^handlerBlock)(NSUInteger index); 18 | @property (strong, nonatomic) void (^willBePressedHandlerBlock)(NSUInteger index); 19 | 20 | @property (strong, nonatomic) UIView *backgroundView; 21 | @property (strong, nonatomic) UIView *sliderView; 22 | 23 | @property (nonatomic) NSInteger selectedIndex; 24 | 25 | @end 26 | 27 | @implementation DVSwitch 28 | 29 | - (instancetype)init 30 | { 31 | self = [super init]; 32 | if (self) { 33 | 34 | [NSException raise:@"DVSwitchInitException" format:@"Init call is prohibited, use initWithStringsArray: method"]; 35 | } 36 | 37 | return self; 38 | } 39 | 40 | + (instancetype)switchWithStringsArray:(NSArray *)strings 41 | { 42 | // to do 43 | return [[DVSwitch alloc] initWithStringsArray:strings]; 44 | } 45 | 46 | - (instancetype)initWithStringsArray:(NSArray *)strings 47 | { 48 | self = [super init]; 49 | 50 | self.strings = strings; 51 | self.cornerRadius = 12.0f; 52 | self.sliderOffset = 1.0f; 53 | 54 | self.backgroundColor = [UIColor colorWithRed:70/255.0 green:70/255.0 blue:70/255.0 alpha:1.0]; 55 | self.sliderColor = [UIColor whiteColor]; 56 | self.labelTextColorInsideSlider = [UIColor blackColor]; 57 | self.labelTextColorOutsideSlider = [UIColor whiteColor]; 58 | 59 | self.backgroundView = [[UIView alloc] init]; 60 | self.backgroundView.backgroundColor = self.backgroundColor; 61 | self.backgroundView.userInteractionEnabled = YES; 62 | [self addSubview:self.backgroundView]; 63 | 64 | self.labels = [[NSMutableArray alloc] init]; 65 | 66 | for (int k = 0; k < [self.strings count]; k++) { 67 | 68 | NSString *string = self.strings[k]; 69 | UILabel *label = [[UILabel alloc] init]; 70 | label.tag = k; 71 | label.text = string; 72 | label.font = self.font; 73 | label.adjustsFontSizeToFitWidth = YES; 74 | label.adjustsLetterSpacingToFitWidth = YES; 75 | label.textAlignment = NSTextAlignmentCenter; 76 | label.textColor = self.labelTextColorOutsideSlider; 77 | [self.backgroundView addSubview:label]; 78 | [self.labels addObject:label]; 79 | 80 | UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleRecognizerTap:)]; 81 | [label addGestureRecognizer:rec]; 82 | label.userInteractionEnabled = YES; 83 | } 84 | 85 | self.sliderView = [[UIView alloc] init]; 86 | self.sliderView.backgroundColor = self.sliderColor; 87 | self.sliderView.clipsToBounds = YES; 88 | [self addSubview:self.sliderView]; 89 | 90 | self.onTopLabels = [[NSMutableArray alloc] init]; 91 | 92 | for (NSString *string in self.strings) { 93 | 94 | UILabel *label = [[UILabel alloc] init]; 95 | label.text = string; 96 | label.font = self.font; 97 | label.adjustsFontSizeToFitWidth = YES; 98 | label.adjustsLetterSpacingToFitWidth = YES; 99 | label.textAlignment = NSTextAlignmentCenter; 100 | label.textColor = self.labelTextColorInsideSlider; 101 | [self.sliderView addSubview:label]; 102 | [self.onTopLabels addObject:label]; 103 | } 104 | 105 | UIPanGestureRecognizer *sliderRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(sliderMoved:)]; 106 | [self.sliderView addGestureRecognizer:sliderRec]; 107 | 108 | return self; 109 | } 110 | 111 | - (instancetype)initWithAttributedStringsArray:(NSArray *)strings { 112 | self = [super init]; 113 | 114 | self.strings = strings; 115 | self.cornerRadius = 12.0f; 116 | self.sliderOffset = 1.0f; 117 | 118 | self.backgroundColor = [UIColor colorWithRed:70/255.0 green:70/255.0 blue:70/255.0 alpha:1.0]; 119 | self.sliderColor = [UIColor whiteColor]; 120 | self.labelTextColorInsideSlider = [UIColor blackColor]; 121 | self.labelTextColorOutsideSlider = [UIColor whiteColor]; 122 | 123 | self.backgroundView = [[UIView alloc] init]; 124 | 125 | self.backgroundView.backgroundColor = self.backgroundColor; 126 | self.backgroundView.userInteractionEnabled = YES; 127 | [self addSubview:self.backgroundView]; 128 | 129 | self.labels = [[NSMutableArray alloc] init]; 130 | 131 | [self.strings enumerateObjectsUsingBlock:^(NSMutableAttributedString *str, NSUInteger idx, BOOL *stop) { 132 | 133 | [str addAttribute:NSForegroundColorAttributeName 134 | value:self.labelTextColorOutsideSlider 135 | range:NSMakeRange(0, str.length)]; 136 | 137 | UILabel *label = [[UILabel alloc] init]; 138 | label.tag = idx; 139 | label.attributedText = str; 140 | label.textAlignment = NSTextAlignmentCenter; 141 | 142 | [self.backgroundView addSubview:label]; 143 | [self.labels addObject:label]; 144 | 145 | UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self 146 | action:@selector(handleRecognizerTap:)]; 147 | [label addGestureRecognizer:rec]; 148 | label.userInteractionEnabled = YES; 149 | }]; 150 | 151 | self.sliderView = [[UIView alloc] init]; 152 | self.sliderView.backgroundColor = self.sliderColor; 153 | self.sliderView.clipsToBounds = YES; 154 | [self addSubview:self.sliderView]; 155 | 156 | self.onTopLabels = [[NSMutableArray alloc] init]; 157 | 158 | [self.strings enumerateObjectsUsingBlock:^(NSMutableAttributedString *str, NSUInteger idx, BOOL *stop) { 159 | 160 | [str addAttribute:NSForegroundColorAttributeName 161 | value:self.labelTextColorInsideSlider 162 | range:NSMakeRange(0, str.length)]; 163 | 164 | UILabel *label = [[UILabel alloc] init]; 165 | label.attributedText = str; 166 | label.textAlignment = NSTextAlignmentCenter; 167 | 168 | [self.sliderView addSubview:label]; 169 | [self.onTopLabels addObject:label]; 170 | }]; 171 | 172 | UIPanGestureRecognizer *sliderRec = [[UIPanGestureRecognizer alloc] initWithTarget:self 173 | action:@selector(sliderMoved:)]; 174 | [self.sliderView addGestureRecognizer:sliderRec]; 175 | 176 | return self; 177 | } 178 | 179 | - (void)setPressedHandler:(void (^)(NSUInteger))handler 180 | { 181 | self.handlerBlock = handler; 182 | } 183 | 184 | - (void)setWillBePressedHandler:(void (^)(NSUInteger))handler 185 | { 186 | self.willBePressedHandlerBlock = handler; 187 | } 188 | 189 | - (void)forceSelectedIndex:(NSInteger)index animated:(BOOL)animated 190 | { 191 | if (index > [self.strings count]) { 192 | return; 193 | } 194 | 195 | self.selectedIndex = index; 196 | 197 | if (animated) { 198 | 199 | [self animateChangeToIndex:index callHandler:YES]; 200 | 201 | } else { 202 | 203 | [self changeToIndexWithoutAnimation:index callHandler:YES]; 204 | } 205 | } 206 | 207 | - (void)selectIndex:(NSInteger)index animated:(BOOL)animated 208 | { 209 | if (index > [self.strings count]) { 210 | return; 211 | } 212 | self.selectedIndex = index; 213 | 214 | if (animated) { 215 | 216 | [self animateChangeToIndex:index callHandler:NO]; 217 | 218 | } else { 219 | 220 | [self changeToIndexWithoutAnimation:index callHandler:NO]; 221 | } 222 | } 223 | 224 | - (void)layoutSubviews 225 | { 226 | self.backgroundView.layer.cornerRadius = self.cornerRadius; 227 | self.sliderView.layer.cornerRadius = self.cornerRadius - 1; 228 | 229 | self.backgroundView.backgroundColor = self.backgroundColor; 230 | self.sliderView.backgroundColor = self.sliderColor; 231 | 232 | self.backgroundView.frame = [self convertRect:self.frame fromView:self.superview]; 233 | 234 | self.backgroundView.layer.cornerRadius = self.cornerRadius; 235 | self.sliderView.layer.cornerRadius = self.cornerRadius; 236 | 237 | CGFloat sliderWidth = self.frame.size.width / [self.strings count]; 238 | 239 | self.sliderView.frame = CGRectMake(sliderWidth * self.selectedIndex + self.sliderOffset, self.backgroundView.frame.origin.y + self.sliderOffset, sliderWidth - self.sliderOffset * 2, self.frame.size.height - self.sliderOffset * 2); 240 | 241 | for (int i = 0; i < [self.labels count]; i++) { 242 | 243 | UILabel *label = self.labels[i]; 244 | label.frame = CGRectMake(i * sliderWidth, 0, sliderWidth, self.frame.size.height); 245 | if (self.font) { 246 | label.font = self.font; 247 | } 248 | label.textColor = self.labelTextColorOutsideSlider; 249 | } 250 | 251 | for (int j = 0; j < [self.onTopLabels count]; j++) { 252 | 253 | UILabel *label = self.onTopLabels[j]; 254 | label.frame = CGRectMake([self.sliderView convertPoint:CGPointMake(j * sliderWidth, 0) fromView:self.backgroundView].x, - self.sliderOffset, sliderWidth, self.frame.size.height); 255 | if (self.font) { 256 | label.font = self.font; 257 | } 258 | label.textColor = self.labelTextColorInsideSlider; 259 | } 260 | } 261 | 262 | - (void)animateChangeToIndex:(NSUInteger)selectedIndex callHandler:(BOOL)callHandler 263 | { 264 | 265 | if (self.willBePressedHandlerBlock) { 266 | self.willBePressedHandlerBlock(selectedIndex); 267 | } 268 | 269 | [UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ 270 | 271 | CGFloat sliderWidth = self.frame.size.width / [self.strings count]; 272 | 273 | CGRect oldFrame = self.sliderView.frame; 274 | CGRect newFrame = CGRectMake(sliderWidth * self.selectedIndex + self.sliderOffset, self.backgroundView.frame.origin.y + self.sliderOffset, sliderWidth - self.sliderOffset * 2, self.frame.size.height - self.sliderOffset * 2); 275 | 276 | CGRect offRect = CGRectMake(newFrame.origin.x - oldFrame.origin.x, newFrame.origin.y - oldFrame.origin.y, 0, 0); 277 | 278 | self.sliderView.frame = newFrame; 279 | 280 | for (UILabel *label in self.onTopLabels) { 281 | 282 | label.frame = CGRectMake(label.frame.origin.x - offRect.origin.x, label.frame.origin.y - offRect.origin.y, label.frame.size.width, label.frame.size.height); 283 | } 284 | 285 | } completion:^(BOOL finished) { 286 | 287 | if (self.handlerBlock && callHandler) { 288 | self.handlerBlock(selectedIndex); 289 | } 290 | }]; 291 | } 292 | 293 | - (void)changeToIndexWithoutAnimation:(NSUInteger)selectedIndex callHandler:(BOOL)callHandler 294 | { 295 | if (self.willBePressedHandlerBlock) { 296 | self.willBePressedHandlerBlock(selectedIndex); 297 | } 298 | 299 | CGFloat sliderWidth = self.frame.size.width / [self.strings count]; 300 | 301 | CGRect oldFrame = self.sliderView.frame; 302 | CGRect newFrame = CGRectMake(sliderWidth * self.selectedIndex + self.sliderOffset, self.backgroundView.frame.origin.y + self.sliderOffset, sliderWidth - self.sliderOffset * 2, self.frame.size.height - self.sliderOffset * 2); 303 | 304 | CGRect offRect = CGRectMake(newFrame.origin.x - oldFrame.origin.x, newFrame.origin.y - oldFrame.origin.y, 0, 0); 305 | 306 | self.sliderView.frame = newFrame; 307 | 308 | for (UILabel *label in self.onTopLabels) { 309 | 310 | label.frame = CGRectMake(label.frame.origin.x - offRect.origin.x, label.frame.origin.y - offRect.origin.y, label.frame.size.width, label.frame.size.height); 311 | } 312 | 313 | if (self.handlerBlock && callHandler) { 314 | self.handlerBlock(selectedIndex); 315 | } 316 | } 317 | 318 | - (void)handleRecognizerTap:(UITapGestureRecognizer *)rec 319 | { 320 | self.selectedIndex = rec.view.tag; 321 | [self animateChangeToIndex:self.selectedIndex callHandler:YES]; 322 | } 323 | 324 | - (void)sliderMoved:(UIPanGestureRecognizer *)rec 325 | { 326 | if (rec.state == UIGestureRecognizerStateChanged) { 327 | 328 | CGRect oldFrame = self.sliderView.frame; 329 | 330 | CGFloat minPos = 0 + self.sliderOffset; 331 | CGFloat maxPos = self.frame.size.width - self.sliderOffset - self.sliderView.frame.size.width; 332 | 333 | CGPoint center = rec.view.center; 334 | CGPoint translation = [rec translationInView:rec.view]; 335 | 336 | center = CGPointMake(center.x + translation.x, center.y); 337 | rec.view.center = center; 338 | [rec setTranslation:CGPointZero inView:rec.view]; 339 | 340 | if (self.sliderView.frame.origin.x < minPos) { 341 | 342 | self.sliderView.frame = CGRectMake(minPos, self.sliderView.frame.origin.y, self.sliderView.frame.size.width, self.sliderView.frame.size.height); 343 | 344 | } else if (self.sliderView.frame.origin.x > maxPos) { 345 | 346 | self.sliderView.frame = CGRectMake(maxPos, self.sliderView.frame.origin.y, self.sliderView.frame.size.width, self.sliderView.frame.size.height); 347 | } 348 | 349 | CGRect newFrame = self.sliderView.frame; 350 | CGRect offRect = CGRectMake(newFrame.origin.x - oldFrame.origin.x, newFrame.origin.y - oldFrame.origin.y, 0, 0); 351 | 352 | for (UILabel *label in self.onTopLabels) { 353 | 354 | label.frame = CGRectMake(label.frame.origin.x - offRect.origin.x, label.frame.origin.y - offRect.origin.y, label.frame.size.width, label.frame.size.height); 355 | } 356 | 357 | } else if (rec.state == UIGestureRecognizerStateEnded || rec.state == UIGestureRecognizerStateCancelled || rec.state == UIGestureRecognizerStateFailed) { 358 | 359 | NSMutableArray *distances = [[NSMutableArray alloc] init]; 360 | 361 | for (int i = 0; i < [self.strings count]; i++) { 362 | 363 | CGFloat possibleX = i * self.sliderView.frame.size.width; 364 | CGFloat distance = possibleX - self.sliderView.frame.origin.x; 365 | [distances addObject:@(fabs(distance))]; 366 | } 367 | 368 | NSNumber *num = [distances valueForKeyPath:@"@min.doubleValue"]; 369 | NSInteger index = [distances indexOfObject:num]; 370 | 371 | if (self.willBePressedHandlerBlock) { 372 | self.willBePressedHandlerBlock(index); 373 | } 374 | 375 | CGFloat sliderWidth = self.frame.size.width / [self.strings count]; 376 | CGFloat desiredX = sliderWidth * index + self.sliderOffset; 377 | 378 | if (self.sliderView.frame.origin.x != desiredX) { 379 | 380 | CGRect evenOlderFrame = self.sliderView.frame; 381 | 382 | CGFloat distance = desiredX - self.sliderView.frame.origin.x; 383 | CGFloat time = fabs(distance / 300); 384 | 385 | [UIView animateWithDuration:time animations:^{ 386 | 387 | self.sliderView.frame = CGRectMake(desiredX, self.sliderView.frame.origin.y, self.sliderView.frame.size.width, self.sliderView.frame.size.height); 388 | 389 | CGRect newFrame = self.sliderView.frame; 390 | 391 | CGRect offRect = CGRectMake(newFrame.origin.x - evenOlderFrame.origin.x, newFrame.origin.y - evenOlderFrame.origin.y, 0, 0); 392 | 393 | for (UILabel *label in self.onTopLabels) { 394 | 395 | label.frame = CGRectMake(label.frame.origin.x - offRect.origin.x, label.frame.origin.y - offRect.origin.y, label.frame.size.width, label.frame.size.height); 396 | } 397 | } completion:^(BOOL finished) { 398 | 399 | self.selectedIndex = index; 400 | 401 | if (self.handlerBlock) { 402 | self.handlerBlock(index); 403 | } 404 | 405 | }]; 406 | 407 | } else { 408 | 409 | self.selectedIndex = index; 410 | 411 | if (self.handlerBlock) { 412 | self.handlerBlock(self.selectedIndex); 413 | } 414 | } 415 | } 416 | } 417 | 418 | 419 | @end 420 | -------------------------------------------------------------------------------- /DVSwitcherExample/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /DVSwitcherExample/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | com.dmitryvolevodz.$(PRODUCT_NAME:rfc1034identifier) 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 | -------------------------------------------------------------------------------- /DVSwitcherExample/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // DVSwitcherExample 4 | // 5 | // Created by Dmitry Volevodz on 08.10.14. 6 | // Copyright (c) 2014 Dmitry Volevodz. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @class DVSwitch; 12 | 13 | @interface ViewController : UIViewController 14 | 15 | @property (strong, nonatomic) DVSwitch *switcher; 16 | 17 | @end 18 | 19 | -------------------------------------------------------------------------------- /DVSwitcherExample/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // DVSwitcherExample 4 | // 5 | // Created by Dmitry Volevodz on 08.10.14. 6 | // Copyright (c) 2014 Dmitry Volevodz. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "DVSwitch.h" 11 | 12 | @interface ViewController () 13 | 14 | @end 15 | 16 | @implementation ViewController 17 | 18 | - (void)viewDidLoad { 19 | 20 | [super viewDidLoad]; 21 | 22 | // first switch 23 | 24 | NSInteger margin = 20; 25 | 26 | self.switcher = [[DVSwitch alloc] initWithStringsArray:@[@"First", @"Second"]]; 27 | self.switcher.frame = CGRectMake(margin, margin * 2, self.view.frame.size.width - margin * 2, 30); 28 | [self.view addSubview:self.switcher]; 29 | [self.switcher setPressedHandler:^(NSUInteger index) { 30 | 31 | NSLog(@"Did press position on first switch at index: %lu", (unsigned long)index); 32 | 33 | }]; 34 | 35 | // second switch 36 | 37 | DVSwitch *secondSwitch = [DVSwitch switchWithStringsArray:@[@"1", @"2", @"3", @"4", @"5"]]; 38 | secondSwitch.frame = CGRectMake(margin, 100, self.view.frame.size.width - margin * 2, 34); 39 | secondSwitch.backgroundColor = [UIColor greenColor]; 40 | secondSwitch.sliderColor = [UIColor redColor]; 41 | secondSwitch.labelTextColorInsideSlider = [UIColor blueColor]; 42 | secondSwitch.labelTextColorOutsideSlider = [UIColor yellowColor]; 43 | secondSwitch.cornerRadius = 0; 44 | [self.view addSubview:secondSwitch]; 45 | 46 | // third Switch 47 | 48 | DVSwitch *third = [DVSwitch switchWithStringsArray:@[@"Hello", @"Dear", @"World"]]; 49 | third.frame = CGRectMake(margin, 160, self.view.frame.size.width - margin * 2, 48); 50 | third.font = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:26]; 51 | third.backgroundColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0]; 52 | third.sliderColor = [UIColor colorWithRed:51/255.0 green:102/255.0 blue:153/255.0 alpha:1.0]; 53 | [self.view addSubview:third]; 54 | 55 | // fourth switch 56 | 57 | DVSwitch *fourth = [DVSwitch switchWithStringsArray:@[@"Apples", @"Oranges"]]; 58 | fourth.frame = CGRectMake(10, 230, self.view.frame.size.width / 2 - margin, 20); 59 | fourth.sliderOffset = 2.0; 60 | fourth.cornerRadius = 10; 61 | fourth.font = [UIFont fontWithName:@"Baskerville-Italic" size:14]; 62 | fourth.backgroundColor = [UIColor colorWithRed:200/255.0 green:65/255.0 blue:39/255.0 alpha:1.0]; 63 | fourth.sliderColor = [UIColor colorWithRed:103/255.0 green:197/255.0 blue:194/255.0 alpha:1.0]; 64 | [self.view addSubview:fourth]; 65 | 66 | // fifth switch 67 | 68 | DVSwitch *fifth = [DVSwitch switchWithStringsArray:@[@"Wow", @"Such good"]]; 69 | fifth.frame = CGRectMake(self.view.frame.size.width / 2 + 10, 230, self.view.frame.size.width / 2 - 40, 20); 70 | fifth.sliderOffset = 1.0; 71 | fifth.cornerRadius = 10; 72 | fifth.font = [UIFont fontWithName:@"Baskerville-Italic" size:18]; 73 | fifth.labelTextColorOutsideSlider = [UIColor colorWithRed:255/255.0 green:30/255.0 blue:30/255.0 alpha:1.0]; 74 | fifth.labelTextColorInsideSlider = [UIColor colorWithRed:255 green:255 blue:102 alpha:1.0]; 75 | fifth.backgroundColor = [UIColor colorWithRed:255/255.0 green:204/255.0 blue:0/255.0 alpha:1.0]; 76 | fifth.sliderColor = [UIColor colorWithRed:255/255.0 green:0/255.0 blue:0/255.0 alpha:1.0]; 77 | [self.view addSubview:fifth]; 78 | } 79 | 80 | 81 | 82 | @end 83 | -------------------------------------------------------------------------------- /DVSwitcherExample/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // DVSwitcherExample 4 | // 5 | // Created by Dmitry Volevodz on 08.10.14. 6 | // Copyright (c) 2014 Dmitry Volevodz. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /DVSwitcherExampleTests/DVSwitcherExampleTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // DVSwitcherExampleTests.m 3 | // DVSwitcherExampleTests 4 | // 5 | // Created by Dmitry Volevodz on 08.10.14. 6 | // Copyright (c) 2014 Dmitry Volevodz. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | @interface DVSwitcherExampleTests : XCTestCase 13 | 14 | @end 15 | 16 | @implementation DVSwitcherExampleTests 17 | 18 | - (void)setUp { 19 | [super setUp]; 20 | // Put setup code here. This method is called before the invocation of each test method in the class. 21 | } 22 | 23 | - (void)tearDown { 24 | // Put teardown code here. This method is called after the invocation of each test method in the class. 25 | [super tearDown]; 26 | } 27 | 28 | - (void)testExample { 29 | // This is an example of a functional test case. 30 | XCTAssert(YES, @"Pass"); 31 | } 32 | 33 | - (void)testPerformanceExample { 34 | // This is an example of a performance test case. 35 | [self measureBlock:^{ 36 | // Put the code you want to measure the time of here. 37 | }]; 38 | } 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /DVSwitcherExampleTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | com.dmitryvolevodz.$(PRODUCT_NAME:rfc1034identifier) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Dmitry Volevodz 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 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | DVSwitch 2 | ======== 3 | 4 | Customizable control based on UISwitch and UISegmentedControl written in Objective-C 5 | 6 | 7 | 8 | 9 | DVSwitch was inspired by UISwitch and UISegmentedControl. The goals of this code are: 10 | 11 | * Easily customizable control with nice animations 12 | * Supporting pan or swipe interactions 13 | * Requires very little setup - images are not needed 14 | * Beautiful font color inversion effect - label color changes per pixel: 15 | * Automatic adjustment based on number of items 16 | 17 | 18 | 19 | *Slider is halfway from one item to another, notice per pixel text color change* 20 | 21 | 22 | Usage 23 | ----- 24 | 25 | DVSwitch *switcher = [[DVSwitch alloc] initWithStringsArray:@[@"First", @"Second"]]; 26 | switcher.frame = CGRectMake(20, 60, self.view.frame.size.width - 40, 34); 27 | [self.view addSubview:switcher]; 28 | [switcher setPressedHandler:^(NSUInteger index) { 29 | 30 | NSLog(@"Did switch to index: %lu", (unsigned long)index); 31 | 32 | }]; 33 | 34 | 35 | 36 | Customizable properties: 37 | 38 | * `UIColor *backgroundColor` - color of the controls background 39 | * `UIColor *sliderColor` - color of slider 40 | * `UIColor *labelTextColorInsideSlider` - color of text when slider hovers over it 41 | * `UIColor *labelTextColorOutsideSlider` - color of text when outside of slider 42 | * `UIFont *font` - font used in control 43 | * `CGFloat cornerRadius` - corner radius of control and corner radius of slider 44 | * `CGFLoat sliderOffset` - pixel offset in points between the slider and the edge of control 45 | 46 | When the user taps or slides the control, handler block is getting called with the index of element which was triggered. To set it use the following method:
47 | `- (void)setPressedHandler:(void (^)(NSUInteger index))handler;` 48 | 49 | -- 50 | 51 | Source code contains example project with few different types of switch. 52 | 53 | Requirements: 54 | ----- 55 | iOS 7.0 and Xcode 6.0 56 | 57 | The control might work on earlier versions, but this was not tested. 58 | 59 | Changelog 60 | ----- 61 | 62 | 1.0.1 - Added method to change index without calling completion block. 63 | 1.0.0 - Initial version. 64 | 65 | Support 66 | ----- 67 | We will welcome any feedback or pull requests to the project. Any changes should NOT change already working behaviour and API. Additions are welcome. 68 | 69 | 70 | Version: 1.0.1
71 | License: [MIT](http://opensource.org/licenses/MIT) 72 | 73 | Contributors: 74 | ----- 75 | Stas Zhukovskiy 76 | René Fouquet 77 | Dmitry Volevodz 78 | 79 | 80 | -- 81 | -------------------------------------------------------------------------------- /Source/DVSwitch.h: -------------------------------------------------------------------------------- 1 | // 2 | // DVSwitch.h 3 | // DVSwitcherExample 4 | // 5 | // Created by Dmitry Volevodz on 08.10.14. 6 | // Copyright (c) 2014 Dmitry Volevodz. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface DVSwitch : UIControl 12 | 13 | @property (strong, nonatomic) UIColor *backgroundColor; // defaults to gray 14 | @property (strong, nonatomic) UIColor *sliderColor; // defaults to white 15 | @property (strong, nonatomic) UIColor *labelTextColorInsideSlider; // defaults to black 16 | @property (strong, nonatomic) UIColor *labelTextColorOutsideSlider; // defaults to white 17 | @property (strong, nonatomic) UIFont *font; // default is nil 18 | @property (nonatomic) CGFloat cornerRadius; // defaults to 12 19 | @property (nonatomic) CGFloat sliderOffset; // slider offset from background, top, bottom, left, right 20 | 21 | + (instancetype)switchWithStringsArray:(NSArray *)strings; 22 | - (instancetype)initWithStringsArray:(NSArray *)strings; 23 | - (instancetype)initWithAttributedStringsArray:(NSArray *)strings; 24 | 25 | - (void)forceSelectedIndex:(NSInteger)index animated:(BOOL)animated; // sets the index, also calls handler block 26 | 27 | // This method sets handler block that is getting called after the switcher is done animating the transition 28 | 29 | - (void)setPressedHandler:(void (^)(NSUInteger index))handler; 30 | 31 | // This method sets handler block that is getting called right before the switcher starts animating the transition 32 | 33 | - (void)setWillBePressedHandler:(void (^)(NSUInteger index))handler; 34 | 35 | - (void)selectIndex:(NSInteger)index animated:(BOOL)animated; // sets the index without calling the handler block 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /Source/DVSwitch.m: -------------------------------------------------------------------------------- 1 | // 2 | // DVSwitch.m 3 | // DVSwitcherExample 4 | // 5 | // Created by Dmitry Volevodz on 08.10.14. 6 | // Copyright (c) 2014 Dmitry Volevodz. All rights reserved. 7 | // 8 | 9 | #import "DVSwitch.h" 10 | 11 | @interface DVSwitch () 12 | 13 | @property (strong, nonatomic) NSMutableArray *labels; 14 | @property (strong, nonatomic) NSMutableArray *onTopLabels; 15 | @property (strong, nonatomic) NSArray *strings; 16 | 17 | @property (strong, nonatomic) void (^handlerBlock)(NSUInteger index); 18 | @property (strong, nonatomic) void (^willBePressedHandlerBlock)(NSUInteger index); 19 | 20 | @property (strong, nonatomic) UIView *backgroundView; 21 | @property (strong, nonatomic) UIView *sliderView; 22 | 23 | @property (nonatomic) NSInteger selectedIndex; 24 | 25 | @end 26 | 27 | @implementation DVSwitch 28 | 29 | - (instancetype)init 30 | { 31 | self = [super init]; 32 | if (self) { 33 | 34 | [NSException raise:@"DVSwitchInitException" format:@"Init call is prohibited, use initWithStringsArray: method"]; 35 | } 36 | 37 | return self; 38 | } 39 | 40 | + (instancetype)switchWithStringsArray:(NSArray *)strings 41 | { 42 | // to do 43 | return [[DVSwitch alloc] initWithStringsArray:strings]; 44 | } 45 | 46 | - (instancetype)initWithStringsArray:(NSArray *)strings 47 | { 48 | self = [super init]; 49 | 50 | self.strings = strings; 51 | self.cornerRadius = 12.0f; 52 | self.sliderOffset = 1.0f; 53 | 54 | self.backgroundColor = [UIColor colorWithRed:70/255.0 green:70/255.0 blue:70/255.0 alpha:1.0]; 55 | self.sliderColor = [UIColor whiteColor]; 56 | self.labelTextColorInsideSlider = [UIColor blackColor]; 57 | self.labelTextColorOutsideSlider = [UIColor whiteColor]; 58 | 59 | self.backgroundView = [[UIView alloc] init]; 60 | self.backgroundView.backgroundColor = self.backgroundColor; 61 | self.backgroundView.userInteractionEnabled = YES; 62 | [self addSubview:self.backgroundView]; 63 | 64 | self.labels = [[NSMutableArray alloc] init]; 65 | 66 | for (int k = 0; k < [self.strings count]; k++) { 67 | 68 | NSString *string = self.strings[k]; 69 | UILabel *label = [[UILabel alloc] init]; 70 | label.tag = k; 71 | label.text = string; 72 | label.font = self.font; 73 | label.adjustsFontSizeToFitWidth = YES; 74 | label.textAlignment = NSTextAlignmentCenter; 75 | label.textColor = self.labelTextColorOutsideSlider; 76 | [self.backgroundView addSubview:label]; 77 | [self.labels addObject:label]; 78 | 79 | UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleRecognizerTap:)]; 80 | [label addGestureRecognizer:rec]; 81 | label.userInteractionEnabled = YES; 82 | } 83 | 84 | self.sliderView = [[UIView alloc] init]; 85 | self.sliderView.backgroundColor = self.sliderColor; 86 | self.sliderView.clipsToBounds = YES; 87 | [self addSubview:self.sliderView]; 88 | 89 | self.onTopLabels = [[NSMutableArray alloc] init]; 90 | 91 | for (NSString *string in self.strings) { 92 | 93 | UILabel *label = [[UILabel alloc] init]; 94 | label.text = string; 95 | label.font = self.font; 96 | label.adjustsFontSizeToFitWidth = YES; 97 | label.textAlignment = NSTextAlignmentCenter; 98 | label.textColor = self.labelTextColorInsideSlider; 99 | [self.sliderView addSubview:label]; 100 | [self.onTopLabels addObject:label]; 101 | } 102 | 103 | UIPanGestureRecognizer *sliderRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(sliderMoved:)]; 104 | [self.sliderView addGestureRecognizer:sliderRec]; 105 | 106 | return self; 107 | } 108 | 109 | - (instancetype)initWithAttributedStringsArray:(NSArray *)strings { 110 | self = [super init]; 111 | 112 | self.strings = strings; 113 | self.cornerRadius = 12.0f; 114 | self.sliderOffset = 1.0f; 115 | 116 | self.backgroundColor = [UIColor colorWithRed:70/255.0 green:70/255.0 blue:70/255.0 alpha:1.0]; 117 | self.sliderColor = [UIColor whiteColor]; 118 | self.labelTextColorInsideSlider = [UIColor blackColor]; 119 | self.labelTextColorOutsideSlider = [UIColor whiteColor]; 120 | 121 | self.backgroundView = [[UIView alloc] init]; 122 | 123 | self.backgroundView.backgroundColor = self.backgroundColor; 124 | self.backgroundView.userInteractionEnabled = YES; 125 | [self addSubview:self.backgroundView]; 126 | 127 | self.labels = [[NSMutableArray alloc] init]; 128 | 129 | [self.strings enumerateObjectsUsingBlock:^(NSMutableAttributedString *str, NSUInteger idx, BOOL *stop) { 130 | 131 | [str addAttribute:NSForegroundColorAttributeName 132 | value:self.labelTextColorOutsideSlider 133 | range:NSMakeRange(0, str.length)]; 134 | 135 | UILabel *label = [[UILabel alloc] init]; 136 | label.tag = idx; 137 | label.attributedText = str; 138 | label.textAlignment = NSTextAlignmentCenter; 139 | 140 | [self.backgroundView addSubview:label]; 141 | [self.labels addObject:label]; 142 | 143 | UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self 144 | action:@selector(handleRecognizerTap:)]; 145 | [label addGestureRecognizer:rec]; 146 | label.userInteractionEnabled = YES; 147 | }]; 148 | 149 | self.sliderView = [[UIView alloc] init]; 150 | self.sliderView.backgroundColor = self.sliderColor; 151 | self.sliderView.clipsToBounds = YES; 152 | [self addSubview:self.sliderView]; 153 | 154 | self.onTopLabels = [[NSMutableArray alloc] init]; 155 | 156 | [self.strings enumerateObjectsUsingBlock:^(NSMutableAttributedString *str, NSUInteger idx, BOOL *stop) { 157 | 158 | [str addAttribute:NSForegroundColorAttributeName 159 | value:self.labelTextColorInsideSlider 160 | range:NSMakeRange(0, str.length)]; 161 | 162 | UILabel *label = [[UILabel alloc] init]; 163 | label.attributedText = str; 164 | label.textAlignment = NSTextAlignmentCenter; 165 | 166 | [self.sliderView addSubview:label]; 167 | [self.onTopLabels addObject:label]; 168 | }]; 169 | 170 | UIPanGestureRecognizer *sliderRec = [[UIPanGestureRecognizer alloc] initWithTarget:self 171 | action:@selector(sliderMoved:)]; 172 | [self.sliderView addGestureRecognizer:sliderRec]; 173 | 174 | return self; 175 | } 176 | 177 | - (void)setPressedHandler:(void (^)(NSUInteger))handler 178 | { 179 | self.handlerBlock = handler; 180 | } 181 | 182 | - (void)setWillBePressedHandler:(void (^)(NSUInteger))handler 183 | { 184 | self.willBePressedHandlerBlock = handler; 185 | } 186 | 187 | - (void)forceSelectedIndex:(NSInteger)index animated:(BOOL)animated 188 | { 189 | if (index > [self.strings count]) { 190 | return; 191 | } 192 | 193 | self.selectedIndex = index; 194 | 195 | if (animated) { 196 | 197 | [self animateChangeToIndex:index callHandler:YES]; 198 | 199 | } else { 200 | 201 | [self changeToIndexWithoutAnimation:index callHandler:YES]; 202 | } 203 | } 204 | 205 | - (void)selectIndex:(NSInteger)index animated:(BOOL)animated 206 | { 207 | if (index > [self.strings count]) { 208 | return; 209 | } 210 | self.selectedIndex = index; 211 | 212 | if (animated) { 213 | 214 | [self animateChangeToIndex:index callHandler:NO]; 215 | 216 | } else { 217 | 218 | [self changeToIndexWithoutAnimation:index callHandler:NO]; 219 | } 220 | } 221 | 222 | - (void)layoutSubviews 223 | { 224 | self.backgroundView.layer.cornerRadius = self.cornerRadius; 225 | self.sliderView.layer.cornerRadius = self.cornerRadius - 1; 226 | 227 | self.backgroundView.backgroundColor = self.backgroundColor; 228 | self.sliderView.backgroundColor = self.sliderColor; 229 | 230 | self.backgroundView.frame = [self convertRect:self.frame fromView:self.superview]; 231 | 232 | self.backgroundView.layer.cornerRadius = self.cornerRadius; 233 | self.sliderView.layer.cornerRadius = self.cornerRadius; 234 | 235 | CGFloat sliderWidth = self.frame.size.width / [self.strings count]; 236 | 237 | self.sliderView.frame = CGRectMake(sliderWidth * self.selectedIndex + self.sliderOffset, self.backgroundView.frame.origin.y + self.sliderOffset, sliderWidth - self.sliderOffset * 2, self.frame.size.height - self.sliderOffset * 2); 238 | 239 | for (int i = 0; i < [self.labels count]; i++) { 240 | 241 | UILabel *label = self.labels[i]; 242 | label.frame = CGRectMake(i * sliderWidth, 0, sliderWidth, self.frame.size.height); 243 | if (self.font) { 244 | label.font = self.font; 245 | } 246 | label.textColor = self.labelTextColorOutsideSlider; 247 | } 248 | 249 | for (int j = 0; j < [self.onTopLabels count]; j++) { 250 | 251 | UILabel *label = self.onTopLabels[j]; 252 | label.frame = CGRectMake([self.sliderView convertPoint:CGPointMake(j * sliderWidth, 0) fromView:self.backgroundView].x, - self.sliderOffset, sliderWidth, self.frame.size.height); 253 | if (self.font) { 254 | label.font = self.font; 255 | } 256 | label.textColor = self.labelTextColorInsideSlider; 257 | } 258 | } 259 | 260 | - (void)animateChangeToIndex:(NSUInteger)selectedIndex callHandler:(BOOL)callHandler 261 | { 262 | 263 | if (self.willBePressedHandlerBlock) { 264 | self.willBePressedHandlerBlock(selectedIndex); 265 | } 266 | 267 | [UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ 268 | 269 | CGFloat sliderWidth = self.frame.size.width / [self.strings count]; 270 | 271 | CGRect oldFrame = self.sliderView.frame; 272 | CGRect newFrame = CGRectMake(sliderWidth * self.selectedIndex + self.sliderOffset, self.backgroundView.frame.origin.y + self.sliderOffset, sliderWidth - self.sliderOffset * 2, self.frame.size.height - self.sliderOffset * 2); 273 | 274 | CGRect offRect = CGRectMake(newFrame.origin.x - oldFrame.origin.x, newFrame.origin.y - oldFrame.origin.y, 0, 0); 275 | 276 | self.sliderView.frame = newFrame; 277 | 278 | for (UILabel *label in self.onTopLabels) { 279 | 280 | label.frame = CGRectMake(label.frame.origin.x - offRect.origin.x, label.frame.origin.y - offRect.origin.y, label.frame.size.width, label.frame.size.height); 281 | } 282 | 283 | } completion:^(BOOL finished) { 284 | 285 | if (self.handlerBlock && callHandler) { 286 | self.handlerBlock(selectedIndex); 287 | } 288 | }]; 289 | } 290 | 291 | - (void)changeToIndexWithoutAnimation:(NSUInteger)selectedIndex callHandler:(BOOL)callHandler 292 | { 293 | if (self.willBePressedHandlerBlock) { 294 | self.willBePressedHandlerBlock(selectedIndex); 295 | } 296 | 297 | CGFloat sliderWidth = self.frame.size.width / [self.strings count]; 298 | 299 | CGRect oldFrame = self.sliderView.frame; 300 | CGRect newFrame = CGRectMake(sliderWidth * self.selectedIndex + self.sliderOffset, self.backgroundView.frame.origin.y + self.sliderOffset, sliderWidth - self.sliderOffset * 2, self.frame.size.height - self.sliderOffset * 2); 301 | 302 | CGRect offRect = CGRectMake(newFrame.origin.x - oldFrame.origin.x, newFrame.origin.y - oldFrame.origin.y, 0, 0); 303 | 304 | self.sliderView.frame = newFrame; 305 | 306 | for (UILabel *label in self.onTopLabels) { 307 | 308 | label.frame = CGRectMake(label.frame.origin.x - offRect.origin.x, label.frame.origin.y - offRect.origin.y, label.frame.size.width, label.frame.size.height); 309 | } 310 | 311 | if (self.handlerBlock && callHandler) { 312 | self.handlerBlock(selectedIndex); 313 | } 314 | } 315 | 316 | - (void)handleRecognizerTap:(UITapGestureRecognizer *)rec 317 | { 318 | self.selectedIndex = rec.view.tag; 319 | [self animateChangeToIndex:self.selectedIndex callHandler:YES]; 320 | } 321 | 322 | - (void)sliderMoved:(UIPanGestureRecognizer *)rec 323 | { 324 | if (rec.state == UIGestureRecognizerStateChanged) { 325 | 326 | CGRect oldFrame = self.sliderView.frame; 327 | 328 | CGFloat minPos = 0 + self.sliderOffset; 329 | CGFloat maxPos = self.frame.size.width - self.sliderOffset - self.sliderView.frame.size.width; 330 | 331 | CGPoint center = rec.view.center; 332 | CGPoint translation = [rec translationInView:rec.view]; 333 | 334 | center = CGPointMake(center.x + translation.x, center.y); 335 | rec.view.center = center; 336 | [rec setTranslation:CGPointZero inView:rec.view]; 337 | 338 | if (self.sliderView.frame.origin.x < minPos) { 339 | 340 | self.sliderView.frame = CGRectMake(minPos, self.sliderView.frame.origin.y, self.sliderView.frame.size.width, self.sliderView.frame.size.height); 341 | 342 | } else if (self.sliderView.frame.origin.x > maxPos) { 343 | 344 | self.sliderView.frame = CGRectMake(maxPos, self.sliderView.frame.origin.y, self.sliderView.frame.size.width, self.sliderView.frame.size.height); 345 | } 346 | 347 | CGRect newFrame = self.sliderView.frame; 348 | CGRect offRect = CGRectMake(newFrame.origin.x - oldFrame.origin.x, newFrame.origin.y - oldFrame.origin.y, 0, 0); 349 | 350 | for (UILabel *label in self.onTopLabels) { 351 | 352 | label.frame = CGRectMake(label.frame.origin.x - offRect.origin.x, label.frame.origin.y - offRect.origin.y, label.frame.size.width, label.frame.size.height); 353 | } 354 | 355 | } else if (rec.state == UIGestureRecognizerStateEnded || rec.state == UIGestureRecognizerStateCancelled || rec.state == UIGestureRecognizerStateFailed) { 356 | 357 | NSMutableArray *distances = [[NSMutableArray alloc] init]; 358 | 359 | for (int i = 0; i < [self.strings count]; i++) { 360 | 361 | CGFloat possibleX = i * self.sliderView.frame.size.width; 362 | CGFloat distance = possibleX - self.sliderView.frame.origin.x; 363 | [distances addObject:@(fabs(distance))]; 364 | } 365 | 366 | NSNumber *num = [distances valueForKeyPath:@"@min.doubleValue"]; 367 | NSInteger index = [distances indexOfObject:num]; 368 | 369 | if (self.willBePressedHandlerBlock) { 370 | self.willBePressedHandlerBlock(index); 371 | } 372 | 373 | CGFloat sliderWidth = self.frame.size.width / [self.strings count]; 374 | CGFloat desiredX = sliderWidth * index + self.sliderOffset; 375 | 376 | if (self.sliderView.frame.origin.x != desiredX) { 377 | 378 | CGRect evenOlderFrame = self.sliderView.frame; 379 | 380 | CGFloat distance = desiredX - self.sliderView.frame.origin.x; 381 | CGFloat time = fabs(distance / 300); 382 | 383 | [UIView animateWithDuration:time animations:^{ 384 | 385 | self.sliderView.frame = CGRectMake(desiredX, self.sliderView.frame.origin.y, self.sliderView.frame.size.width, self.sliderView.frame.size.height); 386 | 387 | CGRect newFrame = self.sliderView.frame; 388 | 389 | CGRect offRect = CGRectMake(newFrame.origin.x - evenOlderFrame.origin.x, newFrame.origin.y - evenOlderFrame.origin.y, 0, 0); 390 | 391 | for (UILabel *label in self.onTopLabels) { 392 | 393 | label.frame = CGRectMake(label.frame.origin.x - offRect.origin.x, label.frame.origin.y - offRect.origin.y, label.frame.size.width, label.frame.size.height); 394 | } 395 | } completion:^(BOOL finished) { 396 | 397 | self.selectedIndex = index; 398 | 399 | if (self.handlerBlock) { 400 | self.handlerBlock(index); 401 | } 402 | 403 | }]; 404 | 405 | } else { 406 | 407 | self.selectedIndex = index; 408 | 409 | if (self.handlerBlock) { 410 | self.handlerBlock(self.selectedIndex); 411 | } 412 | } 413 | } 414 | } 415 | 416 | 417 | @end 418 | --------------------------------------------------------------------------------