├── .gitignore ├── ITSwitch-Demo ├── ITSwitch-Demo.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── ITSwitch-Demo │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json │ ├── Base.lproj │ └── Main.storyboard │ ├── ITSwitch_Demo.entitlements │ ├── Info.plist │ ├── ViewController.h │ ├── ViewController.m │ └── main.m ├── ITSwitch.podspec ├── ITSwitch ├── ITSwitch.h └── ITSwitch.m ├── LICENSE ├── README.md └── demo.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | .DS_Store 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 | profile 14 | *.moved-aside 15 | DerivedData 16 | .idea/ 17 | *.hmap 18 | *.xccheckout 19 | 20 | #CocoaPods 21 | Pods 22 | -------------------------------------------------------------------------------- /ITSwitch-Demo/ITSwitch-Demo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 910CEE7E2070EBB700B4E048 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 910CEE7D2070EBB700B4E048 /* AppDelegate.m */; }; 11 | 910CEE812070EBB700B4E048 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 910CEE802070EBB700B4E048 /* ViewController.m */; }; 12 | 910CEE832070EBB800B4E048 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 910CEE822070EBB800B4E048 /* Assets.xcassets */; }; 13 | 910CEE862070EBB800B4E048 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 910CEE842070EBB800B4E048 /* Main.storyboard */; }; 14 | 910CEE892070EBB800B4E048 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 910CEE882070EBB800B4E048 /* main.m */; }; 15 | 910CEE932070EBD100B4E048 /* ITSwitch.m in Sources */ = {isa = PBXBuildFile; fileRef = 910CEE922070EBD100B4E048 /* ITSwitch.m */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | 910CEE792070EBB700B4E048 /* ITSwitch-Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "ITSwitch-Demo.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 20 | 910CEE7C2070EBB700B4E048 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 21 | 910CEE7D2070EBB700B4E048 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 22 | 910CEE7F2070EBB700B4E048 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 23 | 910CEE802070EBB700B4E048 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 24 | 910CEE822070EBB800B4E048 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 25 | 910CEE852070EBB800B4E048 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 26 | 910CEE872070EBB800B4E048 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 27 | 910CEE882070EBB800B4E048 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 28 | 910CEE8A2070EBB800B4E048 /* ITSwitch_Demo.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = ITSwitch_Demo.entitlements; sourceTree = ""; }; 29 | 910CEE912070EBD100B4E048 /* ITSwitch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ITSwitch.h; path = ../../ITSwitch/ITSwitch.h; sourceTree = ""; }; 30 | 910CEE922070EBD100B4E048 /* ITSwitch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ITSwitch.m; path = ../../ITSwitch/ITSwitch.m; sourceTree = ""; }; 31 | /* End PBXFileReference section */ 32 | 33 | /* Begin PBXFrameworksBuildPhase section */ 34 | 910CEE762070EBB700B4E048 /* Frameworks */ = { 35 | isa = PBXFrameworksBuildPhase; 36 | buildActionMask = 2147483647; 37 | files = ( 38 | ); 39 | runOnlyForDeploymentPostprocessing = 0; 40 | }; 41 | /* End PBXFrameworksBuildPhase section */ 42 | 43 | /* Begin PBXGroup section */ 44 | 910CEE702070EBB600B4E048 = { 45 | isa = PBXGroup; 46 | children = ( 47 | 910CEE902070EBC500B4E048 /* ITSwitch */, 48 | 910CEE7B2070EBB700B4E048 /* ITSwitch-Demo */, 49 | 910CEE7A2070EBB700B4E048 /* Products */, 50 | ); 51 | sourceTree = ""; 52 | }; 53 | 910CEE7A2070EBB700B4E048 /* Products */ = { 54 | isa = PBXGroup; 55 | children = ( 56 | 910CEE792070EBB700B4E048 /* ITSwitch-Demo.app */, 57 | ); 58 | name = Products; 59 | sourceTree = ""; 60 | }; 61 | 910CEE7B2070EBB700B4E048 /* ITSwitch-Demo */ = { 62 | isa = PBXGroup; 63 | children = ( 64 | 910CEE7C2070EBB700B4E048 /* AppDelegate.h */, 65 | 910CEE7D2070EBB700B4E048 /* AppDelegate.m */, 66 | 910CEE7F2070EBB700B4E048 /* ViewController.h */, 67 | 910CEE802070EBB700B4E048 /* ViewController.m */, 68 | 910CEE822070EBB800B4E048 /* Assets.xcassets */, 69 | 910CEE842070EBB800B4E048 /* Main.storyboard */, 70 | 910CEE872070EBB800B4E048 /* Info.plist */, 71 | 910CEE882070EBB800B4E048 /* main.m */, 72 | 910CEE8A2070EBB800B4E048 /* ITSwitch_Demo.entitlements */, 73 | ); 74 | path = "ITSwitch-Demo"; 75 | sourceTree = ""; 76 | }; 77 | 910CEE902070EBC500B4E048 /* ITSwitch */ = { 78 | isa = PBXGroup; 79 | children = ( 80 | 910CEE912070EBD100B4E048 /* ITSwitch.h */, 81 | 910CEE922070EBD100B4E048 /* ITSwitch.m */, 82 | ); 83 | path = ITSwitch; 84 | sourceTree = ""; 85 | }; 86 | /* End PBXGroup section */ 87 | 88 | /* Begin PBXNativeTarget section */ 89 | 910CEE782070EBB700B4E048 /* ITSwitch-Demo */ = { 90 | isa = PBXNativeTarget; 91 | buildConfigurationList = 910CEE8D2070EBB800B4E048 /* Build configuration list for PBXNativeTarget "ITSwitch-Demo" */; 92 | buildPhases = ( 93 | 910CEE752070EBB700B4E048 /* Sources */, 94 | 910CEE762070EBB700B4E048 /* Frameworks */, 95 | 910CEE772070EBB700B4E048 /* Resources */, 96 | ); 97 | buildRules = ( 98 | ); 99 | dependencies = ( 100 | ); 101 | name = "ITSwitch-Demo"; 102 | productName = "ITSwitch-Demo"; 103 | productReference = 910CEE792070EBB700B4E048 /* ITSwitch-Demo.app */; 104 | productType = "com.apple.product-type.application"; 105 | }; 106 | /* End PBXNativeTarget section */ 107 | 108 | /* Begin PBXProject section */ 109 | 910CEE712070EBB600B4E048 /* Project object */ = { 110 | isa = PBXProject; 111 | attributes = { 112 | LastUpgradeCheck = 0930; 113 | ORGANIZATIONNAME = "Ilija Tovilo"; 114 | TargetAttributes = { 115 | 910CEE782070EBB700B4E048 = { 116 | CreatedOnToolsVersion = 9.3; 117 | }; 118 | }; 119 | }; 120 | buildConfigurationList = 910CEE742070EBB600B4E048 /* Build configuration list for PBXProject "ITSwitch-Demo" */; 121 | compatibilityVersion = "Xcode 9.3"; 122 | developmentRegion = en; 123 | hasScannedForEncodings = 0; 124 | knownRegions = ( 125 | en, 126 | Base, 127 | ); 128 | mainGroup = 910CEE702070EBB600B4E048; 129 | productRefGroup = 910CEE7A2070EBB700B4E048 /* Products */; 130 | projectDirPath = ""; 131 | projectRoot = ""; 132 | targets = ( 133 | 910CEE782070EBB700B4E048 /* ITSwitch-Demo */, 134 | ); 135 | }; 136 | /* End PBXProject section */ 137 | 138 | /* Begin PBXResourcesBuildPhase section */ 139 | 910CEE772070EBB700B4E048 /* Resources */ = { 140 | isa = PBXResourcesBuildPhase; 141 | buildActionMask = 2147483647; 142 | files = ( 143 | 910CEE832070EBB800B4E048 /* Assets.xcassets in Resources */, 144 | 910CEE862070EBB800B4E048 /* Main.storyboard in Resources */, 145 | ); 146 | runOnlyForDeploymentPostprocessing = 0; 147 | }; 148 | /* End PBXResourcesBuildPhase section */ 149 | 150 | /* Begin PBXSourcesBuildPhase section */ 151 | 910CEE752070EBB700B4E048 /* Sources */ = { 152 | isa = PBXSourcesBuildPhase; 153 | buildActionMask = 2147483647; 154 | files = ( 155 | 910CEE932070EBD100B4E048 /* ITSwitch.m in Sources */, 156 | 910CEE812070EBB700B4E048 /* ViewController.m in Sources */, 157 | 910CEE892070EBB800B4E048 /* main.m in Sources */, 158 | 910CEE7E2070EBB700B4E048 /* AppDelegate.m in Sources */, 159 | ); 160 | runOnlyForDeploymentPostprocessing = 0; 161 | }; 162 | /* End PBXSourcesBuildPhase section */ 163 | 164 | /* Begin PBXVariantGroup section */ 165 | 910CEE842070EBB800B4E048 /* Main.storyboard */ = { 166 | isa = PBXVariantGroup; 167 | children = ( 168 | 910CEE852070EBB800B4E048 /* Base */, 169 | ); 170 | name = Main.storyboard; 171 | sourceTree = ""; 172 | }; 173 | /* End PBXVariantGroup section */ 174 | 175 | /* Begin XCBuildConfiguration section */ 176 | 910CEE8B2070EBB800B4E048 /* Debug */ = { 177 | isa = XCBuildConfiguration; 178 | buildSettings = { 179 | ALWAYS_SEARCH_USER_PATHS = NO; 180 | CLANG_ANALYZER_NONNULL = YES; 181 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 182 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 183 | CLANG_CXX_LIBRARY = "libc++"; 184 | CLANG_ENABLE_MODULES = YES; 185 | CLANG_ENABLE_OBJC_ARC = YES; 186 | CLANG_ENABLE_OBJC_WEAK = YES; 187 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 188 | CLANG_WARN_BOOL_CONVERSION = YES; 189 | CLANG_WARN_COMMA = YES; 190 | CLANG_WARN_CONSTANT_CONVERSION = YES; 191 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 192 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 193 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 194 | CLANG_WARN_EMPTY_BODY = YES; 195 | CLANG_WARN_ENUM_CONVERSION = YES; 196 | CLANG_WARN_INFINITE_RECURSION = YES; 197 | CLANG_WARN_INT_CONVERSION = YES; 198 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 199 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 200 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 201 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 202 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 203 | CLANG_WARN_STRICT_PROTOTYPES = YES; 204 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 205 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 206 | CLANG_WARN_UNREACHABLE_CODE = YES; 207 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 208 | CODE_SIGN_IDENTITY = "-"; 209 | COPY_PHASE_STRIP = NO; 210 | DEBUG_INFORMATION_FORMAT = dwarf; 211 | ENABLE_STRICT_OBJC_MSGSEND = YES; 212 | ENABLE_TESTABILITY = YES; 213 | GCC_C_LANGUAGE_STANDARD = gnu11; 214 | GCC_DYNAMIC_NO_PIC = NO; 215 | GCC_NO_COMMON_BLOCKS = YES; 216 | GCC_OPTIMIZATION_LEVEL = 0; 217 | GCC_PREPROCESSOR_DEFINITIONS = ( 218 | "DEBUG=1", 219 | "$(inherited)", 220 | ); 221 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 222 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 223 | GCC_WARN_UNDECLARED_SELECTOR = YES; 224 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 225 | GCC_WARN_UNUSED_FUNCTION = YES; 226 | GCC_WARN_UNUSED_VARIABLE = YES; 227 | MACOSX_DEPLOYMENT_TARGET = 10.13; 228 | MTL_ENABLE_DEBUG_INFO = YES; 229 | ONLY_ACTIVE_ARCH = YES; 230 | SDKROOT = macosx; 231 | }; 232 | name = Debug; 233 | }; 234 | 910CEE8C2070EBB800B4E048 /* Release */ = { 235 | isa = XCBuildConfiguration; 236 | buildSettings = { 237 | ALWAYS_SEARCH_USER_PATHS = NO; 238 | CLANG_ANALYZER_NONNULL = YES; 239 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 240 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 241 | CLANG_CXX_LIBRARY = "libc++"; 242 | CLANG_ENABLE_MODULES = YES; 243 | CLANG_ENABLE_OBJC_ARC = YES; 244 | CLANG_ENABLE_OBJC_WEAK = YES; 245 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 246 | CLANG_WARN_BOOL_CONVERSION = YES; 247 | CLANG_WARN_COMMA = YES; 248 | CLANG_WARN_CONSTANT_CONVERSION = YES; 249 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 250 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 251 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 252 | CLANG_WARN_EMPTY_BODY = YES; 253 | CLANG_WARN_ENUM_CONVERSION = YES; 254 | CLANG_WARN_INFINITE_RECURSION = YES; 255 | CLANG_WARN_INT_CONVERSION = YES; 256 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 257 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 258 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 259 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 260 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 261 | CLANG_WARN_STRICT_PROTOTYPES = YES; 262 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 263 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 264 | CLANG_WARN_UNREACHABLE_CODE = YES; 265 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 266 | CODE_SIGN_IDENTITY = "-"; 267 | COPY_PHASE_STRIP = NO; 268 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 269 | ENABLE_NS_ASSERTIONS = NO; 270 | ENABLE_STRICT_OBJC_MSGSEND = YES; 271 | GCC_C_LANGUAGE_STANDARD = gnu11; 272 | GCC_NO_COMMON_BLOCKS = YES; 273 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 274 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 275 | GCC_WARN_UNDECLARED_SELECTOR = YES; 276 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 277 | GCC_WARN_UNUSED_FUNCTION = YES; 278 | GCC_WARN_UNUSED_VARIABLE = YES; 279 | MACOSX_DEPLOYMENT_TARGET = 10.13; 280 | MTL_ENABLE_DEBUG_INFO = NO; 281 | SDKROOT = macosx; 282 | }; 283 | name = Release; 284 | }; 285 | 910CEE8E2070EBB800B4E048 /* Debug */ = { 286 | isa = XCBuildConfiguration; 287 | buildSettings = { 288 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 289 | CODE_SIGN_ENTITLEMENTS = "ITSwitch-Demo/ITSwitch_Demo.entitlements"; 290 | CODE_SIGN_STYLE = Automatic; 291 | COMBINE_HIDPI_IMAGES = YES; 292 | INFOPLIST_FILE = "ITSwitch-Demo/Info.plist"; 293 | LD_RUNPATH_SEARCH_PATHS = ( 294 | "$(inherited)", 295 | "@executable_path/../Frameworks", 296 | ); 297 | PRODUCT_BUNDLE_IDENTIFIER = "ch.ilijatovilo.ITSwitch-Demo"; 298 | PRODUCT_NAME = "$(TARGET_NAME)"; 299 | }; 300 | name = Debug; 301 | }; 302 | 910CEE8F2070EBB800B4E048 /* Release */ = { 303 | isa = XCBuildConfiguration; 304 | buildSettings = { 305 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 306 | CODE_SIGN_ENTITLEMENTS = "ITSwitch-Demo/ITSwitch_Demo.entitlements"; 307 | CODE_SIGN_STYLE = Automatic; 308 | COMBINE_HIDPI_IMAGES = YES; 309 | INFOPLIST_FILE = "ITSwitch-Demo/Info.plist"; 310 | LD_RUNPATH_SEARCH_PATHS = ( 311 | "$(inherited)", 312 | "@executable_path/../Frameworks", 313 | ); 314 | PRODUCT_BUNDLE_IDENTIFIER = "ch.ilijatovilo.ITSwitch-Demo"; 315 | PRODUCT_NAME = "$(TARGET_NAME)"; 316 | }; 317 | name = Release; 318 | }; 319 | /* End XCBuildConfiguration section */ 320 | 321 | /* Begin XCConfigurationList section */ 322 | 910CEE742070EBB600B4E048 /* Build configuration list for PBXProject "ITSwitch-Demo" */ = { 323 | isa = XCConfigurationList; 324 | buildConfigurations = ( 325 | 910CEE8B2070EBB800B4E048 /* Debug */, 326 | 910CEE8C2070EBB800B4E048 /* Release */, 327 | ); 328 | defaultConfigurationIsVisible = 0; 329 | defaultConfigurationName = Release; 330 | }; 331 | 910CEE8D2070EBB800B4E048 /* Build configuration list for PBXNativeTarget "ITSwitch-Demo" */ = { 332 | isa = XCConfigurationList; 333 | buildConfigurations = ( 334 | 910CEE8E2070EBB800B4E048 /* Debug */, 335 | 910CEE8F2070EBB800B4E048 /* Release */, 336 | ); 337 | defaultConfigurationIsVisible = 0; 338 | defaultConfigurationName = Release; 339 | }; 340 | /* End XCConfigurationList section */ 341 | }; 342 | rootObject = 910CEE712070EBB600B4E048 /* Project object */; 343 | } 344 | -------------------------------------------------------------------------------- /ITSwitch-Demo/ITSwitch-Demo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /ITSwitch-Demo/ITSwitch-Demo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /ITSwitch-Demo/ITSwitch-Demo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // ITSwitch-Demo 4 | // 5 | // Created by Ilija Tovilo on 01.04.18. 6 | // Copyright © 2018 Ilija Tovilo. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : NSObject 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /ITSwitch-Demo/ITSwitch-Demo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // ITSwitch-Demo 4 | // 5 | // Created by Ilija Tovilo on 01.04.18. 6 | // Copyright © 2018 Ilija Tovilo. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { 18 | // Insert code here to initialize your application 19 | } 20 | 21 | 22 | - (void)applicationWillTerminate:(NSNotification *)aNotification { 23 | // Insert code here to tear down your application 24 | } 25 | 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /ITSwitch-Demo/ITSwitch-Demo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "mac", 5 | "size" : "16x16", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "mac", 10 | "size" : "16x16", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "mac", 15 | "size" : "32x32", 16 | "scale" : "1x" 17 | }, 18 | { 19 | "idiom" : "mac", 20 | "size" : "32x32", 21 | "scale" : "2x" 22 | }, 23 | { 24 | "idiom" : "mac", 25 | "size" : "128x128", 26 | "scale" : "1x" 27 | }, 28 | { 29 | "idiom" : "mac", 30 | "size" : "128x128", 31 | "scale" : "2x" 32 | }, 33 | { 34 | "idiom" : "mac", 35 | "size" : "256x256", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "mac", 40 | "size" : "256x256", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "mac", 45 | "size" : "512x512", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "mac", 50 | "size" : "512x512", 51 | "scale" : "2x" 52 | } 53 | ], 54 | "info" : { 55 | "version" : 1, 56 | "author" : "xcode" 57 | } 58 | } -------------------------------------------------------------------------------- /ITSwitch-Demo/ITSwitch-Demo/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /ITSwitch-Demo/ITSwitch-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 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | Default 530 | 531 | 532 | 533 | 534 | 535 | 536 | Left to Right 537 | 538 | 539 | 540 | 541 | 542 | 543 | Right to Left 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | Default 555 | 556 | 557 | 558 | 559 | 560 | 561 | Left to Right 562 | 563 | 564 | 565 | 566 | 567 | 568 | Right to Left 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | -------------------------------------------------------------------------------- /ITSwitch-Demo/ITSwitch-Demo/ITSwitch_Demo.entitlements: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | com.apple.security.app-sandbox 6 | 7 | com.apple.security.files.user-selected.read-only 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /ITSwitch-Demo/ITSwitch-Demo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIconFile 10 | 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | $(PRODUCT_NAME) 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleVersion 22 | 1 23 | LSMinimumSystemVersion 24 | $(MACOSX_DEPLOYMENT_TARGET) 25 | NSHumanReadableCopyright 26 | Copyright © 2018 Ilija Tovilo. All rights reserved. 27 | NSMainStoryboardFile 28 | Main 29 | NSPrincipalClass 30 | NSApplication 31 | 32 | 33 | -------------------------------------------------------------------------------- /ITSwitch-Demo/ITSwitch-Demo/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // ITSwitch-Demo 4 | // 5 | // Created by Ilija Tovilo on 01.04.18. 6 | // Copyright © 2018 Ilija Tovilo. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : NSViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /ITSwitch-Demo/ITSwitch-Demo/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // ITSwitch-Demo 4 | // 5 | // Created by Ilija Tovilo on 01.04.18. 6 | // Copyright © 2018 Ilija Tovilo. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | 11 | @implementation ViewController 12 | 13 | - (void)viewDidLoad { 14 | [super viewDidLoad]; 15 | 16 | // Do any additional setup after loading the view. 17 | } 18 | 19 | 20 | - (void)setRepresentedObject:(id)representedObject { 21 | [super setRepresentedObject:representedObject]; 22 | 23 | // Update the view, if already loaded. 24 | } 25 | 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /ITSwitch-Demo/ITSwitch-Demo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // ITSwitch-Demo 4 | // 5 | // Created by Ilija Tovilo on 01.04.18. 6 | // Copyright © 2018 Ilija Tovilo. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | int main(int argc, const char * argv[]) { 12 | return NSApplicationMain(argc, argv); 13 | } 14 | -------------------------------------------------------------------------------- /ITSwitch.podspec: -------------------------------------------------------------------------------- 1 | # 2 | # Be sure to run `pod spec lint ITSwitch.podspec' to ensure this is a 3 | # valid spec and to remove all comments including this before submitting the spec. 4 | # 5 | # To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html 6 | # To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/ 7 | # 8 | 9 | Pod::Spec.new do |s| 10 | s.name = "ITSwitch" 11 | s.version = "1.0" 12 | s.summary = "ITSwitch is a replica of UISwitch for Mac OS X" 13 | s.description = "ITSwitch is a simple and lightweight replica of iOS 7 UISwitch for Mac OS X." 14 | s.homepage = "https://github.com/iluuu1994/ITSwitch" 15 | s.license = { :type => "Apache License", :file => "LICENSE" } 16 | s.author = { "Ilija Tovilo" => "support@ilijatovilo.ch" } 17 | s.osx.deployment_target = "10.9" 18 | s.source = { :git => "https://github.com/iluuu1994/ITSwitch.git", :tag => "v1.0" } 19 | s.source_files = "ITSwitch/*.{h,m}" 20 | s.framework = "QuartzCore" 21 | s.requires_arc = true 22 | end 23 | -------------------------------------------------------------------------------- /ITSwitch/ITSwitch.h: -------------------------------------------------------------------------------- 1 | // 2 | // ITSwitch.h 3 | // ITSwitch-Demo 4 | // 5 | // Created by Ilija Tovilo on 01/02/14. 6 | // Copyright (c) 2014 Ilija Tovilo. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | * ITSwitch is a replica of UISwitch for Mac OS X 13 | */ 14 | IB_DESIGNABLE 15 | @interface ITSwitch : NSControl 16 | 17 | /** 18 | * @property checked - Gets or sets the switches state 19 | */ 20 | @property (nonatomic, assign) IBInspectable BOOL checked; 21 | 22 | /** 23 | * @property tintColor - Gets or sets the switches tint 24 | */ 25 | @property (nonatomic, strong) IBInspectable NSColor *tintColor; 26 | 27 | /** 28 | * @property disabledBorderColor - Define the switch's border color for disabled state. 29 | */ 30 | @property (nonatomic, strong) IBInspectable NSColor *disabledBorderColor; 31 | 32 | @end 33 | -------------------------------------------------------------------------------- /ITSwitch/ITSwitch.m: -------------------------------------------------------------------------------- 1 | // 2 | // ITSwitch.m 3 | // ITSwitch-Demo 4 | // 5 | // Created by Ilija Tovilo on 01/02/14. 6 | // Copyright (c) 2014 Ilija Tovilo. All rights reserved. 7 | // 8 | 9 | #import "ITSwitch.h" 10 | #import 11 | 12 | 13 | // ---------------------------------------------------- 14 | #pragma mark - Static Constants 15 | // ---------------------------------------------------- 16 | 17 | static NSTimeInterval const kAnimationDuration = 0.4f; 18 | 19 | static CGFloat const kBorderLineWidth = 1.f; 20 | 21 | static CGFloat const kGoldenRatio = 1.61803398875f; 22 | static CGFloat const kDecreasedGoldenRatio = 1.38; 23 | 24 | static CGFloat const kEnabledOpacity = 1.f; 25 | static CGFloat const kDisabledOpacity = 0.5f; 26 | 27 | // ---------------------------------------------------- 28 | #pragma mark - Preprocessor 29 | // ---------------------------------------------------- 30 | 31 | 32 | #define kKnobBackgroundColor [NSColor colorWithCalibratedWhite:1.f alpha:1.f] 33 | 34 | #define kDisabledBorderColor [NSColor colorWithCalibratedWhite:0.f alpha:0.2f] 35 | #define kDisabledBackgroundColor [NSColor clearColor] 36 | #define kDefaultTintColor [NSColor colorWithCalibratedRed:0.27f green:0.86f blue:0.36f alpha:1.f] 37 | #define kInactiveBackgroundColor [NSColor colorWithCalibratedWhite:0 alpha:0.3] 38 | 39 | // --------------------------------------------------------------------------------------- 40 | #pragma mark - Interface Extension 41 | // --------------------------------------------------------------------------------------- 42 | 43 | @interface ITSwitch () { 44 | __weak id _target; 45 | SEL _action; 46 | } 47 | 48 | @property (nonatomic, getter = isActive) BOOL active; 49 | @property (nonatomic, getter = hasDragged) BOOL dragged; 50 | @property (nonatomic, getter = isDraggingTowardsOn) BOOL draggingTowardsOn; 51 | 52 | @property (nonatomic, readonly, strong) CALayer *rootLayer; 53 | @property (nonatomic, readonly, strong) CALayer *backgroundLayer; 54 | @property (nonatomic, readonly, strong) CALayer *knobLayer; 55 | @property (nonatomic, readonly, strong) CALayer *knobInsideLayer; 56 | 57 | - (void)propagateValue:(id)value forBinding:(NSString*)binding; 58 | 59 | @end 60 | 61 | 62 | 63 | // --------------------------------------------------------------------------------------- 64 | #pragma mark - ITSwitch 65 | // --------------------------------------------------------------------------------------- 66 | 67 | @implementation ITSwitch 68 | @synthesize tintColor = _tintColor, disabledBorderColor = _disabledBorderColor; 69 | 70 | 71 | 72 | // ---------------------------------------------------- 73 | #pragma mark - Init 74 | // ---------------------------------------------------- 75 | 76 | - (id)initWithCoder:(NSCoder *)coder { 77 | self = [super initWithCoder:coder]; 78 | if (!self) return nil; 79 | 80 | [self setUp]; 81 | 82 | return self; 83 | } 84 | 85 | - (id)initWithFrame:(NSRect)frame { 86 | self = [super initWithFrame:frame]; 87 | if (!self) return nil; 88 | 89 | [self setUp]; 90 | 91 | return self; 92 | } 93 | 94 | - (void)setUp { 95 | // The Switch is enabled per default 96 | self.enabled = YES; 97 | 98 | // Set up the layer hierarchy 99 | [self setUpLayers]; 100 | } 101 | 102 | - (void)setUpLayers { 103 | // Root layer 104 | _rootLayer = [CALayer layer]; 105 | //_rootLayer.delegate = self; 106 | self.layer = _rootLayer; 107 | self.wantsLayer = YES; 108 | 109 | // Allow shadow to flow over bounds of the layer 110 | _rootLayer.masksToBounds = NO; 111 | 112 | // Background layer 113 | _backgroundLayer = [CALayer layer]; 114 | _backgroundLayer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable; 115 | _backgroundLayer.bounds = _rootLayer.bounds; 116 | _backgroundLayer.anchorPoint = (CGPoint){ .x = 0.f, .y = 0.f }; 117 | _backgroundLayer.borderWidth = kBorderLineWidth; 118 | [_rootLayer addSublayer:_backgroundLayer]; 119 | 120 | // Knob layer 121 | _knobLayer = [CALayer layer]; 122 | _knobLayer.frame = [self rectForKnob]; 123 | _knobLayer.autoresizingMask = kCALayerHeightSizable; 124 | _knobLayer.backgroundColor = [kKnobBackgroundColor CGColor]; 125 | _knobLayer.shadowColor = [[NSColor blackColor] CGColor]; 126 | _knobLayer.shadowOffset = (CGSize){ .width = 0.f, .height = -2.f }; 127 | _knobLayer.shadowRadius = 1.f; 128 | _knobLayer.shadowOpacity = 0.3f; 129 | [_rootLayer addSublayer:_knobLayer]; 130 | 131 | _knobInsideLayer = [CALayer layer]; 132 | _knobInsideLayer.frame = _knobLayer.bounds; 133 | _knobInsideLayer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable; 134 | _knobInsideLayer.shadowColor = [[NSColor blackColor] CGColor]; 135 | _knobInsideLayer.shadowOffset = (CGSize){ .width = 0.f, .height = 0.f }; 136 | _knobInsideLayer.backgroundColor = [[NSColor whiteColor] CGColor]; 137 | _knobInsideLayer.shadowRadius = 1.f; 138 | _knobInsideLayer.shadowOpacity = 0.35f; 139 | [_knobLayer addSublayer:_knobInsideLayer]; 140 | 141 | // Initial 142 | [self reloadLayerSize]; 143 | [self reloadLayer]; 144 | } 145 | 146 | 147 | 148 | // ---------------------------------------------------- 149 | #pragma mark - NSView 150 | // ---------------------------------------------------- 151 | 152 | - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent { 153 | return YES; 154 | } 155 | 156 | - (void)setFrame:(NSRect)frameRect { 157 | [super setFrame:frameRect]; 158 | 159 | [self reloadLayerSize]; 160 | } 161 | 162 | - (void)drawFocusRingMask { 163 | CGFloat cornerRadius = NSHeight([self bounds])/2.0; 164 | NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:[self bounds] xRadius:cornerRadius yRadius:cornerRadius]; 165 | [[NSColor blackColor] set]; 166 | [path fill]; 167 | } 168 | 169 | - (BOOL)canBecomeKeyView { 170 | return [NSApp isFullKeyboardAccessEnabled]; 171 | } 172 | 173 | - (NSRect)focusRingMaskBounds { 174 | return [self bounds]; 175 | } 176 | 177 | 178 | // ---------------------------------------------------- 179 | #pragma mark - Update Layer 180 | // ---------------------------------------------------- 181 | 182 | - (void)reloadLayer { 183 | [CATransaction begin]; 184 | [CATransaction setAnimationDuration:kAnimationDuration]; 185 | { 186 | // ------------------------------- Animate Border 187 | // The green part also animates, which looks kinda weird 188 | // We'll use the background-color for now 189 | // _backgroundLayer.borderWidth = (YES || self.isActive || self.isOn) ? NSHeight(_backgroundLayer.bounds) / 2 : kBorderLineWidth; 190 | 191 | // ------------------------------- Animate Colors 192 | if (([self hasDragged] && [self isDraggingTowardsOn]) || (![self hasDragged] && [self checked])) { 193 | _backgroundLayer.borderColor = [self.tintColor CGColor]; 194 | _backgroundLayer.backgroundColor = [self.tintColor CGColor]; 195 | } else { 196 | _backgroundLayer.borderColor = [self.disabledBorderColor CGColor]; 197 | _backgroundLayer.backgroundColor = [kDisabledBackgroundColor CGColor]; 198 | } 199 | 200 | // ------------------------------- Animate Enabled-Disabled state 201 | _rootLayer.opacity = (self.isEnabled) ? kEnabledOpacity : kDisabledOpacity; 202 | 203 | // ------------------------------- Animate Frame 204 | if (![self hasDragged]) { 205 | CAMediaTimingFunction *function = [CAMediaTimingFunction functionWithControlPoints:0.25f :1.5f :0.5f :1.f]; 206 | [CATransaction setAnimationTimingFunction:function]; 207 | } 208 | 209 | self.knobLayer.frame = [self rectForKnob]; 210 | self.knobInsideLayer.frame = self.knobLayer.bounds; 211 | } 212 | [CATransaction commit]; 213 | } 214 | 215 | - (void)reloadLayerSize { 216 | [CATransaction begin]; 217 | [CATransaction setDisableActions:YES]; 218 | { 219 | self.knobLayer.frame = [self rectForKnob]; 220 | self.knobInsideLayer.frame = self.knobLayer.bounds; 221 | 222 | [_backgroundLayer setCornerRadius:_backgroundLayer.bounds.size.height / 2.f]; 223 | [_knobLayer setCornerRadius:_knobLayer.bounds.size.height / 2.f]; 224 | [_knobInsideLayer setCornerRadius:_knobLayer.bounds.size.height / 2.f]; 225 | } 226 | [CATransaction commit]; 227 | } 228 | 229 | - (CGFloat)knobHeightForSize:(NSSize)size 230 | { 231 | return size.height - (kBorderLineWidth * 2.f); 232 | } 233 | 234 | - (CGRect)rectForKnob { 235 | CGFloat height = [self knobHeightForSize:_backgroundLayer.bounds.size]; 236 | CGFloat width = ![self isActive] ? (NSWidth(_backgroundLayer.bounds) - 2.f * kBorderLineWidth) * 1.f / kGoldenRatio : 237 | (NSWidth(_backgroundLayer.bounds) - 2.f * kBorderLineWidth) * 1.f / kDecreasedGoldenRatio; 238 | CGFloat x = ((![self hasDragged] && ![self checked]) || (self.hasDragged && ![self isDraggingTowardsOn])) ? 239 | kBorderLineWidth : 240 | NSWidth(_backgroundLayer.bounds) - width - kBorderLineWidth; 241 | 242 | return (CGRect) { 243 | .size.width = width, 244 | .size.height = height, 245 | .origin.x = x, 246 | .origin.y = kBorderLineWidth, 247 | }; 248 | } 249 | 250 | 251 | 252 | // ---------------------------------------------------- 253 | #pragma mark - NSResponder 254 | // ---------------------------------------------------- 255 | 256 | - (BOOL)acceptsFirstResponder { 257 | return [NSApp isFullKeyboardAccessEnabled]; 258 | } 259 | 260 | - (void)mouseDown:(NSEvent *)theEvent { 261 | if (!self.isEnabled) return; 262 | 263 | self.active = YES; 264 | 265 | [self reloadLayer]; 266 | } 267 | 268 | - (void)mouseDragged:(NSEvent *)theEvent { 269 | if (!self.isEnabled) return; 270 | 271 | self.dragged = YES; 272 | 273 | NSPoint draggingPoint = [self convertPoint:[theEvent locationInWindow] fromView:nil]; 274 | self.draggingTowardsOn = draggingPoint.x >= NSWidth(self.bounds) / 2.f; 275 | 276 | [self reloadLayer]; 277 | } 278 | 279 | - (void)mouseUp:(NSEvent *)theEvent { 280 | if (!self.isEnabled) return; 281 | 282 | self.active = NO; 283 | 284 | BOOL checked = (![self hasDragged]) ? ![self checked] : [self isDraggingTowardsOn]; 285 | BOOL invokeTargetAction = (checked != [self checked]); 286 | 287 | self.checked = checked; 288 | if (invokeTargetAction) [self _invokeTargetAction]; 289 | 290 | // Reset 291 | self.dragged = NO; 292 | self.draggingTowardsOn = NO; 293 | 294 | [self reloadLayer]; 295 | } 296 | 297 | - (void)moveLeft:(id)sender { 298 | if ([self checked]) { 299 | self.checked = NO; 300 | [self _invokeTargetAction]; 301 | } 302 | } 303 | 304 | - (void)moveRight:(id)sender { 305 | if ([self checked] == NO) { 306 | self.checked = YES; 307 | [self _invokeTargetAction]; 308 | } 309 | } 310 | 311 | - (BOOL)performKeyEquivalent:(NSEvent *)theEvent { 312 | BOOL handledKeyEquivalent = NO; 313 | if ([[self window] firstResponder] == self) { 314 | NSInteger ch = [theEvent keyCode]; 315 | 316 | if (ch == 49) //Space 317 | { 318 | self.checked = ![self checked]; 319 | [self _invokeTargetAction]; 320 | handledKeyEquivalent = YES; 321 | } 322 | } 323 | return handledKeyEquivalent; 324 | } 325 | 326 | 327 | // ---------------------------------------------------- 328 | #pragma mark - Accessors 329 | // ---------------------------------------------------- 330 | 331 | - (id)target { 332 | return _target; 333 | } 334 | 335 | - (void)setTarget:(id)target { 336 | _target = target; 337 | } 338 | 339 | - (SEL)action { 340 | return _action; 341 | } 342 | 343 | - (void)setAction:(SEL)action { 344 | _action = action; 345 | } 346 | 347 | - (void)setChecked:(BOOL)checked { 348 | if (_checked != checked) { 349 | _checked = checked; 350 | [self propagateValue:@(checked) forBinding:@"checked"]; 351 | } 352 | 353 | [self reloadLayer]; 354 | } 355 | 356 | - (NSColor *)tintColor { 357 | if (!_tintColor) return kDefaultTintColor; 358 | 359 | return _tintColor; 360 | } 361 | 362 | - (void)setTintColor:(NSColor *)tintColor { 363 | _tintColor = tintColor; 364 | 365 | [self reloadLayer]; 366 | } 367 | 368 | - (NSColor *)disabledBorderColor { 369 | if (!_disabledBorderColor) return kDisabledBorderColor; 370 | 371 | return _disabledBorderColor; 372 | } 373 | 374 | - (void)setDisabledBorderColor:(NSColor *)disabledBorderColor { 375 | _disabledBorderColor = disabledBorderColor; 376 | 377 | [self reloadLayer]; 378 | } 379 | 380 | - (void)setEnabled:(BOOL)enabled { 381 | [super setEnabled:enabled]; 382 | [self reloadLayer]; 383 | } 384 | 385 | // ----------------------------------- 386 | #pragma mark - Helpers 387 | // ----------------------------------- 388 | 389 | - (void)_invokeTargetAction { 390 | if (self.action) 391 | [NSApp sendAction:self.action to:self.target from:self]; 392 | } 393 | 394 | // ----------------------------------- 395 | #pragma mark - Accessibility 396 | // ----------------------------------- 397 | 398 | - (BOOL)accessibilityIsIgnored { 399 | return NO; 400 | } 401 | 402 | - (id)accessibilityHitTest:(NSPoint)point { 403 | return self; 404 | } 405 | 406 | - (NSArray *)accessibilityAttributeNames { 407 | static NSArray *attributes = nil; 408 | if (attributes == nil) 409 | { 410 | NSMutableArray *mutableAttributes = [[super accessibilityAttributeNames] mutableCopy]; 411 | if (mutableAttributes == nil) 412 | mutableAttributes = [NSMutableArray new]; 413 | 414 | // Add attributes 415 | if (![mutableAttributes containsObject:NSAccessibilityValueAttribute]) 416 | [mutableAttributes addObject:NSAccessibilityValueAttribute]; 417 | 418 | if (![mutableAttributes containsObject:NSAccessibilityEnabledAttribute]) 419 | [mutableAttributes addObject:NSAccessibilityEnabledAttribute]; 420 | 421 | if (![mutableAttributes containsObject:NSAccessibilityDescriptionAttribute]) 422 | [mutableAttributes addObject:NSAccessibilityDescriptionAttribute]; 423 | 424 | // Remove attributes 425 | if ([mutableAttributes containsObject:NSAccessibilityChildrenAttribute]) 426 | [mutableAttributes removeObject:NSAccessibilityChildrenAttribute]; 427 | 428 | attributes = [mutableAttributes copy]; 429 | } 430 | return attributes; 431 | } 432 | 433 | - (id)accessibilityAttributeValue:(NSString *)attribute { 434 | id retVal = nil; 435 | if ([attribute isEqualToString:NSAccessibilityRoleAttribute]) 436 | retVal = NSAccessibilityCheckBoxRole; 437 | else if ([attribute isEqualToString:NSAccessibilityValueAttribute]) 438 | retVal = [NSNumber numberWithInt:self.checked]; 439 | else if ([attribute isEqualToString:NSAccessibilityEnabledAttribute]) 440 | retVal = [NSNumber numberWithBool:self.enabled]; 441 | else 442 | retVal = [super accessibilityAttributeValue:attribute]; 443 | return retVal; 444 | } 445 | 446 | - (BOOL)accessibilityIsAttributeSettable:(NSString *)attribute { 447 | BOOL retVal; 448 | if ([attribute isEqualToString:NSAccessibilityValueAttribute]) 449 | retVal = YES; 450 | else if ([attribute isEqualToString:NSAccessibilityEnabledAttribute]) 451 | retVal = NO; 452 | else if ([attribute isEqualToString:NSAccessibilityDescriptionAttribute]) 453 | retVal = NO; 454 | else 455 | retVal = [super accessibilityIsAttributeSettable:attribute]; 456 | return retVal; 457 | } 458 | 459 | - (void)accessibilitySetValue:(id)value forAttribute:(NSString *)attribute { 460 | if ([attribute isEqualToString:NSAccessibilityValueAttribute]) { 461 | BOOL invokeTargetAction = self.checked != [value boolValue]; 462 | self.checked = [value boolValue]; 463 | if (invokeTargetAction) { 464 | [self _invokeTargetAction]; 465 | } 466 | } 467 | else { 468 | [super accessibilitySetValue:value forAttribute:attribute]; 469 | } 470 | } 471 | 472 | - (NSArray *)accessibilityActionNames { 473 | static NSArray *actions = nil; 474 | if (actions == nil) 475 | { 476 | NSMutableArray *mutableActions = [[super accessibilityActionNames] mutableCopy]; 477 | if (mutableActions == nil) 478 | mutableActions = [NSMutableArray new]; 479 | if (![mutableActions containsObject:NSAccessibilityPressAction]) 480 | [mutableActions addObject:NSAccessibilityPressAction]; 481 | actions = [mutableActions copy]; 482 | } 483 | return actions; 484 | } 485 | 486 | - (void)accessibilityPerformAction:(NSString *)actionString { 487 | if ([actionString isEqualToString:NSAccessibilityPressAction]) { 488 | self.checked = ![self checked]; 489 | [self _invokeTargetAction]; 490 | } 491 | else { 492 | [super accessibilityPerformAction:actionString]; 493 | } 494 | } 495 | 496 | #pragma mark - 497 | #pragma mark Bindings Extension 498 | 499 | - (void)propagateValue:(id)value forBinding:(NSString*)binding 500 | { 501 | NSParameterAssert(binding != nil); 502 | 503 | // WARNING: bindingInfo contains NSNull, so it must be accounted for 504 | NSDictionary* bindingInfo = [self infoForBinding:binding]; 505 | if(!bindingInfo) 506 | return; //there is no binding 507 | 508 | // apply the value transformer, if one has been set 509 | NSDictionary* bindingOptions = [bindingInfo objectForKey:NSOptionsKey]; 510 | if(bindingOptions){ 511 | NSValueTransformer* transformer = [bindingOptions valueForKey:NSValueTransformerBindingOption]; 512 | if(!transformer || (id)transformer == [NSNull null]){ 513 | NSString* transformerName = [bindingOptions valueForKey:NSValueTransformerNameBindingOption]; 514 | if(transformerName && (id)transformerName != [NSNull null]){ 515 | transformer = [NSValueTransformer valueTransformerForName:transformerName]; 516 | } 517 | } 518 | 519 | if(transformer && (id)transformer != [NSNull null]){ 520 | if([[transformer class] allowsReverseTransformation]){ 521 | value = [transformer reverseTransformedValue:value]; 522 | } else { 523 | NSLog(@"WARNING: binding \"%@\" has value transformer, but it doesn't allow reverse transformations in %s", binding, __PRETTY_FUNCTION__); 524 | } 525 | } 526 | } 527 | 528 | id boundObject = [bindingInfo objectForKey:NSObservedObjectKey]; 529 | if(!boundObject || boundObject == [NSNull null]){ 530 | NSLog(@"ERROR: NSObservedObjectKey was nil for binding \"%@\" in %s", binding, __PRETTY_FUNCTION__); 531 | return; 532 | } 533 | 534 | NSString* boundKeyPath = [bindingInfo objectForKey:NSObservedKeyPathKey]; 535 | if(!boundKeyPath || (id)boundKeyPath == [NSNull null]){ 536 | NSLog(@"ERROR: NSObservedKeyPathKey was nil for binding \"%@\" in %s", binding, __PRETTY_FUNCTION__); 537 | return; 538 | } 539 | 540 | [boundObject setValue:value forKeyPath:boundKeyPath]; 541 | } 542 | 543 | @end 544 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2014 Ilija Tovilo 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ITSwitch 2 | ======== 3 | 4 | `ITSwitch` is a simple and lightweight replica of iOS 7 `UISwitch` for Mac OS X. 5 | 6 | ![](./demo.png) 7 | 8 | Usage 9 | ----- 10 | 11 | Not much to it, simply set the custom class of a `NSView` to `ITSwitch` in Interface Builder. 12 | You can create an IBAction and simply connect the two in Interface Builder. 13 | Or you can use Cocoa Bindings to directly bind it to one of your properties: 14 | 15 | ```objc 16 | [switch bind:@"checked" toObject:self withKeyPath:@"prop" options:nil]; 17 | ``` 18 | 19 | Use these two properties to explicitely mutate the switches state: 20 | 21 | ```objc 22 | /** 23 | * @property checked - Gets or sets the switches state 24 | */ 25 | @property (nonatomic, assign) IBInspectable BOOL checked; 26 | 27 | /** 28 | * @property tintColor - Gets or sets the switches tint 29 | */ 30 | @property (nonatomic, strong) IBInspectable NSColor *tintColor; 31 | ``` 32 | 33 | Since `ITSwitch` uses its `CALayer` to draw a drop-shadow, you should also layer-back it's superview. 34 | If you don't use Core Animation, you can also simply embed the view in a layer-backed view. 35 | 36 | Requirements 37 | ------------ 38 | 39 | ITSwitch requires 10.9+ and linking against the QuartzCore.framework. 40 | 41 | Tips 42 | ---- 43 | 44 | You may want to consider setting the width of the view to the golden ratio * height. 45 | 46 | So for example: 47 | 48 | ```objc 49 | height = 20; 50 | width = height * 1.618; 51 | ``` 52 | -------------------------------------------------------------------------------- /demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iluuu1994/ITSwitch/b246d81daac107c0daf708eefba3a549518647cc/demo.png --------------------------------------------------------------------------------