├── .gitignore ├── DPDraggableButton.gif ├── DPDraggableButton.podspec ├── DPDraggableButton ├── DPDraggableButton.h └── Info.plist ├── DPDraggableButtonDemo.xcodeproj ├── project.pbxproj └── xcshareddata │ └── xcschemes │ └── DPDraggableButton.xcscheme ├── DPDraggableButtonDemo ├── AppDelegate.swift ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── Info.plist └── ViewController.swift ├── LICENSE ├── Package.swift ├── README.md ├── Sources └── DPDraggableButton-Swift │ └── DPDraggableButton.swift └── Tests ├── DPDraggableButton-SwiftTests ├── DPDraggableButton_SwiftTests.swift └── XCTestManifests.swift └── LinuxMain.swift /.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 | *.xcworkspace 13 | !default.xcworkspace 14 | xcuserdata 15 | profile 16 | *.moved-aside 17 | DerivedData 18 | .idea/ 19 | # Pods - for those of you who use CocoaPods 20 | Pods 21 | -------------------------------------------------------------------------------- /DPDraggableButton.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HongliYu/DPDraggableButton-Swift/c20b158d51c5604b9115bcad98f2570a09f274bf/DPDraggableButton.gif -------------------------------------------------------------------------------- /DPDraggableButton.podspec: -------------------------------------------------------------------------------- 1 | # 2 | # Be sure to run `pod spec lint DPDraggableButton.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 | 11 | s.name = "DPDraggableButton" 12 | s.version = "0.1.0" 13 | s.summary = "Draggable Button" 14 | 15 | s.description = <<-DESC 16 | Drag or tap the button to trigger gesture event 17 | DESC 18 | 19 | s.homepage = "https://github.com/HongliYu/DPDraggableButton-Swift" 20 | s.license = "MIT" 21 | s.author = { "HongliYu" => "yhlssdone@gmail.com" } 22 | s.source = { :git => "https://github.com/HongliYu/DPDraggableButton-Swift.git", :tag => "#{s.version}" } 23 | 24 | s.platform = :ios, "12.0" 25 | s.requires_arc = true 26 | s.source_files = "Sources/DPDraggableButton-Swift/*.swift" 27 | s.frameworks = 'UIKit', 'Foundation' 28 | s.module_name = 'DPDraggableButton' 29 | s.swift_version = "5.0" 30 | 31 | end 32 | 33 | -------------------------------------------------------------------------------- /DPDraggableButton/DPDraggableButton.h: -------------------------------------------------------------------------------- 1 | // 2 | // DPDraggableButton.h 3 | // DPDraggableButton 4 | // 5 | // Created by Hongli Yu on 2018/12/23. 6 | // Copyright © 2018 Hongli Yu. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for DPDraggableButton. 12 | FOUNDATION_EXPORT double DPDraggableButtonVersionNumber; 13 | 14 | //! Project version string for DPDraggableButton. 15 | FOUNDATION_EXPORT const unsigned char DPDraggableButtonVersionString[]; 16 | 17 | // In this header, you should import all the public headers of your framework using statements like #import 18 | 19 | 20 | -------------------------------------------------------------------------------- /DPDraggableButton/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | $(CURRENT_PROJECT_VERSION) 21 | 22 | 23 | -------------------------------------------------------------------------------- /DPDraggableButtonDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | F730EAFD1D59C5980053B5BF /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F730EAFC1D59C5980053B5BF /* AppDelegate.swift */; }; 11 | F730EAFF1D59C5980053B5BF /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F730EAFE1D59C5980053B5BF /* ViewController.swift */; }; 12 | F730EB021D59C5980053B5BF /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F730EB001D59C5980053B5BF /* Main.storyboard */; }; 13 | F730EB041D59C5980053B5BF /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F730EB031D59C5980053B5BF /* Assets.xcassets */; }; 14 | F730EB071D59C5980053B5BF /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F730EB051D59C5980053B5BF /* LaunchScreen.storyboard */; }; 15 | F78D0AFF21CF61860075D189 /* DPDraggableButton.h in Headers */ = {isa = PBXBuildFile; fileRef = F78D0AFD21CF61860075D189 /* DPDraggableButton.h */; settings = {ATTRIBUTES = (Public, ); }; }; 16 | F78D0B0221CF61860075D189 /* DPDraggableButton.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F78D0AFB21CF61860075D189 /* DPDraggableButton.framework */; }; 17 | F78D0B0321CF61860075D189 /* DPDraggableButton.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = F78D0AFB21CF61860075D189 /* DPDraggableButton.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 18 | F791CB6D25174F0F0015752F /* DPDraggableButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = F791CB6C25174F0F0015752F /* DPDraggableButton.swift */; }; 19 | /* End PBXBuildFile section */ 20 | 21 | /* Begin PBXContainerItemProxy section */ 22 | F78D0B0021CF61860075D189 /* PBXContainerItemProxy */ = { 23 | isa = PBXContainerItemProxy; 24 | containerPortal = F730EAF11D59C5980053B5BF /* Project object */; 25 | proxyType = 1; 26 | remoteGlobalIDString = F78D0AFA21CF61860075D189; 27 | remoteInfo = DPDraggableButton; 28 | }; 29 | /* End PBXContainerItemProxy section */ 30 | 31 | /* Begin PBXCopyFilesBuildPhase section */ 32 | F78D0B0721CF61860075D189 /* Embed Frameworks */ = { 33 | isa = PBXCopyFilesBuildPhase; 34 | buildActionMask = 2147483647; 35 | dstPath = ""; 36 | dstSubfolderSpec = 10; 37 | files = ( 38 | F78D0B0321CF61860075D189 /* DPDraggableButton.framework in Embed Frameworks */, 39 | ); 40 | name = "Embed Frameworks"; 41 | runOnlyForDeploymentPostprocessing = 0; 42 | }; 43 | /* End PBXCopyFilesBuildPhase section */ 44 | 45 | /* Begin PBXFileReference section */ 46 | F730EAF91D59C5980053B5BF /* DPDraggableButtonDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DPDraggableButtonDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 47 | F730EAFC1D59C5980053B5BF /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 48 | F730EAFE1D59C5980053B5BF /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 49 | F730EB011D59C5980053B5BF /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 50 | F730EB031D59C5980053B5BF /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 51 | F730EB061D59C5980053B5BF /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 52 | F730EB081D59C5980053B5BF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 53 | F78D0AFB21CF61860075D189 /* DPDraggableButton.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = DPDraggableButton.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 54 | F78D0AFD21CF61860075D189 /* DPDraggableButton.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DPDraggableButton.h; sourceTree = ""; }; 55 | F78D0AFE21CF61860075D189 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 56 | F791CB6C25174F0F0015752F /* DPDraggableButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DPDraggableButton.swift; sourceTree = ""; }; 57 | /* End PBXFileReference section */ 58 | 59 | /* Begin PBXFrameworksBuildPhase section */ 60 | F730EAF61D59C5980053B5BF /* Frameworks */ = { 61 | isa = PBXFrameworksBuildPhase; 62 | buildActionMask = 2147483647; 63 | files = ( 64 | F78D0B0221CF61860075D189 /* DPDraggableButton.framework in Frameworks */, 65 | ); 66 | runOnlyForDeploymentPostprocessing = 0; 67 | }; 68 | F78D0AF821CF61860075D189 /* Frameworks */ = { 69 | isa = PBXFrameworksBuildPhase; 70 | buildActionMask = 2147483647; 71 | files = ( 72 | ); 73 | runOnlyForDeploymentPostprocessing = 0; 74 | }; 75 | /* End PBXFrameworksBuildPhase section */ 76 | 77 | /* Begin PBXGroup section */ 78 | F730EAF01D59C5980053B5BF = { 79 | isa = PBXGroup; 80 | children = ( 81 | F730EAFB1D59C5980053B5BF /* DPDraggableButtonDemo */, 82 | F78D0AFC21CF61860075D189 /* DPDraggableButton */, 83 | F730EAFA1D59C5980053B5BF /* Products */, 84 | ); 85 | sourceTree = ""; 86 | }; 87 | F730EAFA1D59C5980053B5BF /* Products */ = { 88 | isa = PBXGroup; 89 | children = ( 90 | F730EAF91D59C5980053B5BF /* DPDraggableButtonDemo.app */, 91 | F78D0AFB21CF61860075D189 /* DPDraggableButton.framework */, 92 | ); 93 | name = Products; 94 | sourceTree = ""; 95 | }; 96 | F730EAFB1D59C5980053B5BF /* DPDraggableButtonDemo */ = { 97 | isa = PBXGroup; 98 | children = ( 99 | F791CB6B25174F0F0015752F /* DPDraggableButton-Swift */, 100 | F730EAFC1D59C5980053B5BF /* AppDelegate.swift */, 101 | F730EAFE1D59C5980053B5BF /* ViewController.swift */, 102 | F730EB001D59C5980053B5BF /* Main.storyboard */, 103 | F730EB031D59C5980053B5BF /* Assets.xcassets */, 104 | F730EB051D59C5980053B5BF /* LaunchScreen.storyboard */, 105 | F730EB081D59C5980053B5BF /* Info.plist */, 106 | ); 107 | path = DPDraggableButtonDemo; 108 | sourceTree = ""; 109 | }; 110 | F78D0AFC21CF61860075D189 /* DPDraggableButton */ = { 111 | isa = PBXGroup; 112 | children = ( 113 | F78D0AFD21CF61860075D189 /* DPDraggableButton.h */, 114 | F78D0AFE21CF61860075D189 /* Info.plist */, 115 | ); 116 | path = DPDraggableButton; 117 | sourceTree = ""; 118 | }; 119 | F791CB6B25174F0F0015752F /* DPDraggableButton-Swift */ = { 120 | isa = PBXGroup; 121 | children = ( 122 | F791CB6C25174F0F0015752F /* DPDraggableButton.swift */, 123 | ); 124 | name = "DPDraggableButton-Swift"; 125 | path = "Sources/DPDraggableButton-Swift"; 126 | sourceTree = SOURCE_ROOT; 127 | }; 128 | /* End PBXGroup section */ 129 | 130 | /* Begin PBXHeadersBuildPhase section */ 131 | F78D0AF621CF61860075D189 /* Headers */ = { 132 | isa = PBXHeadersBuildPhase; 133 | buildActionMask = 2147483647; 134 | files = ( 135 | F78D0AFF21CF61860075D189 /* DPDraggableButton.h in Headers */, 136 | ); 137 | runOnlyForDeploymentPostprocessing = 0; 138 | }; 139 | /* End PBXHeadersBuildPhase section */ 140 | 141 | /* Begin PBXNativeTarget section */ 142 | F730EAF81D59C5980053B5BF /* DPDraggableButtonDemo */ = { 143 | isa = PBXNativeTarget; 144 | buildConfigurationList = F730EB0B1D59C5980053B5BF /* Build configuration list for PBXNativeTarget "DPDraggableButtonDemo" */; 145 | buildPhases = ( 146 | F730EAF51D59C5980053B5BF /* Sources */, 147 | F730EAF61D59C5980053B5BF /* Frameworks */, 148 | F730EAF71D59C5980053B5BF /* Resources */, 149 | F78D0B0721CF61860075D189 /* Embed Frameworks */, 150 | ); 151 | buildRules = ( 152 | ); 153 | dependencies = ( 154 | F78D0B0121CF61860075D189 /* PBXTargetDependency */, 155 | ); 156 | name = DPDraggableButtonDemo; 157 | productName = DPDraggableButtonDemo; 158 | productReference = F730EAF91D59C5980053B5BF /* DPDraggableButtonDemo.app */; 159 | productType = "com.apple.product-type.application"; 160 | }; 161 | F78D0AFA21CF61860075D189 /* DPDraggableButton */ = { 162 | isa = PBXNativeTarget; 163 | buildConfigurationList = F78D0B0421CF61860075D189 /* Build configuration list for PBXNativeTarget "DPDraggableButton" */; 164 | buildPhases = ( 165 | F78D0AF621CF61860075D189 /* Headers */, 166 | F78D0AF721CF61860075D189 /* Sources */, 167 | F78D0AF821CF61860075D189 /* Frameworks */, 168 | F78D0AF921CF61860075D189 /* Resources */, 169 | ); 170 | buildRules = ( 171 | ); 172 | dependencies = ( 173 | ); 174 | name = DPDraggableButton; 175 | productName = DPDraggableButton; 176 | productReference = F78D0AFB21CF61860075D189 /* DPDraggableButton.framework */; 177 | productType = "com.apple.product-type.framework"; 178 | }; 179 | /* End PBXNativeTarget section */ 180 | 181 | /* Begin PBXProject section */ 182 | F730EAF11D59C5980053B5BF /* Project object */ = { 183 | isa = PBXProject; 184 | attributes = { 185 | LastSwiftUpdateCheck = 0730; 186 | LastUpgradeCheck = 1200; 187 | ORGANIZATIONNAME = "Hongli Yu"; 188 | TargetAttributes = { 189 | F730EAF81D59C5980053B5BF = { 190 | CreatedOnToolsVersion = 7.3.1; 191 | DevelopmentTeam = FC9QT6TC28; 192 | LastSwiftMigration = 1020; 193 | }; 194 | F78D0AFA21CF61860075D189 = { 195 | CreatedOnToolsVersion = 10.1; 196 | DevelopmentTeam = FC9QT6TC28; 197 | LastSwiftMigration = 1020; 198 | ProvisioningStyle = Automatic; 199 | }; 200 | }; 201 | }; 202 | buildConfigurationList = F730EAF41D59C5980053B5BF /* Build configuration list for PBXProject "DPDraggableButtonDemo" */; 203 | compatibilityVersion = "Xcode 3.2"; 204 | developmentRegion = en; 205 | hasScannedForEncodings = 0; 206 | knownRegions = ( 207 | en, 208 | Base, 209 | ); 210 | mainGroup = F730EAF01D59C5980053B5BF; 211 | productRefGroup = F730EAFA1D59C5980053B5BF /* Products */; 212 | projectDirPath = ""; 213 | projectRoot = ""; 214 | targets = ( 215 | F730EAF81D59C5980053B5BF /* DPDraggableButtonDemo */, 216 | F78D0AFA21CF61860075D189 /* DPDraggableButton */, 217 | ); 218 | }; 219 | /* End PBXProject section */ 220 | 221 | /* Begin PBXResourcesBuildPhase section */ 222 | F730EAF71D59C5980053B5BF /* Resources */ = { 223 | isa = PBXResourcesBuildPhase; 224 | buildActionMask = 2147483647; 225 | files = ( 226 | F730EB071D59C5980053B5BF /* LaunchScreen.storyboard in Resources */, 227 | F730EB041D59C5980053B5BF /* Assets.xcassets in Resources */, 228 | F730EB021D59C5980053B5BF /* Main.storyboard in Resources */, 229 | ); 230 | runOnlyForDeploymentPostprocessing = 0; 231 | }; 232 | F78D0AF921CF61860075D189 /* Resources */ = { 233 | isa = PBXResourcesBuildPhase; 234 | buildActionMask = 2147483647; 235 | files = ( 236 | ); 237 | runOnlyForDeploymentPostprocessing = 0; 238 | }; 239 | /* End PBXResourcesBuildPhase section */ 240 | 241 | /* Begin PBXSourcesBuildPhase section */ 242 | F730EAF51D59C5980053B5BF /* Sources */ = { 243 | isa = PBXSourcesBuildPhase; 244 | buildActionMask = 2147483647; 245 | files = ( 246 | F730EAFF1D59C5980053B5BF /* ViewController.swift in Sources */, 247 | F730EAFD1D59C5980053B5BF /* AppDelegate.swift in Sources */, 248 | F791CB6D25174F0F0015752F /* DPDraggableButton.swift in Sources */, 249 | ); 250 | runOnlyForDeploymentPostprocessing = 0; 251 | }; 252 | F78D0AF721CF61860075D189 /* Sources */ = { 253 | isa = PBXSourcesBuildPhase; 254 | buildActionMask = 2147483647; 255 | files = ( 256 | ); 257 | runOnlyForDeploymentPostprocessing = 0; 258 | }; 259 | /* End PBXSourcesBuildPhase section */ 260 | 261 | /* Begin PBXTargetDependency section */ 262 | F78D0B0121CF61860075D189 /* PBXTargetDependency */ = { 263 | isa = PBXTargetDependency; 264 | target = F78D0AFA21CF61860075D189 /* DPDraggableButton */; 265 | targetProxy = F78D0B0021CF61860075D189 /* PBXContainerItemProxy */; 266 | }; 267 | /* End PBXTargetDependency section */ 268 | 269 | /* Begin PBXVariantGroup section */ 270 | F730EB001D59C5980053B5BF /* Main.storyboard */ = { 271 | isa = PBXVariantGroup; 272 | children = ( 273 | F730EB011D59C5980053B5BF /* Base */, 274 | ); 275 | name = Main.storyboard; 276 | sourceTree = ""; 277 | }; 278 | F730EB051D59C5980053B5BF /* LaunchScreen.storyboard */ = { 279 | isa = PBXVariantGroup; 280 | children = ( 281 | F730EB061D59C5980053B5BF /* Base */, 282 | ); 283 | name = LaunchScreen.storyboard; 284 | sourceTree = ""; 285 | }; 286 | /* End PBXVariantGroup section */ 287 | 288 | /* Begin XCBuildConfiguration section */ 289 | F730EB091D59C5980053B5BF /* Debug */ = { 290 | isa = XCBuildConfiguration; 291 | buildSettings = { 292 | ALWAYS_SEARCH_USER_PATHS = NO; 293 | CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; 294 | CLANG_ANALYZER_NONNULL = YES; 295 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 296 | CLANG_CXX_LIBRARY = "libc++"; 297 | CLANG_ENABLE_MODULES = YES; 298 | CLANG_ENABLE_OBJC_ARC = YES; 299 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 300 | CLANG_WARN_BOOL_CONVERSION = YES; 301 | CLANG_WARN_COMMA = YES; 302 | CLANG_WARN_CONSTANT_CONVERSION = YES; 303 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 304 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 305 | CLANG_WARN_EMPTY_BODY = YES; 306 | CLANG_WARN_ENUM_CONVERSION = YES; 307 | CLANG_WARN_INFINITE_RECURSION = YES; 308 | CLANG_WARN_INT_CONVERSION = YES; 309 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 310 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 311 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 312 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 313 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 314 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 315 | CLANG_WARN_STRICT_PROTOTYPES = YES; 316 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 317 | CLANG_WARN_UNREACHABLE_CODE = YES; 318 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 319 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 320 | COPY_PHASE_STRIP = NO; 321 | DEBUG_INFORMATION_FORMAT = dwarf; 322 | ENABLE_STRICT_OBJC_MSGSEND = YES; 323 | ENABLE_TESTABILITY = YES; 324 | GCC_C_LANGUAGE_STANDARD = gnu99; 325 | GCC_DYNAMIC_NO_PIC = NO; 326 | GCC_NO_COMMON_BLOCKS = YES; 327 | GCC_OPTIMIZATION_LEVEL = 0; 328 | GCC_PREPROCESSOR_DEFINITIONS = ( 329 | "DEBUG=1", 330 | "$(inherited)", 331 | ); 332 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 333 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 334 | GCC_WARN_UNDECLARED_SELECTOR = YES; 335 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 336 | GCC_WARN_UNUSED_FUNCTION = YES; 337 | GCC_WARN_UNUSED_VARIABLE = YES; 338 | IPHONEOS_DEPLOYMENT_TARGET = 12.0; 339 | MTL_ENABLE_DEBUG_INFO = YES; 340 | ONLY_ACTIVE_ARCH = YES; 341 | SDKROOT = iphoneos; 342 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 343 | }; 344 | name = Debug; 345 | }; 346 | F730EB0A1D59C5980053B5BF /* Release */ = { 347 | isa = XCBuildConfiguration; 348 | buildSettings = { 349 | ALWAYS_SEARCH_USER_PATHS = NO; 350 | CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; 351 | CLANG_ANALYZER_NONNULL = YES; 352 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 353 | CLANG_CXX_LIBRARY = "libc++"; 354 | CLANG_ENABLE_MODULES = YES; 355 | CLANG_ENABLE_OBJC_ARC = YES; 356 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 357 | CLANG_WARN_BOOL_CONVERSION = YES; 358 | CLANG_WARN_COMMA = YES; 359 | CLANG_WARN_CONSTANT_CONVERSION = YES; 360 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 361 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 362 | CLANG_WARN_EMPTY_BODY = YES; 363 | CLANG_WARN_ENUM_CONVERSION = YES; 364 | CLANG_WARN_INFINITE_RECURSION = YES; 365 | CLANG_WARN_INT_CONVERSION = YES; 366 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 367 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 368 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 369 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 370 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 371 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 372 | CLANG_WARN_STRICT_PROTOTYPES = YES; 373 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 374 | CLANG_WARN_UNREACHABLE_CODE = YES; 375 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 376 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 377 | COPY_PHASE_STRIP = NO; 378 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 379 | ENABLE_NS_ASSERTIONS = NO; 380 | ENABLE_STRICT_OBJC_MSGSEND = YES; 381 | GCC_C_LANGUAGE_STANDARD = gnu99; 382 | GCC_NO_COMMON_BLOCKS = YES; 383 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 384 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 385 | GCC_WARN_UNDECLARED_SELECTOR = YES; 386 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 387 | GCC_WARN_UNUSED_FUNCTION = YES; 388 | GCC_WARN_UNUSED_VARIABLE = YES; 389 | IPHONEOS_DEPLOYMENT_TARGET = 12.0; 390 | MTL_ENABLE_DEBUG_INFO = NO; 391 | SDKROOT = iphoneos; 392 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 393 | VALIDATE_PRODUCT = YES; 394 | }; 395 | name = Release; 396 | }; 397 | F730EB0C1D59C5980053B5BF /* Debug */ = { 398 | isa = XCBuildConfiguration; 399 | buildSettings = { 400 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 401 | CODE_SIGN_IDENTITY = "iPhone Developer"; 402 | DEVELOPMENT_TEAM = FC9QT6TC28; 403 | INFOPLIST_FILE = DPDraggableButtonDemo/Info.plist; 404 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 405 | PRODUCT_BUNDLE_IDENTIFIER = cn.hlyu.DPDraggableButtonDemo; 406 | PRODUCT_NAME = "$(TARGET_NAME)"; 407 | SWIFT_VERSION = 5.0; 408 | }; 409 | name = Debug; 410 | }; 411 | F730EB0D1D59C5980053B5BF /* Release */ = { 412 | isa = XCBuildConfiguration; 413 | buildSettings = { 414 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 415 | CODE_SIGN_IDENTITY = "iPhone Developer"; 416 | DEVELOPMENT_TEAM = FC9QT6TC28; 417 | INFOPLIST_FILE = DPDraggableButtonDemo/Info.plist; 418 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 419 | PRODUCT_BUNDLE_IDENTIFIER = cn.hlyu.DPDraggableButtonDemo; 420 | PRODUCT_NAME = "$(TARGET_NAME)"; 421 | SWIFT_VERSION = 5.0; 422 | }; 423 | name = Release; 424 | }; 425 | F78D0B0521CF61860075D189 /* Debug */ = { 426 | isa = XCBuildConfiguration; 427 | buildSettings = { 428 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 429 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 430 | CLANG_ENABLE_OBJC_WEAK = YES; 431 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 432 | CLANG_WARN_COMMA = YES; 433 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 434 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 435 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 436 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 437 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 438 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 439 | CLANG_WARN_STRICT_PROTOTYPES = YES; 440 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 441 | CODE_SIGN_IDENTITY = ""; 442 | CODE_SIGN_STYLE = Automatic; 443 | CURRENT_PROJECT_VERSION = 1; 444 | DEFINES_MODULE = YES; 445 | DEVELOPMENT_TEAM = FC9QT6TC28; 446 | DYLIB_COMPATIBILITY_VERSION = 1; 447 | DYLIB_CURRENT_VERSION = 1; 448 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 449 | GCC_C_LANGUAGE_STANDARD = gnu11; 450 | INFOPLIST_FILE = DPDraggableButton/Info.plist; 451 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 452 | IPHONEOS_DEPLOYMENT_TARGET = 12.1; 453 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 454 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 455 | MTL_FAST_MATH = YES; 456 | OTHER_CFLAGS = ""; 457 | PRODUCT_BUNDLE_IDENTIFIER = hongliyu.github.io.DPDraggableButton; 458 | PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; 459 | SKIP_INSTALL = YES; 460 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 461 | SWIFT_VERSION = 5.0; 462 | TARGETED_DEVICE_FAMILY = "1,2"; 463 | VERSIONING_SYSTEM = "apple-generic"; 464 | VERSION_INFO_PREFIX = ""; 465 | }; 466 | name = Debug; 467 | }; 468 | F78D0B0621CF61860075D189 /* Release */ = { 469 | isa = XCBuildConfiguration; 470 | buildSettings = { 471 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 472 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 473 | CLANG_ENABLE_OBJC_WEAK = YES; 474 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 475 | CLANG_WARN_COMMA = YES; 476 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 477 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 478 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 479 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 480 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 481 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 482 | CLANG_WARN_STRICT_PROTOTYPES = YES; 483 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 484 | CODE_SIGN_IDENTITY = ""; 485 | CODE_SIGN_STYLE = Automatic; 486 | CURRENT_PROJECT_VERSION = 1; 487 | DEFINES_MODULE = YES; 488 | DEVELOPMENT_TEAM = FC9QT6TC28; 489 | DYLIB_COMPATIBILITY_VERSION = 1; 490 | DYLIB_CURRENT_VERSION = 1; 491 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 492 | GCC_C_LANGUAGE_STANDARD = gnu11; 493 | INFOPLIST_FILE = DPDraggableButton/Info.plist; 494 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 495 | IPHONEOS_DEPLOYMENT_TARGET = 12.1; 496 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 497 | MTL_FAST_MATH = YES; 498 | OTHER_CFLAGS = ""; 499 | PRODUCT_BUNDLE_IDENTIFIER = hongliyu.github.io.DPDraggableButton; 500 | PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; 501 | SKIP_INSTALL = YES; 502 | SWIFT_VERSION = 5.0; 503 | TARGETED_DEVICE_FAMILY = "1,2"; 504 | VERSIONING_SYSTEM = "apple-generic"; 505 | VERSION_INFO_PREFIX = ""; 506 | }; 507 | name = Release; 508 | }; 509 | /* End XCBuildConfiguration section */ 510 | 511 | /* Begin XCConfigurationList section */ 512 | F730EAF41D59C5980053B5BF /* Build configuration list for PBXProject "DPDraggableButtonDemo" */ = { 513 | isa = XCConfigurationList; 514 | buildConfigurations = ( 515 | F730EB091D59C5980053B5BF /* Debug */, 516 | F730EB0A1D59C5980053B5BF /* Release */, 517 | ); 518 | defaultConfigurationIsVisible = 0; 519 | defaultConfigurationName = Release; 520 | }; 521 | F730EB0B1D59C5980053B5BF /* Build configuration list for PBXNativeTarget "DPDraggableButtonDemo" */ = { 522 | isa = XCConfigurationList; 523 | buildConfigurations = ( 524 | F730EB0C1D59C5980053B5BF /* Debug */, 525 | F730EB0D1D59C5980053B5BF /* Release */, 526 | ); 527 | defaultConfigurationIsVisible = 0; 528 | defaultConfigurationName = Release; 529 | }; 530 | F78D0B0421CF61860075D189 /* Build configuration list for PBXNativeTarget "DPDraggableButton" */ = { 531 | isa = XCConfigurationList; 532 | buildConfigurations = ( 533 | F78D0B0521CF61860075D189 /* Debug */, 534 | F78D0B0621CF61860075D189 /* Release */, 535 | ); 536 | defaultConfigurationIsVisible = 0; 537 | defaultConfigurationName = Release; 538 | }; 539 | /* End XCConfigurationList section */ 540 | }; 541 | rootObject = F730EAF11D59C5980053B5BF /* Project object */; 542 | } 543 | -------------------------------------------------------------------------------- /DPDraggableButtonDemo.xcodeproj/xcshareddata/xcschemes/DPDraggableButton.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 43 | 44 | 50 | 51 | 52 | 53 | 59 | 60 | 66 | 67 | 68 | 69 | 71 | 72 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /DPDraggableButtonDemo/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // DPDraggableButtonDemo 4 | // 5 | // Created by Hongli Yu on 8/9/16. 6 | // Copyright © 2016 Hongli Yu. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 17 | // Override point for customization after application launch. 18 | return true 19 | } 20 | 21 | func applicationWillResignActive(_ application: UIApplication) { 22 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 23 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 24 | } 25 | 26 | func applicationDidEnterBackground(_ application: UIApplication) { 27 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 28 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 29 | } 30 | 31 | func applicationWillEnterForeground(_ application: UIApplication) { 32 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 33 | } 34 | 35 | func applicationDidBecomeActive(_ application: UIApplication) { 36 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 37 | } 38 | 39 | func applicationWillTerminate(_ application: UIApplication) { 40 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 41 | } 42 | 43 | 44 | } 45 | 46 | -------------------------------------------------------------------------------- /DPDraggableButtonDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ios-marketing", 45 | "size" : "1024x1024", 46 | "scale" : "1x" 47 | } 48 | ], 49 | "info" : { 50 | "version" : 1, 51 | "author" : "xcode" 52 | } 53 | } -------------------------------------------------------------------------------- /DPDraggableButtonDemo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /DPDraggableButtonDemo/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 | 31 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 61 | 75 | 88 | 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 | -------------------------------------------------------------------------------- /DPDraggableButtonDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /DPDraggableButtonDemo/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // DPDraggableButtonDemo 4 | // 5 | // Created by Hongli Yu on 8/9/16. 6 | // Copyright © 2016 Hongli Yu. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController { 12 | 13 | @IBOutlet var consoleLabel: UILabel! 14 | @IBOutlet var logSwitch: UISwitch! 15 | @IBOutlet var draggableButton: DPDraggableButton! 16 | private var logInfo: String = "Log:" 17 | 18 | override func viewDidAppear(_ animated: Bool) { 19 | super.viewDidAppear(animated) 20 | UIApplication.shared.keyWindow?.bringSubviewToFront(draggableButton) 21 | } 22 | 23 | override func viewDidLoad() { 24 | super.viewDidLoad() 25 | 26 | draggableButton.tapBlock = { 27 | [weak self] in 28 | guard let strongSelf = self else { return } 29 | strongSelf.refreshLog("[single tap]") 30 | } 31 | 32 | draggableButton.doubleTapBlock = { 33 | [weak self] in 34 | guard let strongSelf = self else { return } 35 | strongSelf.refreshLog("[double tap]") 36 | } 37 | 38 | draggableButton.longPressBlock = { 39 | [weak self] in 40 | guard let strongSelf = self else { return } 41 | strongSelf.refreshLog("[longpress]") 42 | } 43 | 44 | draggableButton.draggingBlock = { 45 | [weak self] in 46 | guard let strongSelf = self else { return } 47 | strongSelf.refreshLog("[dragging]") 48 | } 49 | 50 | draggableButton.dragDoneBlock = { 51 | [weak self] in 52 | guard let strongSelf = self else { return } 53 | strongSelf.refreshLog("[drag done]") 54 | } 55 | 56 | draggableButton.autoDockingBlock = { 57 | [weak self] in 58 | guard let strongSelf = self else { return } 59 | strongSelf.refreshLog("[auto docking]") 60 | } 61 | 62 | draggableButton.autoDockingDoneBlock = { 63 | [weak self] in 64 | guard let strongSelf = self else { return } 65 | strongSelf.refreshLog("[auto docking done]") 66 | } 67 | } 68 | 69 | @IBAction func cleanLogs(_ sender: AnyObject) { 70 | logInfo = "Log: " 71 | consoleLabel.text = logInfo 72 | } 73 | 74 | @IBAction func removeDraggableButtons(_ sender: Any) { 75 | DPDraggableButton.removeAllFromKeyWindow() 76 | } 77 | 78 | @IBAction func addDraggableButton(_ sender: Any) { 79 | let button = DPDraggableButton(frame: CGRect(x: 0, y: 300, width: 100, height: 30), 80 | draggableButtonType: .round) 81 | button.setTitle("New Button", for: .normal) 82 | button.titleLabel?.textColor = .white 83 | button.titleLabel?.font = UIFont.systemFont(ofSize: 16.0) 84 | button.backgroundColor = .systemGreen 85 | } 86 | 87 | func refreshLog(_ info: String) { 88 | guard logSwitch.isOn else { return } 89 | logInfo += info 90 | consoleLabel.text = logInfo 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 HongliYu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.3 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | 4 | import PackageDescription 5 | 6 | let package = Package( 7 | name: "DPDraggableButton-Swift", 8 | products: [ 9 | // Products define the executables and libraries a package produces, and make them visible to other packages. 10 | .library( 11 | name: "DPDraggableButton-Swift", 12 | targets: ["DPDraggableButton-Swift"]), 13 | ], 14 | dependencies: [ 15 | // Dependencies declare other packages that this package depends on. 16 | // .package(url: /* package url */, from: "1.0.0"), 17 | ], 18 | targets: [ 19 | // Targets are the basic building blocks of a package. A target can define a module or a test suite. 20 | // Targets can depend on other targets in this package, and on products in packages this package depends on. 21 | .target( 22 | name: "DPDraggableButton-Swift", 23 | dependencies: []), 24 | .testTarget( 25 | name: "DPDraggableButton-SwiftTests", 26 | dependencies: ["DPDraggableButton-Swift"]), 27 | ] 28 | ) 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DPDraggableButton-Swift 2 | Drag or tap the button to trigger Gesture event 3 | 4 | [![Cocoapods](https://img.shields.io/cocoapods/v/DPDraggableButton.svg)](http://cocoapods.org/?q=DPDraggableButton) 5 | [![Pod License](http://img.shields.io/cocoapods/l/DPDraggableButton.svg)](https://github.com/HongliYu/DPDraggableButton-Swift/blob/master/LICENSE) 6 | [![Swift-5.3](https://img.shields.io/badge/Swift-5.3-blue.svg)]() 7 | [![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) 8 | 9 | alt text 10 | 11 | # Usage 12 | 13 | ``` swift 14 | draggableButton.tapBlock = { 15 | [weak self] in 16 | guard let strongSelf = self else { return } 17 | strongSelf.refreshLog("[single tap]") 18 | } 19 | 20 | draggableButton.doubleTapBlock = { 21 | [weak self] in 22 | guard let strongSelf = self else { return } 23 | strongSelf.refreshLog("[double tap]") 24 | } 25 | 26 | draggableButton.longPressBlock = { 27 | [weak self] in 28 | guard let strongSelf = self else { return } 29 | strongSelf.refreshLog("[longpress]") 30 | } 31 | 32 | draggableButton.draggingBlock = { 33 | [weak self] in 34 | guard let strongSelf = self else { return } 35 | strongSelf.refreshLog("[dragging]") 36 | } 37 | 38 | draggableButton.dragDoneBlock = { 39 | [weak self] in 40 | guard let strongSelf = self else { return } 41 | strongSelf.refreshLog("[drag done]") 42 | } 43 | 44 | draggableButton.autoDockingBlock = { 45 | [weak self] in 46 | guard let strongSelf = self else { return } 47 | strongSelf.refreshLog("[auto docking]") 48 | } 49 | 50 | draggableButton.autoDockingDoneBlock = { 51 | [weak self] in 52 | guard let strongSelf = self else { return } 53 | strongSelf.refreshLog("[auto docking done]") 54 | } 55 | 56 | -------------------------------------------------------------------------------- /Sources/DPDraggableButton-Swift/DPDraggableButton.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DPDraggableButton.swift 3 | // DPDraggableButtonDemo 4 | // 5 | // Created by Hongli Yu on 8/11/16. 6 | // Copyright © 2016 Hongli Yu. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | public enum DPDraggableButtonType { 12 | case rect 13 | case round 14 | } 15 | 16 | let kDPAutoDockingDuration: Double = 0.2 17 | let kDPDoubleTapTimeInterval: Double = 0.36 18 | 19 | open class DPDraggableButton: UIButton { 20 | 21 | open var draggable = true 22 | open var dragging = false 23 | open var autoDocking = true 24 | open var singleTapBeenCanceled = false 25 | open var draggableButtonType = DPDraggableButtonType.rect 26 | 27 | open var beginLocation: CGPoint? 28 | open var longPressGestureRecognizer = UILongPressGestureRecognizer() 29 | 30 | open var tapBlock:(()->Void)? { 31 | set(tapBlock) { 32 | if let aTapBlock = tapBlock { 33 | self.tapBlockStored = aTapBlock 34 | self.addTarget(self, action: #selector(tapAction(_:)), for: .touchUpInside) 35 | } 36 | } 37 | get { 38 | return self.tapBlockStored! 39 | } 40 | } 41 | private var tapBlockStored:(()->Void)? 42 | 43 | open var doubleTapBlock:(()->Void)? 44 | open var longPressBlock:(()->Void)? 45 | open var draggingBlock:(()->Void)? 46 | open var dragDoneBlock:(()->Void)? 47 | open var autoDockingBlock:(()->Void)? 48 | open var autoDockingDoneBlock:(()->Void)? 49 | 50 | required public init?(coder aDecoder: NSCoder) { 51 | super.init(coder: aDecoder) 52 | } 53 | 54 | override open func awakeFromNib() { 55 | translatesAutoresizingMaskIntoConstraints = true 56 | super.awakeFromNib() 57 | config(.rect) 58 | } 59 | 60 | public init(frame: CGRect, 61 | draggableButtonType: DPDraggableButtonType) { 62 | super.init(frame: frame) 63 | self.config(draggableButtonType) 64 | } 65 | 66 | open func addButtonToKeyWindow() { 67 | if self.superview != nil { 68 | self.removeFromSuperview() 69 | } 70 | if let keyWindow = UIApplication.shared.keyWindow { 71 | keyWindow.addSubview(self) 72 | } else { 73 | UIApplication.shared.windows.first?.addSubview(self) 74 | } 75 | } 76 | 77 | open class func removeAllFromKeyWindow() { 78 | guard let subviews = UIApplication.shared.keyWindow?.subviews else { return } 79 | for case let button as DPDraggableButton in subviews { 80 | button.removeFromSuperview() 81 | } 82 | } 83 | 84 | open class func removeFromKeyWindow(button: DPDraggableButton) { 85 | guard let subviews = UIApplication.shared.keyWindow?.subviews else { return } 86 | subviews.filter { (view) -> Bool in 87 | view == button 88 | }.first?.removeFromSuperview() 89 | } 90 | 91 | /// During configuration, button will be added to key window, 92 | /// - Parameter type: DPDraggableButtonType 93 | private func config(_ type: DPDraggableButtonType) { 94 | addButtonToKeyWindow() 95 | draggableButtonType = type 96 | switch (type) { 97 | case .rect: 98 | break 99 | case .round: 100 | layer.cornerRadius = frame.size.height / 2.0 101 | layer.borderColor = UIColor.lightGray.cgColor 102 | layer.borderWidth = 0.5 103 | layer.masksToBounds = true 104 | } 105 | longPressGestureRecognizer.addTarget(self, action:#selector(longPressHandler(_:)) ) 106 | longPressGestureRecognizer.allowableMovement = 0 107 | addGestureRecognizer(longPressGestureRecognizer) 108 | } 109 | 110 | // MARK: Gestures Handler 111 | @objc func longPressHandler(_ gesture: UILongPressGestureRecognizer) { 112 | switch gesture.state { 113 | case .began: 114 | longPressBlock?() 115 | default: 116 | break 117 | } 118 | } 119 | 120 | // MARK: Actions 121 | @objc func tapAction(_ sender: AnyObject) { 122 | let delayInSeconds: Double = (doubleTapBlock != nil ? kDPDoubleTapTimeInterval : 0) 123 | let popTime: DispatchTime = DispatchTime.now() 124 | + Double((Int64)(delayInSeconds * (Double)(NSEC_PER_SEC))) / Double(NSEC_PER_SEC) 125 | DispatchQueue.main.asyncAfter(deadline: popTime) { [weak self] in 126 | guard let strongSelf = self else { return } 127 | if let tapBlock = strongSelf.tapBlock, 128 | !strongSelf.singleTapBeenCanceled, 129 | !strongSelf.dragging { 130 | tapBlock() 131 | } 132 | } 133 | } 134 | 135 | // MARK: Touch 136 | open override func touchesBegan(_ touches: Set, with event: UIEvent?) { 137 | dragging = false 138 | super.touchesBegan(touches, with: event) 139 | guard let touch = touches.randomElement() else { return } 140 | if touch.tapCount == 2 { 141 | doubleTapBlock?() 142 | singleTapBeenCanceled = true 143 | } else { 144 | singleTapBeenCanceled = false 145 | } 146 | beginLocation = touch.location(in: self) 147 | } 148 | 149 | open override func touchesMoved(_ touches: Set, with event: UIEvent?) { 150 | guard let touch = touches.randomElement(), 151 | let beginLocation = beginLocation, 152 | let superviewFrame = self.superview?.frame, 153 | draggable else { return } 154 | 155 | dragging = true 156 | let currentLocation = touch.location(in: self) 157 | 158 | let offsetX = currentLocation.x - beginLocation.x 159 | let offsetY = currentLocation.y - beginLocation.y 160 | center = CGPoint(x: center.x + offsetX, y: center.y + offsetY) 161 | 162 | let frame: CGRect = self.frame 163 | let leftLimitX: CGFloat = frame.size.width / 2.0 164 | let rightLimitX: CGFloat = superviewFrame.size.width - leftLimitX 165 | let topLimitY: CGFloat = frame.size.height / 2.0 166 | let bottomLimitY: CGFloat = superviewFrame.size.height - topLimitY 167 | 168 | if center.x > rightLimitX { 169 | center = CGPoint(x: rightLimitX, y: center.y) 170 | } else if (center.x <= leftLimitX) { 171 | center = CGPoint(x: leftLimitX, y: center.y) 172 | } 173 | 174 | if center.y > bottomLimitY { 175 | center = CGPoint(x: center.x, y: bottomLimitY) 176 | } else if (center.y <= topLimitY) { 177 | center = CGPoint(x: center.x, y: topLimitY) 178 | } 179 | draggingBlock?() 180 | } 181 | 182 | open override func touchesEnded(_ touches: Set, with event: UIEvent?) { 183 | super.touchesEnded(touches, with: event) 184 | 185 | if dragging, dragDoneBlock != nil { 186 | dragDoneBlock?() 187 | singleTapBeenCanceled = true 188 | } 189 | if dragging, autoDocking, let superviewFrame = self.superview?.frame { 190 | let frame: CGRect = self.frame 191 | let middleX = superviewFrame.size.width / 2.0 192 | 193 | if self.center.x >= middleX { 194 | UIView.animate(withDuration: kDPAutoDockingDuration, animations: { [weak self] in 195 | guard let strongSelf = self else { return } 196 | strongSelf.center = CGPoint(x: superviewFrame.size.width - frame.size.width / 2.0, 197 | y: strongSelf.center.y) 198 | strongSelf.autoDockingBlock?() 199 | }, 200 | completion: { [weak self] finished in 201 | guard let strongSelf = self else { return } 202 | strongSelf.autoDockingDoneBlock?() 203 | }) 204 | } else { 205 | UIView.animate(withDuration: kDPAutoDockingDuration, animations: { [weak self] in 206 | guard let strongSelf = self else { return } 207 | strongSelf.center = CGPoint(x: frame.size.width / 2, y: strongSelf.center.y) 208 | strongSelf.autoDockingBlock?() 209 | }, 210 | completion: { [weak self] finished in 211 | guard let strongSelf = self else { return } 212 | strongSelf.autoDockingDoneBlock?() 213 | }) 214 | } 215 | } 216 | dragging = false 217 | } 218 | 219 | open override func touchesCancelled(_ touches: Set, with event: UIEvent?) { 220 | dragging = false 221 | super.touchesCancelled(touches, with:event) 222 | } 223 | 224 | } 225 | -------------------------------------------------------------------------------- /Tests/DPDraggableButton-SwiftTests/DPDraggableButton_SwiftTests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | @testable import DPDraggableButton_Swift 3 | 4 | final class DPDraggableButton_SwiftTests: XCTestCase { 5 | func testExample() { 6 | // This is an example of a functional test case. 7 | // Use XCTAssert and related functions to verify your tests produce the correct 8 | // results. 9 | XCTAssertEqual(DPDraggableButton_Swift().text, "Hello, World!") 10 | } 11 | 12 | static var allTests = [ 13 | ("testExample", testExample), 14 | ] 15 | } 16 | -------------------------------------------------------------------------------- /Tests/DPDraggableButton-SwiftTests/XCTestManifests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | #if !canImport(ObjectiveC) 4 | public func allTests() -> [XCTestCaseEntry] { 5 | return [ 6 | testCase(DPDraggableButton_SwiftTests.allTests), 7 | ] 8 | } 9 | #endif 10 | -------------------------------------------------------------------------------- /Tests/LinuxMain.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | import DPDraggableButton_SwiftTests 4 | 5 | var tests = [XCTestCaseEntry]() 6 | tests += DPDraggableButton_SwiftTests.allTests() 7 | XCTMain(tests) 8 | --------------------------------------------------------------------------------