├── .github └── workflows │ └── checks.yml ├── .gitignore ├── .gitmodules ├── .swiftformat ├── .swiftpm └── xcode │ └── xcshareddata │ └── xcschemes │ ├── SnapDraggingModifier.xcscheme │ ├── SwiftUISnapDraggingModifier.xcscheme │ └── swiftui-snap-dragging-modifier.xcscheme ├── Development ├── Development.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ └── swiftpm │ │ └── Package.resolved └── Development │ ├── Assets.xcassets │ ├── AccentColor.colorset │ │ └── Contents.json │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json │ ├── BookScrollView.swift │ ├── ContentView.swift │ ├── DevelopmentApp.swift │ └── Preview Content │ └── Preview Assets.xcassets │ └── Contents.json ├── LICENSE ├── Package.resolved ├── Package.swift ├── README.md ├── Sources └── SwiftUISnapDraggingModifier │ ├── Modifiers.swift │ └── SnapDraggingModifier.swift └── Tests └── SnapDraggingModifierTests └── swiftui_snap_dragging_modifierTests.swift /.github/workflows/checks.yml: -------------------------------------------------------------------------------- 1 | name: checks 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | test: 7 | runs-on: macos-13 8 | 9 | steps: 10 | - uses: maxim-lobanov/setup-xcode@v1.1 11 | with: 12 | xcode-version: "15.0" 13 | - uses: actions/checkout@v2 14 | - name: Build 15 | run: xcodebuild -scheme SwiftUISnapDraggingModifier -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 14 Pro,OS=16.4' 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/config/registries.json 8 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 9 | .netrc 10 | .swiftpm 11 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "submodules/swiftui-scrollview-interoperable-drag-gesture"] 2 | path = submodules/swiftui-scrollview-interoperable-drag-gesture 3 | url = https://github.com/FluidGroup/swiftui-scrollview-interoperable-drag-gesture.git 4 | -------------------------------------------------------------------------------- /.swiftformat: -------------------------------------------------------------------------------- 1 | { 2 | "maximumBlankLines" : 1, 3 | "indentConditionalCompilationBlocks" : false, 4 | "lineLength" : 100, 5 | "rules" : { 6 | "DoNotUseSemicolons" : false 7 | }, 8 | "lineBreakBeforeEachGenericRequirement" : true, 9 | "lineBreakBeforeEachArgument" : true, 10 | "indentation" : { 11 | "spaces" : 2 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /.swiftpm/xcode/xcshareddata/xcschemes/SnapDraggingModifier.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 31 | 32 | 42 | 43 | 49 | 50 | 56 | 57 | 58 | 59 | 61 | 62 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /.swiftpm/xcode/xcshareddata/xcschemes/SwiftUISnapDraggingModifier.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 31 | 32 | 42 | 43 | 49 | 50 | 56 | 57 | 58 | 59 | 61 | 62 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /.swiftpm/xcode/xcshareddata/xcschemes/swiftui-snap-dragging-modifier.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 29 | 35 | 36 | 37 | 43 | 49 | 50 | 51 | 52 | 53 | 59 | 60 | 62 | 68 | 69 | 70 | 71 | 72 | 82 | 83 | 89 | 90 | 96 | 97 | 98 | 99 | 101 | 102 | 105 | 106 | 107 | -------------------------------------------------------------------------------- /Development/Development.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 70; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 4B5794852C4FC44B000BC6F9 /* SwiftUISnapDraggingModifier in Frameworks */ = {isa = PBXBuildFile; productRef = 4B5794842C4FC44B000BC6F9 /* SwiftUISnapDraggingModifier */; }; 11 | 4BCD74E42C51692C00073BFD /* SwiftUIScrollViewInteroperableDragGesture in Frameworks */ = {isa = PBXBuildFile; productRef = 4BCD74E32C51692C00073BFD /* SwiftUIScrollViewInteroperableDragGesture */; }; 12 | /* End PBXBuildFile section */ 13 | 14 | /* Begin PBXFileReference section */ 15 | 4B5794722C4FC3F2000BC6F9 /* Development.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Development.app; sourceTree = BUILT_PRODUCTS_DIR; }; 16 | /* End PBXFileReference section */ 17 | 18 | /* Begin PBXFileSystemSynchronizedRootGroup section */ 19 | 4B5794742C4FC3F2000BC6F9 /* Development */ = {isa = PBXFileSystemSynchronizedRootGroup; explicitFileTypes = {}; explicitFolders = (); path = Development; sourceTree = ""; }; 20 | /* End PBXFileSystemSynchronizedRootGroup section */ 21 | 22 | /* Begin PBXFrameworksBuildPhase section */ 23 | 4B57946F2C4FC3F2000BC6F9 /* Frameworks */ = { 24 | isa = PBXFrameworksBuildPhase; 25 | buildActionMask = 2147483647; 26 | files = ( 27 | 4B5794852C4FC44B000BC6F9 /* SwiftUISnapDraggingModifier in Frameworks */, 28 | 4BCD74E42C51692C00073BFD /* SwiftUIScrollViewInteroperableDragGesture in Frameworks */, 29 | ); 30 | runOnlyForDeploymentPostprocessing = 0; 31 | }; 32 | /* End PBXFrameworksBuildPhase section */ 33 | 34 | /* Begin PBXGroup section */ 35 | 4B5794692C4FC3F2000BC6F9 = { 36 | isa = PBXGroup; 37 | children = ( 38 | 4B5794742C4FC3F2000BC6F9 /* Development */, 39 | 4B5794732C4FC3F2000BC6F9 /* Products */, 40 | ); 41 | sourceTree = ""; 42 | }; 43 | 4B5794732C4FC3F2000BC6F9 /* Products */ = { 44 | isa = PBXGroup; 45 | children = ( 46 | 4B5794722C4FC3F2000BC6F9 /* Development.app */, 47 | ); 48 | name = Products; 49 | sourceTree = ""; 50 | }; 51 | /* End PBXGroup section */ 52 | 53 | /* Begin PBXNativeTarget section */ 54 | 4B5794712C4FC3F2000BC6F9 /* Development */ = { 55 | isa = PBXNativeTarget; 56 | buildConfigurationList = 4B5794802C4FC3F3000BC6F9 /* Build configuration list for PBXNativeTarget "Development" */; 57 | buildPhases = ( 58 | 4B57946E2C4FC3F2000BC6F9 /* Sources */, 59 | 4B57946F2C4FC3F2000BC6F9 /* Frameworks */, 60 | 4B5794702C4FC3F2000BC6F9 /* Resources */, 61 | ); 62 | buildRules = ( 63 | ); 64 | dependencies = ( 65 | ); 66 | fileSystemSynchronizedGroups = ( 67 | 4B5794742C4FC3F2000BC6F9 /* Development */, 68 | ); 69 | name = Development; 70 | packageProductDependencies = ( 71 | 4B5794842C4FC44B000BC6F9 /* SwiftUISnapDraggingModifier */, 72 | 4BCD74E32C51692C00073BFD /* SwiftUIScrollViewInteroperableDragGesture */, 73 | ); 74 | productName = Development; 75 | productReference = 4B5794722C4FC3F2000BC6F9 /* Development.app */; 76 | productType = "com.apple.product-type.application"; 77 | }; 78 | /* End PBXNativeTarget section */ 79 | 80 | /* Begin PBXProject section */ 81 | 4B57946A2C4FC3F2000BC6F9 /* Project object */ = { 82 | isa = PBXProject; 83 | attributes = { 84 | BuildIndependentTargetsInParallel = 1; 85 | LastSwiftUpdateCheck = 1600; 86 | LastUpgradeCheck = 1600; 87 | TargetAttributes = { 88 | 4B5794712C4FC3F2000BC6F9 = { 89 | CreatedOnToolsVersion = 16.0; 90 | }; 91 | }; 92 | }; 93 | buildConfigurationList = 4B57946D2C4FC3F2000BC6F9 /* Build configuration list for PBXProject "Development" */; 94 | compatibilityVersion = "Xcode 15.0"; 95 | developmentRegion = en; 96 | hasScannedForEncodings = 0; 97 | knownRegions = ( 98 | en, 99 | Base, 100 | ); 101 | mainGroup = 4B5794692C4FC3F2000BC6F9; 102 | packageReferences = ( 103 | 4B5794832C4FC44B000BC6F9 /* XCLocalSwiftPackageReference "../../swiftui-snap-dragging-modifier" */, 104 | 4BCD74E22C51692C00073BFD /* XCLocalSwiftPackageReference "../submodules/swiftui-scrollview-interoperable-drag-gesture" */, 105 | ); 106 | productRefGroup = 4B5794732C4FC3F2000BC6F9 /* Products */; 107 | projectDirPath = ""; 108 | projectRoot = ""; 109 | targets = ( 110 | 4B5794712C4FC3F2000BC6F9 /* Development */, 111 | ); 112 | }; 113 | /* End PBXProject section */ 114 | 115 | /* Begin PBXResourcesBuildPhase section */ 116 | 4B5794702C4FC3F2000BC6F9 /* Resources */ = { 117 | isa = PBXResourcesBuildPhase; 118 | buildActionMask = 2147483647; 119 | files = ( 120 | ); 121 | runOnlyForDeploymentPostprocessing = 0; 122 | }; 123 | /* End PBXResourcesBuildPhase section */ 124 | 125 | /* Begin PBXSourcesBuildPhase section */ 126 | 4B57946E2C4FC3F2000BC6F9 /* Sources */ = { 127 | isa = PBXSourcesBuildPhase; 128 | buildActionMask = 2147483647; 129 | files = ( 130 | ); 131 | runOnlyForDeploymentPostprocessing = 0; 132 | }; 133 | /* End PBXSourcesBuildPhase section */ 134 | 135 | /* Begin XCBuildConfiguration section */ 136 | 4B57947E2C4FC3F3000BC6F9 /* Debug */ = { 137 | isa = XCBuildConfiguration; 138 | buildSettings = { 139 | ALWAYS_SEARCH_USER_PATHS = NO; 140 | ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; 141 | CLANG_ANALYZER_NONNULL = YES; 142 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 143 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; 144 | CLANG_ENABLE_MODULES = YES; 145 | CLANG_ENABLE_OBJC_ARC = YES; 146 | CLANG_ENABLE_OBJC_WEAK = YES; 147 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 148 | CLANG_WARN_BOOL_CONVERSION = YES; 149 | CLANG_WARN_COMMA = YES; 150 | CLANG_WARN_CONSTANT_CONVERSION = YES; 151 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 152 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 153 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 154 | CLANG_WARN_EMPTY_BODY = YES; 155 | CLANG_WARN_ENUM_CONVERSION = YES; 156 | CLANG_WARN_INFINITE_RECURSION = YES; 157 | CLANG_WARN_INT_CONVERSION = YES; 158 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 159 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 160 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 161 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 162 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 163 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 164 | CLANG_WARN_STRICT_PROTOTYPES = YES; 165 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 166 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 167 | CLANG_WARN_UNREACHABLE_CODE = YES; 168 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 169 | COPY_PHASE_STRIP = NO; 170 | DEBUG_INFORMATION_FORMAT = dwarf; 171 | ENABLE_STRICT_OBJC_MSGSEND = YES; 172 | ENABLE_TESTABILITY = YES; 173 | ENABLE_USER_SCRIPT_SANDBOXING = YES; 174 | GCC_C_LANGUAGE_STANDARD = gnu17; 175 | GCC_DYNAMIC_NO_PIC = NO; 176 | GCC_NO_COMMON_BLOCKS = YES; 177 | GCC_OPTIMIZATION_LEVEL = 0; 178 | GCC_PREPROCESSOR_DEFINITIONS = ( 179 | "DEBUG=1", 180 | "$(inherited)", 181 | ); 182 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 183 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 184 | GCC_WARN_UNDECLARED_SELECTOR = YES; 185 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 186 | GCC_WARN_UNUSED_FUNCTION = YES; 187 | GCC_WARN_UNUSED_VARIABLE = YES; 188 | IPHONEOS_DEPLOYMENT_TARGET = 18.0; 189 | LOCALIZATION_PREFERS_STRING_CATALOGS = YES; 190 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 191 | MTL_FAST_MATH = YES; 192 | ONLY_ACTIVE_ARCH = YES; 193 | SDKROOT = iphoneos; 194 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)"; 195 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 196 | }; 197 | name = Debug; 198 | }; 199 | 4B57947F2C4FC3F3000BC6F9 /* Release */ = { 200 | isa = XCBuildConfiguration; 201 | buildSettings = { 202 | ALWAYS_SEARCH_USER_PATHS = NO; 203 | ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; 204 | CLANG_ANALYZER_NONNULL = YES; 205 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 206 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; 207 | CLANG_ENABLE_MODULES = YES; 208 | CLANG_ENABLE_OBJC_ARC = YES; 209 | CLANG_ENABLE_OBJC_WEAK = YES; 210 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 211 | CLANG_WARN_BOOL_CONVERSION = YES; 212 | CLANG_WARN_COMMA = YES; 213 | CLANG_WARN_CONSTANT_CONVERSION = YES; 214 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 215 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 216 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 217 | CLANG_WARN_EMPTY_BODY = YES; 218 | CLANG_WARN_ENUM_CONVERSION = YES; 219 | CLANG_WARN_INFINITE_RECURSION = YES; 220 | CLANG_WARN_INT_CONVERSION = YES; 221 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 222 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 223 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 224 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 225 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 226 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 227 | CLANG_WARN_STRICT_PROTOTYPES = YES; 228 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 229 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 230 | CLANG_WARN_UNREACHABLE_CODE = YES; 231 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 232 | COPY_PHASE_STRIP = NO; 233 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 234 | ENABLE_NS_ASSERTIONS = NO; 235 | ENABLE_STRICT_OBJC_MSGSEND = YES; 236 | ENABLE_USER_SCRIPT_SANDBOXING = YES; 237 | GCC_C_LANGUAGE_STANDARD = gnu17; 238 | GCC_NO_COMMON_BLOCKS = YES; 239 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 240 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 241 | GCC_WARN_UNDECLARED_SELECTOR = YES; 242 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 243 | GCC_WARN_UNUSED_FUNCTION = YES; 244 | GCC_WARN_UNUSED_VARIABLE = YES; 245 | IPHONEOS_DEPLOYMENT_TARGET = 18.0; 246 | LOCALIZATION_PREFERS_STRING_CATALOGS = YES; 247 | MTL_ENABLE_DEBUG_INFO = NO; 248 | MTL_FAST_MATH = YES; 249 | SDKROOT = iphoneos; 250 | SWIFT_COMPILATION_MODE = wholemodule; 251 | VALIDATE_PRODUCT = YES; 252 | }; 253 | name = Release; 254 | }; 255 | 4B5794812C4FC3F3000BC6F9 /* Debug */ = { 256 | isa = XCBuildConfiguration; 257 | buildSettings = { 258 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 259 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 260 | CODE_SIGN_STYLE = Automatic; 261 | CURRENT_PROJECT_VERSION = 1; 262 | DEVELOPMENT_ASSET_PATHS = "\"Development/Preview Content\""; 263 | DEVELOPMENT_TEAM = JX92XL88RZ; 264 | ENABLE_PREVIEWS = YES; 265 | GENERATE_INFOPLIST_FILE = YES; 266 | INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; 267 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 268 | INFOPLIST_KEY_UILaunchScreen_Generation = YES; 269 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 270 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 271 | LD_RUNPATH_SEARCH_PATHS = ( 272 | "$(inherited)", 273 | "@executable_path/Frameworks", 274 | ); 275 | MARKETING_VERSION = 1.0; 276 | PRODUCT_BUNDLE_IDENTIFIER = app.muukii.Development; 277 | PRODUCT_NAME = "$(TARGET_NAME)"; 278 | SWIFT_EMIT_LOC_STRINGS = YES; 279 | SWIFT_VERSION = 5.0; 280 | TARGETED_DEVICE_FAMILY = "1,2"; 281 | }; 282 | name = Debug; 283 | }; 284 | 4B5794822C4FC3F3000BC6F9 /* Release */ = { 285 | isa = XCBuildConfiguration; 286 | buildSettings = { 287 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 288 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 289 | CODE_SIGN_STYLE = Automatic; 290 | CURRENT_PROJECT_VERSION = 1; 291 | DEVELOPMENT_ASSET_PATHS = "\"Development/Preview Content\""; 292 | DEVELOPMENT_TEAM = JX92XL88RZ; 293 | ENABLE_PREVIEWS = YES; 294 | GENERATE_INFOPLIST_FILE = YES; 295 | INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; 296 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 297 | INFOPLIST_KEY_UILaunchScreen_Generation = YES; 298 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 299 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 300 | LD_RUNPATH_SEARCH_PATHS = ( 301 | "$(inherited)", 302 | "@executable_path/Frameworks", 303 | ); 304 | MARKETING_VERSION = 1.0; 305 | PRODUCT_BUNDLE_IDENTIFIER = app.muukii.Development; 306 | PRODUCT_NAME = "$(TARGET_NAME)"; 307 | SWIFT_EMIT_LOC_STRINGS = YES; 308 | SWIFT_VERSION = 5.0; 309 | TARGETED_DEVICE_FAMILY = "1,2"; 310 | }; 311 | name = Release; 312 | }; 313 | /* End XCBuildConfiguration section */ 314 | 315 | /* Begin XCConfigurationList section */ 316 | 4B57946D2C4FC3F2000BC6F9 /* Build configuration list for PBXProject "Development" */ = { 317 | isa = XCConfigurationList; 318 | buildConfigurations = ( 319 | 4B57947E2C4FC3F3000BC6F9 /* Debug */, 320 | 4B57947F2C4FC3F3000BC6F9 /* Release */, 321 | ); 322 | defaultConfigurationIsVisible = 0; 323 | defaultConfigurationName = Release; 324 | }; 325 | 4B5794802C4FC3F3000BC6F9 /* Build configuration list for PBXNativeTarget "Development" */ = { 326 | isa = XCConfigurationList; 327 | buildConfigurations = ( 328 | 4B5794812C4FC3F3000BC6F9 /* Debug */, 329 | 4B5794822C4FC3F3000BC6F9 /* Release */, 330 | ); 331 | defaultConfigurationIsVisible = 0; 332 | defaultConfigurationName = Release; 333 | }; 334 | /* End XCConfigurationList section */ 335 | 336 | /* Begin XCLocalSwiftPackageReference section */ 337 | 4B5794832C4FC44B000BC6F9 /* XCLocalSwiftPackageReference "../../swiftui-snap-dragging-modifier" */ = { 338 | isa = XCLocalSwiftPackageReference; 339 | relativePath = "../../swiftui-snap-dragging-modifier"; 340 | }; 341 | 4BCD74E22C51692C00073BFD /* XCLocalSwiftPackageReference "../submodules/swiftui-scrollview-interoperable-drag-gesture" */ = { 342 | isa = XCLocalSwiftPackageReference; 343 | relativePath = "../submodules/swiftui-scrollview-interoperable-drag-gesture"; 344 | }; 345 | /* End XCLocalSwiftPackageReference section */ 346 | 347 | /* Begin XCSwiftPackageProductDependency section */ 348 | 4B5794842C4FC44B000BC6F9 /* SwiftUISnapDraggingModifier */ = { 349 | isa = XCSwiftPackageProductDependency; 350 | productName = SwiftUISnapDraggingModifier; 351 | }; 352 | 4BCD74E32C51692C00073BFD /* SwiftUIScrollViewInteroperableDragGesture */ = { 353 | isa = XCSwiftPackageProductDependency; 354 | productName = SwiftUIScrollViewInteroperableDragGesture; 355 | }; 356 | /* End XCSwiftPackageProductDependency section */ 357 | }; 358 | rootObject = 4B57946A2C4FC3F2000BC6F9 /* Project object */; 359 | } 360 | -------------------------------------------------------------------------------- /Development/Development.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Development/Development.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved: -------------------------------------------------------------------------------- 1 | { 2 | "originHash" : "bf69f78dc379b70fc68829e1f8cba58518fbec4de48b39f78f417eb30a455bfa", 3 | "pins" : [ 4 | { 5 | "identity" : "swift-rubber-banding", 6 | "kind" : "remoteSourceControl", 7 | "location" : "https://github.com/FluidGroup/swift-rubber-banding", 8 | "state" : { 9 | "revision" : "f2dbd09829c6267db0a769e7c37fe332dab0b675", 10 | "version" : "1.0.0" 11 | } 12 | }, 13 | { 14 | "identity" : "swiftui-support", 15 | "kind" : "remoteSourceControl", 16 | "location" : "https://github.com/FluidGroup/swiftui-support", 17 | "state" : { 18 | "revision" : "266e052494f8b7432374ad19a4bd31877cf54640", 19 | "version" : "0.10.0" 20 | } 21 | } 22 | ], 23 | "version" : 3 24 | } 25 | -------------------------------------------------------------------------------- /Development/Development/Assets.xcassets/AccentColor.colorset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "colors" : [ 3 | { 4 | "idiom" : "universal" 5 | } 6 | ], 7 | "info" : { 8 | "author" : "xcode", 9 | "version" : 1 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Development/Development/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "platform" : "ios", 6 | "size" : "1024x1024" 7 | }, 8 | { 9 | "appearances" : [ 10 | { 11 | "appearance" : "luminosity", 12 | "value" : "dark" 13 | } 14 | ], 15 | "idiom" : "universal", 16 | "platform" : "ios", 17 | "size" : "1024x1024" 18 | }, 19 | { 20 | "appearances" : [ 21 | { 22 | "appearance" : "luminosity", 23 | "value" : "tinted" 24 | } 25 | ], 26 | "idiom" : "universal", 27 | "platform" : "ios", 28 | "size" : "1024x1024" 29 | } 30 | ], 31 | "info" : { 32 | "author" : "xcode", 33 | "version" : 1 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Development/Development/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Development/Development/BookScrollView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // BookScrollView.swift 3 | // Development 4 | // 5 | // Created by Muukii on 2024/07/25. 6 | // 7 | 8 | import SwiftUI 9 | import SwiftUIScrollViewInteroperableDragGesture 10 | import SwiftUISnapDraggingModifier 11 | 12 | #if DEBUG 13 | 14 | @available(iOS 18, *) 15 | private var scrollView: some View { 16 | ScrollView([.horizontal, .vertical]) { 17 | Grid(horizontalSpacing: 20, verticalSpacing: 20) { 18 | ForEach(0..<4) { _ in 19 | GridRow { 20 | ForEach(0..<4) { _ in Color.teal.frame(width: 30, height: 30) } 21 | } 22 | } 23 | } 24 | .padding() 25 | .background(Color.red) 26 | .padding() 27 | .background(Color.blue) 28 | } 29 | } 30 | 31 | @available(iOS 18, *) 32 | #Preview("Normal") { 33 | 34 | @Previewable @State var offset: CGSize = .zero 35 | 36 | ZStack { 37 | 38 | VStack { 39 | scrollView 40 | } 41 | .frame(width: 200, height: 200) 42 | .background(Color.green.secondary) 43 | .padding() 44 | .background(Color.green.tertiary) 45 | .modifier( 46 | SnapDraggingModifier( 47 | gestureMode: .scrollViewInteroperable( 48 | .init(ignoresScrollView: false, targetEdges: [], sticksToEdges: false) 49 | ), 50 | offset: $offset 51 | ) 52 | ) 53 | .background(Color.purple.tertiary) 54 | 55 | } 56 | } 57 | 58 | @available(iOS 18, *) 59 | #Preview("SticksToEdges") { 60 | 61 | @Previewable @State var offset: CGSize = .zero 62 | 63 | ZStack { 64 | 65 | VStack { 66 | scrollView 67 | } 68 | .frame(width: 200, height: 200) 69 | .background(Color.green.secondary) 70 | .padding() 71 | .background(Color.green.tertiary) 72 | .modifier( 73 | SnapDraggingModifier( 74 | gestureMode: .scrollViewInteroperable( 75 | .init(ignoresScrollView: false, targetEdges: .all, sticksToEdges: true) 76 | ), 77 | offset: $offset 78 | ) 79 | ) 80 | .background(Color.purple.tertiary) 81 | 82 | } 83 | } 84 | 85 | @available(iOS 18, *) 86 | #Preview("IgnoreScrollView") { 87 | 88 | @Previewable @State var offset: CGSize = .zero 89 | 90 | ZStack { 91 | 92 | VStack { 93 | scrollView 94 | } 95 | .frame(width: 200, height: 200) 96 | .background(Color.green.secondary) 97 | .padding() 98 | .background(Color.green.tertiary) 99 | .modifier( 100 | SnapDraggingModifier( 101 | gestureMode: .scrollViewInteroperable( 102 | .init(ignoresScrollView: true, targetEdges: .all, sticksToEdges: true) 103 | ), 104 | offset: $offset 105 | ) 106 | ) 107 | .background(Color.purple.tertiary) 108 | 109 | } 110 | } 111 | 112 | 113 | #endif 114 | -------------------------------------------------------------------------------- /Development/Development/ContentView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ContentView.swift 3 | // Development 4 | // 5 | // Created by Muukii on 2024/07/23. 6 | // 7 | 8 | import SwiftUI 9 | import SwiftUISnapDraggingModifier 10 | 11 | struct ContentView: View { 12 | var body: some View { 13 | Joystick() 14 | } 15 | } 16 | 17 | #Preview { 18 | ContentView() 19 | } 20 | 21 | struct Joystick: View { 22 | 23 | @State var offset: CGSize = .zero 24 | 25 | @State var isOn: Bool = false 26 | 27 | var body: some View { 28 | stick 29 | .padding(10) 30 | } 31 | 32 | private var stick: some View { 33 | 34 | VStack { 35 | 36 | Button("Add offset") { 37 | withAnimation(.interpolatingSpring(mass: 1, stiffness: 1, damping: 1, initialVelocity: 0)) { 38 | offset.width += 10 39 | } 40 | } 41 | 42 | Circle() 43 | .fill(Color.yellow) 44 | .frame(width: 100, height: 100) 45 | .modifier( 46 | SnapDraggingModifier( 47 | gestureMode: .normal, 48 | offset: $offset, 49 | springParameter: .interpolation(mass: 1, stiffness: 1, damping: 1) 50 | ) 51 | ) 52 | Circle() 53 | .fill(Color.green) 54 | .frame(width: 100, height: 100) 55 | 56 | } 57 | .padding(20) 58 | .background(Color.secondary) 59 | .coordinateSpace(name: "A") 60 | 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Development/Development/DevelopmentApp.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DevelopmentApp.swift 3 | // Development 4 | // 5 | // Created by Muukii on 2024/07/23. 6 | // 7 | 8 | import SwiftUI 9 | 10 | @main 11 | struct DevelopmentApp: App { 12 | var body: some Scene { 13 | WindowGroup { 14 | ContentView() 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Development/Development/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /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 [yyyy] [name of copyright owner] 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 | -------------------------------------------------------------------------------- /Package.resolved: -------------------------------------------------------------------------------- 1 | { 2 | "pins" : [ 3 | { 4 | "identity" : "swift-rubber-banding", 5 | "kind" : "remoteSourceControl", 6 | "location" : "https://github.com/FluidGroup/swift-rubber-banding", 7 | "state" : { 8 | "revision" : "f2dbd09829c6267db0a769e7c37fe332dab0b675", 9 | "version" : "1.0.0" 10 | } 11 | }, 12 | { 13 | "identity" : "swiftui-scrollview-interoperable-drag-gesture", 14 | "kind" : "remoteSourceControl", 15 | "location" : "https://github.com/FluidGroup/swiftui-scrollview-interoperable-drag-gesture", 16 | "state" : { 17 | "revision" : "60854a651c8224b3e08252cc5864c149d59a9739", 18 | "version" : "0.2.0" 19 | } 20 | }, 21 | { 22 | "identity" : "swiftui-support", 23 | "kind" : "remoteSourceControl", 24 | "location" : "https://github.com/FluidGroup/swiftui-support", 25 | "state" : { 26 | "revision" : "10b463fc241552c4c6668700c37d4112ae926fe5", 27 | "version" : "0.12.0" 28 | } 29 | } 30 | ], 31 | "version" : 2 32 | } 33 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 5.7 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: "swiftui-snap-dragging-modifier", 8 | platforms: [.iOS(.v16)], 9 | products: [ 10 | .library( 11 | name: "SwiftUISnapDraggingModifier", 12 | targets: ["SwiftUISnapDraggingModifier"] 13 | ) 14 | ], 15 | dependencies: [ 16 | .package(url: "https://github.com/FluidGroup/swiftui-support", from: "0.9.0"), 17 | .package(url: "https://github.com/FluidGroup/swift-rubber-banding", from: "1.0.0"), 18 | .package(url: "https://github.com/FluidGroup/swiftui-scrollview-interoperable-drag-gesture", from: "0.1.0") 19 | ], 20 | targets: [ 21 | .target( 22 | name: "SwiftUISnapDraggingModifier", 23 | dependencies: [ 24 | .product(name: "RubberBanding", package: "swift-rubber-banding"), 25 | .product(name: "SwiftUISupportSizing", package: "swiftui-support"), 26 | .product(name: "SwiftUISupportGeometryEffect", package: "swiftui-support"), 27 | .product(name: "SwiftUIScrollViewInteroperableDragGesture", package: "swiftui-scrollview-interoperable-drag-gesture"), 28 | ] 29 | ), 30 | .testTarget( 31 | name: "SnapDraggingModifierTests", 32 | dependencies: ["SwiftUISnapDraggingModifier"] 33 | ), 34 | ] 35 | ) 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SwiftUI - SnapDraggingModifier 2 | 3 | This is a small SwiftUI package that allows for the creation of a draggable view and tracks the velocity of the dragging action, which can be used to create fluid animations when the drag is released. This component is a big help in creating interactive user interfaces and enhancing their fluidity. 4 | 5 | About Fluid interfaces : https://developer.apple.com/videos/play/wwdc2018/803/ 6 | 7 | > [UIKit Version](https://github.com/FluidGroup/FluidInterfaceKit) FluidInterfaceKit/FluidGesture module 8 | 9 | ## Examples 10 | 11 | **Throwing a ball** 12 | 13 | 14 | 15 | ```swift 16 | Circle() 17 | .fill(Color.blue) 18 | .frame(width: 100, height: 100) 19 | .modifier(SnapDraggingModifier()) 20 | ``` 21 | 22 | --- 23 | 24 | **Fixed draggable direction and rubber banding effect** 25 | 26 | 27 | 28 | 29 | ```swift 30 | RoundedRectangle(cornerRadius: 16, style: .continuous) 31 | .fill(Color.blue) 32 | .frame(width: 120, height: 50) 33 | .modifier( 34 | SnapDraggingModifier( 35 | axis: [.vertical], 36 | verticalBoundary: .init(min: -10, max: 10, bandLength: 50) 37 | ) 38 | ) 39 | ``` 40 | 41 | --- 42 | 43 | **Thowing to the point** 44 | 45 | 46 | 47 | "The modifier asks for the destination point when the gesture ends, and the view will smoothly move to the specified point with velocity-based animation." 48 | 49 | ```swift 50 | RoundedRectangle(cornerRadius: 16, style: .continuous) 51 | .fill(Color.blue) 52 | .frame(width: nil, height: 50) 53 | .modifier( 54 | SnapDraggingModifier( 55 | axis: .horizontal, 56 | horizontalBoundary: .init(min: 0, max: .infinity, bandLength: 50), 57 | handler: .init(onEndDragging: { velocity, offset, contentSize in 58 | 59 | print(velocity, offset, contentSize) 60 | 61 | if velocity.dx > 50 || offset.width > (contentSize.width / 2) { 62 | print("remove") 63 | return .init(width: contentSize.width, height: 0) 64 | } else { 65 | print("stay") 66 | return .zero 67 | } 68 | }) 69 | ) 70 | ) 71 | ``` 72 | -------------------------------------------------------------------------------- /Sources/SwiftUISnapDraggingModifier/Modifiers.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | struct XTranslationEffect: GeometryEffect { 4 | 5 | var offset: CGFloat = .zero 6 | 7 | @Binding var presenting: CGFloat 8 | 9 | init(offset: CGFloat, presenting: Binding) { 10 | self.offset = offset 11 | self._presenting = presenting 12 | } 13 | 14 | var animatableData: CGFloat { 15 | get { 16 | offset 17 | } 18 | set { 19 | DispatchQueue.main.async { [$presenting] in 20 | $presenting.wrappedValue = newValue 21 | } 22 | offset = newValue 23 | } 24 | } 25 | 26 | func effectValue(size: CGSize) -> ProjectionTransform { 27 | return .init(.init(translationX: offset, y: 0)) 28 | } 29 | 30 | } 31 | 32 | struct YTranslationEffect: GeometryEffect { 33 | 34 | var offset: CGFloat = .zero 35 | 36 | @Binding var presenting: CGFloat 37 | 38 | init(offset: CGFloat, presenting: Binding) { 39 | self.offset = offset 40 | self._presenting = presenting 41 | } 42 | 43 | var animatableData: CGFloat { 44 | get { 45 | offset 46 | } 47 | set { 48 | DispatchQueue.main.async { [$presenting] in 49 | $presenting.wrappedValue = newValue 50 | } 51 | offset = newValue 52 | } 53 | } 54 | 55 | func effectValue(size: CGSize) -> ProjectionTransform { 56 | return .init(.init(translationX: 0, y: offset)) 57 | } 58 | 59 | } 60 | 61 | extension View { 62 | 63 | /// Applies offset effect that is animatable against ``SwiftUI/View/offset`` 64 | func _animatableOffset(x: CGFloat, presenting: Binding) -> some View { 65 | self.modifier(XTranslationEffect(offset: x, presenting: presenting)) 66 | } 67 | 68 | /// Applies offset effect that is animatable against ``SwiftUI/View/offset`` 69 | func _animatableOffset(y: CGFloat, presenting: Binding) -> some View { 70 | self.modifier(YTranslationEffect(offset: y, presenting: presenting)) 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /Sources/SwiftUISnapDraggingModifier/SnapDraggingModifier.swift: -------------------------------------------------------------------------------- 1 | import RubberBanding 2 | import SwiftUI 3 | import SwiftUIScrollViewInteroperableDragGesture 4 | import SwiftUISupportGeometryEffect 5 | import SwiftUISupportSizing 6 | 7 | public protocol GestureMode { 8 | 9 | } 10 | 11 | public struct GestureModeNormal: GestureMode {} 12 | 13 | public struct GestureModeHighPriority: GestureMode {} 14 | 15 | @available(iOS 18, *) 16 | public struct GestureModeScrollViewInteroperable: GestureMode { 17 | 18 | let configuration: ScrollViewInteroperableDragGesture.Configuration 19 | 20 | public init(configuration: ScrollViewInteroperableDragGesture.Configuration) { 21 | self.configuration = configuration 22 | } 23 | } 24 | 25 | extension GestureMode where Self == GestureModeNormal { 26 | 27 | public static var normal: Self { 28 | .init() 29 | } 30 | } 31 | 32 | extension GestureMode where Self == GestureModeHighPriority { 33 | 34 | public static var highPriority: Self { 35 | .init() 36 | } 37 | } 38 | 39 | @available(iOS 18, *) 40 | extension GestureMode where Self == GestureModeScrollViewInteroperable { 41 | 42 | public static func scrollViewInteroperable( 43 | _ configuration: ScrollViewInteroperableDragGesture.Configuration 44 | ) -> Self { 45 | .init(configuration: configuration) 46 | } 47 | } 48 | 49 | public struct SnapDraggingModifier: ViewModifier { 50 | 51 | public struct Activation { 52 | 53 | public enum Region { 54 | /// entire view 55 | case screen 56 | /// 57 | case edge(Edge.Set) 58 | } 59 | 60 | public let minimumDistance: Double 61 | public let regionToActivate: Region 62 | 63 | public init(minimumDistance: Double = 0, regionToActivate: Region = .screen) { 64 | self.minimumDistance = minimumDistance 65 | self.regionToActivate = regionToActivate 66 | } 67 | } 68 | 69 | public struct Handler { 70 | /** 71 | A callback closure that is called when the user finishes dragging the content. 72 | This closure takes a CGSize as a return value, which is used as the target offset to finalize the animation. 73 | 74 | For example, return CGSize.zero to put it back to the original position. 75 | */ 76 | public var onEndDragging: 77 | (_ velocity: inout CGVector, _ offset: CGSize, _ contentSize: CGSize) -> CGSize 78 | 79 | public var onStartDragging: () -> Void 80 | 81 | fileprivate var onCompleteAnimation: () -> Void 82 | 83 | public init( 84 | onStartDragging: @escaping () -> Void = {}, 85 | onEndDragging: @escaping (_ velocity: inout CGVector, _ offset: CGSize, _ contentSize: CGSize) 86 | -> CGSize = { _, _, _ in .zero } 87 | ) { 88 | self.onStartDragging = onStartDragging 89 | self.onEndDragging = onEndDragging 90 | self.onCompleteAnimation = {} 91 | } 92 | 93 | @available(iOS 17.0, *) 94 | public init( 95 | onStartDragging: @escaping () -> Void = {}, 96 | onEndDragging: @escaping (_ velocity: inout CGVector, _ offset: CGSize, _ contentSize: CGSize) 97 | -> CGSize = { _, _, _ in .zero }, 98 | onCompleteAnimation: @escaping () -> Void 99 | ) { 100 | self.onStartDragging = onStartDragging 101 | self.onEndDragging = onEndDragging 102 | self.onCompleteAnimation = onCompleteAnimation 103 | } 104 | } 105 | 106 | public enum SpringParameter { 107 | case interpolation( 108 | mass: Double, 109 | stiffness: Double, 110 | damping: Double 111 | ) 112 | 113 | public static var hard: Self { 114 | .interpolation(mass: 1.0, stiffness: 200, damping: 20) 115 | } 116 | } 117 | 118 | public struct Boundary { 119 | public let min: Double 120 | public let max: Double 121 | public let bandLength: Double 122 | 123 | public init(min: Double, max: Double, bandLength: Double) { 124 | self.min = min 125 | self.max = max 126 | self.bandLength = bandLength 127 | } 128 | 129 | public static var infinity: Self { 130 | return .init( 131 | min: -Double.greatestFiniteMagnitude, 132 | max: Double.greatestFiniteMagnitude, 133 | bandLength: 0 134 | ) 135 | } 136 | } 137 | 138 | /** 139 | ??? 140 | Use just State instead of GestureState to trigger animation on gesture ended. 141 | This approach is right? 142 | 143 | refs: 144 | https://stackoverflow.com/questions/72880712/animate-gesturestate-on-reset 145 | */ 146 | @Binding private var currentOffset: CGSize 147 | 148 | // value for animating 149 | @State private var presentingOffset: CGSize = .zero 150 | 151 | @State private var targetOffset: CGSize = .zero 152 | 153 | @GestureState private var initialOffset: CGSize? 154 | @GestureState private var isTracking = false 155 | @GestureState private var pointInView: CGPoint = .zero 156 | 157 | @State private var isActive = false 158 | @State private var contentSize: CGSize = .zero 159 | 160 | @Environment(\.layoutDirection) var layoutDirection 161 | 162 | public let axis: Axis.Set 163 | public let springParameter: SpringParameter 164 | public let gestureMode: any GestureMode 165 | public let activation: Activation 166 | 167 | private let horizontalBoundary: Boundary 168 | private let verticalBoundary: Boundary 169 | private let handler: Handler 170 | 171 | public init( 172 | gestureMode: any GestureMode, 173 | offset: Binding, 174 | activation: Activation = .init(), 175 | axis: Axis.Set = [.horizontal, .vertical], 176 | horizontalBoundary: Boundary = .infinity, 177 | verticalBoundary: Boundary = .infinity, 178 | springParameter: SpringParameter = .hard, 179 | handler: Handler = .init() 180 | ) { 181 | self._currentOffset = offset 182 | self.axis = axis 183 | self.springParameter = springParameter 184 | self.horizontalBoundary = horizontalBoundary 185 | self.verticalBoundary = verticalBoundary 186 | self.gestureMode = gestureMode 187 | self.handler = handler 188 | self.activation = activation 189 | } 190 | 191 | public func body(content: Content) -> some View { 192 | 193 | let base = 194 | content 195 | .coordinateSpace(name: _CoordinateSpaceTag.pointInView) 196 | .measureSize($contentSize) 197 | .onChange(of: isTracking) { newValue in 198 | if isTracking == false, currentOffset != targetOffset { 199 | // For recovery of gesture unexpectedly canceled by the other gesture. 200 | // `onEnded` never get called in the case. 201 | self.onEnded(velocity: .zero) 202 | } 203 | } 204 | 205 | if true, #available(iOS 18, *) { 206 | 207 | Group { 208 | switch gestureMode { 209 | case let normal as GestureModeNormal: 210 | base 211 | .gesture(dragGesture.simultaneously(with: gesture), including: .all) 212 | case let highPriority as GestureModeHighPriority: 213 | base 214 | .highPriorityGesture(dragGesture.simultaneously(with: gesture), including: .all) 215 | case let scrollViewInteroperable as GestureModeScrollViewInteroperable: 216 | base 217 | .gesture(_gesture(configuration: scrollViewInteroperable.configuration)) 218 | .simultaneousGesture(gesture) 219 | default: 220 | EmptyView() 221 | } 222 | } 223 | ._animatableOffset(x: currentOffset.width, presenting: $presentingOffset.width) 224 | ._animatableOffset(y: currentOffset.height, presenting: $presentingOffset.height) 225 | 226 | .coordinateSpace(name: _CoordinateSpaceTag.transition) 227 | .onChange(of: isTracking) { newValue in 228 | if newValue { 229 | handler.onStartDragging() 230 | } 231 | } 232 | 233 | } else { 234 | 235 | let addingGesture = dragGesture.simultaneously(with: gesture) 236 | 237 | Group { 238 | switch gestureMode { 239 | case let normal as GestureModeNormal: 240 | base 241 | .gesture(addingGesture, including: .all) 242 | case let highPriority as GestureModeHighPriority: 243 | base 244 | .highPriorityGesture(addingGesture, including: .all) 245 | default: 246 | EmptyView() 247 | } 248 | } 249 | ._animatableOffset(x: currentOffset.width, presenting: $presentingOffset.width) 250 | ._animatableOffset(y: currentOffset.height, presenting: $presentingOffset.height) 251 | 252 | .coordinateSpace(name: _CoordinateSpaceTag.transition) 253 | .onChange(of: isTracking) { newValue in 254 | if newValue { 255 | handler.onStartDragging() 256 | } 257 | } 258 | 259 | } 260 | 261 | } 262 | 263 | private func isInActivation(startLocation: CGPoint) -> Bool { 264 | 265 | switch activation.regionToActivate { 266 | case .screen: 267 | return true 268 | case .edge(let edge): 269 | 270 | let space: Double = 20 271 | let contentSize = self.contentSize 272 | 273 | if edge.contains(.leading) { 274 | switch layoutDirection { 275 | case .leftToRight: 276 | if CGRect(origin: .zero, size: .init(width: space, height: contentSize.height)).contains( 277 | startLocation 278 | ) { 279 | return true 280 | } 281 | case .rightToLeft: 282 | if CGRect( 283 | origin: .init(x: contentSize.width - space, y: 0), 284 | size: .init(width: space, height: contentSize.height) 285 | ).contains(startLocation) { 286 | return true 287 | } 288 | @unknown default: 289 | break 290 | } 291 | } 292 | 293 | if edge.contains(.trailing) { 294 | switch layoutDirection { 295 | case .leftToRight: 296 | if CGRect( 297 | origin: .init(x: contentSize.width - space, y: 0), 298 | size: .init(width: space, height: contentSize.height) 299 | ).contains(startLocation) { 300 | return true 301 | } 302 | case .rightToLeft: 303 | if CGRect(origin: .zero, size: .init(width: 20, height: CGFloat.greatestFiniteMagnitude)) 304 | .contains(startLocation) 305 | { 306 | return true 307 | } 308 | @unknown default: 309 | return false 310 | } 311 | } 312 | 313 | if edge.contains(.top) { 314 | if CGRect(origin: .zero, size: .init(width: contentSize.width, height: space)).contains( 315 | startLocation 316 | ) { 317 | return true 318 | } 319 | } 320 | 321 | if edge.contains(.bottom) { 322 | if CGRect( 323 | origin: .init(x: 0, y: contentSize.height - space), 324 | size: .init(width: contentSize.width, height: space) 325 | ).contains(startLocation) { 326 | return true 327 | } 328 | } 329 | 330 | return false 331 | } 332 | 333 | } 334 | 335 | private var gesture: some Gesture { 336 | DragGesture(minimumDistance: 0, coordinateSpace: .named(_CoordinateSpaceTag.pointInView)) 337 | .updating( 338 | $pointInView, 339 | body: { v, s, _ in 340 | s = v.startLocation 341 | } 342 | ) 343 | } 344 | 345 | @available(iOS 18.0, *) 346 | @available(macOS, unavailable) 347 | @available(tvOS, unavailable) 348 | @available(watchOS, unavailable) 349 | @available(visionOS, unavailable) 350 | private func _gesture(configuration: ScrollViewInteroperableDragGesture.Configuration) 351 | -> ScrollViewInteroperableDragGesture 352 | { 353 | 354 | let baseOffset = presentingOffset 355 | 356 | return ScrollViewInteroperableDragGesture( 357 | configuration: configuration, 358 | coordinateSpaceInDragging: .named(_CoordinateSpaceTag.transition), 359 | onChange: { value in 360 | 361 | // if self.isActive || isInActivation(startLocation: value.startLocation) { 362 | // 363 | // self.isActive = true 364 | 365 | let proposedOffset = CGSize( 366 | width: baseOffset.width + value.translation.width, 367 | height: baseOffset.height + value.translation.height 368 | ) 369 | 370 | // TODO: stop the current animation when dragging restarted. 371 | withAnimation(.interactiveSpring()) { 372 | if axis.contains(.horizontal) { 373 | currentOffset.width = rubberBand( 374 | value: proposedOffset.width, 375 | min: horizontalBoundary.min, 376 | max: horizontalBoundary.max, 377 | bandLength: horizontalBoundary.bandLength 378 | ) 379 | } 380 | if axis.contains(.vertical) { 381 | currentOffset.height = rubberBand( 382 | value: proposedOffset.height, 383 | min: verticalBoundary.min, 384 | max: verticalBoundary.max, 385 | bandLength: verticalBoundary.bandLength 386 | ) 387 | } 388 | } 389 | // } 390 | }, 391 | onEnd: { value in 392 | onEnded( 393 | velocity: .init( 394 | dx: value.velocity.width, 395 | dy: value.velocity.height 396 | ) 397 | ) 398 | }) 399 | } 400 | 401 | private var dragGesture: some Gesture { 402 | 403 | DragGesture( 404 | minimumDistance: activation.minimumDistance, 405 | coordinateSpace: .named(_CoordinateSpaceTag.transition) 406 | ) 407 | .updating( 408 | $initialOffset, 409 | body: { _, state, _ in 410 | if state == nil { 411 | state = presentingOffset 412 | } 413 | } 414 | ) 415 | .updating( 416 | $isTracking, 417 | body: { _, state, _ in 418 | state = true 419 | } 420 | ) 421 | .onChanged({ value in 422 | 423 | if self.isActive || isInActivation(startLocation: value.startLocation) { 424 | 425 | self.isActive = true 426 | 427 | // TODO: including minimumDistance 428 | 429 | // Because of GestureState, this value is set always. 430 | let baseOffset = initialOffset! 431 | 432 | let proposedOffset = CGSize( 433 | width: baseOffset.width + value.translation.width, 434 | height: baseOffset.height + value.translation.height 435 | ) 436 | 437 | // TODO: stop the current animation when dragging restarted. 438 | withAnimation(.interactiveSpring()) { 439 | if axis.contains(.horizontal) { 440 | currentOffset.width = rubberBand( 441 | value: proposedOffset.width, 442 | min: horizontalBoundary.min, 443 | max: horizontalBoundary.max, 444 | bandLength: horizontalBoundary.bandLength 445 | ) 446 | } 447 | if axis.contains(.vertical) { 448 | currentOffset.height = rubberBand( 449 | value: proposedOffset.height, 450 | min: verticalBoundary.min, 451 | max: verticalBoundary.max, 452 | bandLength: verticalBoundary.bandLength 453 | ) 454 | } 455 | } 456 | } 457 | }) 458 | .onEnded({ value in 459 | 460 | if isActive { 461 | onEnded( 462 | velocity: .init( 463 | dx: value.velocity.width, 464 | dy: value.velocity.height 465 | ) 466 | ) 467 | } else { 468 | assert(currentOffset == targetOffset) 469 | } 470 | 471 | self.isActive = false 472 | }) 473 | 474 | } 475 | 476 | private func onEnded(velocity: CGVector) { 477 | var usingVelocity = velocity 478 | 479 | let targetOffset: CGSize = handler.onEndDragging( 480 | &usingVelocity, 481 | self.currentOffset, 482 | self.contentSize 483 | ) 484 | 485 | self.targetOffset = targetOffset 486 | 487 | let velocity = usingVelocity 488 | 489 | let distance = CGSize( 490 | width: targetOffset.width - currentOffset.width, 491 | height: targetOffset.height - currentOffset.height 492 | ) 493 | 494 | let mappedVelocity = CGVector( 495 | dx: velocity.dx / distance.width, 496 | dy: velocity.dy / distance.height 497 | ) 498 | 499 | var animationX: Animation { 500 | switch springParameter { 501 | case .interpolation(let mass, let stiffness, let damping): 502 | return .interpolatingSpring( 503 | mass: mass, 504 | stiffness: stiffness, 505 | damping: damping, 506 | initialVelocity: mappedVelocity.dx 507 | ) 508 | } 509 | } 510 | 511 | var animationY: Animation { 512 | switch springParameter { 513 | case .interpolation(let mass, let stiffness, let damping): 514 | return .interpolatingSpring( 515 | mass: mass, 516 | stiffness: stiffness, 517 | damping: damping, 518 | initialVelocity: mappedVelocity.dy 519 | ) 520 | } 521 | } 522 | 523 | if #available(iOS 17.0, *) { 524 | let group = DispatchGroup() 525 | group.enter() 526 | group.enter() 527 | 528 | group.notify(queue: .main) { [handler] in 529 | handler.onCompleteAnimation() 530 | } 531 | 532 | withAnimation(animationX) { 533 | currentOffset.width = targetOffset.width 534 | } completion: { 535 | group.leave() 536 | } 537 | 538 | withAnimation(animationY) { 539 | currentOffset.height = targetOffset.height 540 | } completion: { 541 | group.leave() 542 | } 543 | 544 | } else { 545 | withAnimation( 546 | animationX 547 | ) { 548 | currentOffset.width = targetOffset.width 549 | } 550 | 551 | withAnimation( 552 | animationY 553 | ) { 554 | currentOffset.height = targetOffset.height 555 | } 556 | } 557 | 558 | } 559 | 560 | } 561 | 562 | private enum _CoordinateSpaceTag: Hashable { 563 | case pointInView 564 | case transition 565 | } 566 | 567 | #if DEBUG 568 | 569 | #Preview("Joystick") { 570 | Joystick() 571 | } 572 | 573 | #Preview("SwipeAction") { 574 | SwipeAction() 575 | } 576 | 577 | struct Joystick: View { 578 | 579 | @State var offset: CGSize = .zero 580 | 581 | @State var isOn: Bool = false 582 | 583 | var body: some View { 584 | stick 585 | .padding(10) 586 | } 587 | 588 | private var stick: some View { 589 | 590 | VStack { 591 | 592 | Button("Add offset") { 593 | withAnimation(.interpolatingSpring(mass: 1, stiffness: 1, damping: 1, initialVelocity: 0)) 594 | { 595 | offset.width += 10 596 | } 597 | } 598 | 599 | Circle() 600 | .fill(Color.yellow) 601 | .frame(width: 100, height: 100) 602 | .modifier( 603 | SnapDraggingModifier( 604 | gestureMode: .normal, 605 | offset: $offset, 606 | activation: .init(minimumDistance: 0), 607 | springParameter: .interpolation(mass: 1, stiffness: 1, damping: 1) 608 | ) 609 | ) 610 | Circle() 611 | .fill(Color.green) 612 | .frame(width: 100, height: 100) 613 | 614 | } 615 | .padding(20) 616 | .background(Color.secondary) 617 | .coordinateSpace(name: "A") 618 | 619 | } 620 | } 621 | 622 | struct SwipeAction: View { 623 | 624 | @State var offset: CGSize = .zero 625 | 626 | var body: some View { 627 | 628 | RoundedRectangle(cornerRadius: 16, style: .continuous) 629 | .fill(Color.blue) 630 | .frame(width: nil, height: 50) 631 | .modifier( 632 | SnapDraggingModifier( 633 | gestureMode: .normal, 634 | offset: $offset, 635 | axis: .horizontal, 636 | horizontalBoundary: .init(min: 0, max: .infinity, bandLength: 50), 637 | springParameter: .interpolation(mass: 1, stiffness: 100, damping: 10), 638 | handler: .init(onEndDragging: { velocity, offset, contentSize in 639 | 640 | print(velocity, offset, contentSize) 641 | 642 | if velocity.dx > 50 || offset.width > (contentSize.width / 2) { 643 | print("remove") 644 | return .init(width: contentSize.width, height: 0) 645 | } else { 646 | print("stay") 647 | return .zero 648 | } 649 | }) 650 | ) 651 | ) 652 | .padding(.horizontal, 20) 653 | 654 | } 655 | 656 | } 657 | 658 | #endif 659 | -------------------------------------------------------------------------------- /Tests/SnapDraggingModifierTests/swiftui_snap_dragging_modifierTests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | @testable import SwiftUISnapDraggingModifier 4 | 5 | final class swiftui_snap_dragging_modifierTests: XCTestCase { 6 | func testExample() throws { 7 | } 8 | } 9 | --------------------------------------------------------------------------------