├── .gitignore ├── DraggableGridView.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── xcuserdata │ └── heath.xcuserdatad │ └── xcschemes │ └── xcschememanagement.plist ├── DraggableGridView ├── Assets.xcassets │ ├── AccentColor.colorset │ │ └── Contents.json │ ├── AppIcon.appiconset │ │ └── Contents.json │ ├── Contents.json │ ├── img_twice_50.imageset │ │ ├── Contents.json │ │ └── img_twice_50.png │ ├── img_twice_51.imageset │ │ ├── Contents.json │ │ └── img_twice_51.png │ ├── img_twice_52.imageset │ │ ├── Contents.json │ │ └── img_twice_52.png │ ├── img_twice_53.imageset │ │ ├── Contents.json │ │ └── img_twice_53.png │ ├── img_twice_54.imageset │ │ ├── Contents.json │ │ └── img_twice_54.png │ └── img_twice_55.imageset │ │ ├── Contents.json │ │ └── img_twice_55.png ├── DraggableGridView.swift ├── DraggableGridViewApp.swift ├── Info.plist └── Preview Content │ └── Preview Assets.xcassets │ └── Contents.json ├── LICENSE ├── README.md └── draggableview_small.gif /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## User settings 6 | xcuserdata/ 7 | 8 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 9 | *.xcscmblueprint 10 | *.xccheckout 11 | 12 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 13 | build/ 14 | DerivedData/ 15 | *.moved-aside 16 | *.pbxuser 17 | !default.pbxuser 18 | *.mode1v3 19 | !default.mode1v3 20 | *.mode2v3 21 | !default.mode2v3 22 | *.perspectivev3 23 | !default.perspectivev3 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | 28 | ## App packaging 29 | *.ipa 30 | *.dSYM.zip 31 | *.dSYM 32 | 33 | ## Playgrounds 34 | timeline.xctimeline 35 | playground.xcworkspace 36 | 37 | # Swift Package Manager 38 | # 39 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 40 | # Packages/ 41 | # Package.pins 42 | # Package.resolved 43 | # *.xcodeproj 44 | # 45 | # Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata 46 | # hence it is not needed unless you have added a package configuration file to your project 47 | # .swiftpm 48 | 49 | .build/ 50 | 51 | # CocoaPods 52 | # 53 | # We recommend against adding the Pods directory to your .gitignore. However 54 | # you should judge for yourself, the pros and cons are mentioned at: 55 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 56 | # 57 | # Pods/ 58 | # 59 | # Add this line if you want to avoid checking in source code from the Xcode workspace 60 | # *.xcworkspace 61 | 62 | # Carthage 63 | # 64 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 65 | # Carthage/Checkouts 66 | 67 | Carthage/Build/ 68 | 69 | # Accio dependency management 70 | Dependencies/ 71 | .accio/ 72 | 73 | # fastlane 74 | # 75 | # It is recommended to not store the screenshots in the git repo. 76 | # Instead, use fastlane to re-generate the screenshots whenever they are needed. 77 | # For more information about the recommended setup visit: 78 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 79 | 80 | fastlane/report.xml 81 | fastlane/Preview.html 82 | fastlane/screenshots/**/*.png 83 | fastlane/test_output 84 | 85 | # Code Injection 86 | # 87 | # After new code Injection tools there's a generated folder /iOSInjectionProject 88 | # https://github.com/johnno1962/injectionforxcode 89 | 90 | iOSInjectionProject/ 91 | -------------------------------------------------------------------------------- /DraggableGridView.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 7E7B6BE225D0E7B900AA1078 /* DraggableGridViewApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E7B6BE125D0E7B900AA1078 /* DraggableGridViewApp.swift */; }; 11 | 7E7B6BE625D0E7BA00AA1078 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7E7B6BE525D0E7BA00AA1078 /* Assets.xcassets */; }; 12 | 7E7B6BE925D0E7BA00AA1078 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7E7B6BE825D0E7BA00AA1078 /* Preview Assets.xcassets */; }; 13 | 7E7B6BF225D0E7DD00AA1078 /* DraggableGridView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E7B6BF125D0E7DD00AA1078 /* DraggableGridView.swift */; }; 14 | /* End PBXBuildFile section */ 15 | 16 | /* Begin PBXFileReference section */ 17 | 7E7B6BDE25D0E7B900AA1078 /* DraggableGridView.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DraggableGridView.app; sourceTree = BUILT_PRODUCTS_DIR; }; 18 | 7E7B6BE125D0E7B900AA1078 /* DraggableGridViewApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DraggableGridViewApp.swift; sourceTree = ""; }; 19 | 7E7B6BE525D0E7BA00AA1078 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 20 | 7E7B6BE825D0E7BA00AA1078 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 21 | 7E7B6BEA25D0E7BA00AA1078 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 22 | 7E7B6BF125D0E7DD00AA1078 /* DraggableGridView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DraggableGridView.swift; sourceTree = ""; }; 23 | /* End PBXFileReference section */ 24 | 25 | /* Begin PBXFrameworksBuildPhase section */ 26 | 7E7B6BDB25D0E7B900AA1078 /* Frameworks */ = { 27 | isa = PBXFrameworksBuildPhase; 28 | buildActionMask = 2147483647; 29 | files = ( 30 | ); 31 | runOnlyForDeploymentPostprocessing = 0; 32 | }; 33 | /* End PBXFrameworksBuildPhase section */ 34 | 35 | /* Begin PBXGroup section */ 36 | 7E7B6BD525D0E7B900AA1078 = { 37 | isa = PBXGroup; 38 | children = ( 39 | 7E7B6BE025D0E7B900AA1078 /* DraggableGridView */, 40 | 7E7B6BDF25D0E7B900AA1078 /* Products */, 41 | ); 42 | sourceTree = ""; 43 | }; 44 | 7E7B6BDF25D0E7B900AA1078 /* Products */ = { 45 | isa = PBXGroup; 46 | children = ( 47 | 7E7B6BDE25D0E7B900AA1078 /* DraggableGridView.app */, 48 | ); 49 | name = Products; 50 | sourceTree = ""; 51 | }; 52 | 7E7B6BE025D0E7B900AA1078 /* DraggableGridView */ = { 53 | isa = PBXGroup; 54 | children = ( 55 | 7E7B6BE125D0E7B900AA1078 /* DraggableGridViewApp.swift */, 56 | 7E7B6BF125D0E7DD00AA1078 /* DraggableGridView.swift */, 57 | 7E7B6BE525D0E7BA00AA1078 /* Assets.xcassets */, 58 | 7E7B6BEA25D0E7BA00AA1078 /* Info.plist */, 59 | 7E7B6BE725D0E7BA00AA1078 /* Preview Content */, 60 | ); 61 | path = DraggableGridView; 62 | sourceTree = ""; 63 | }; 64 | 7E7B6BE725D0E7BA00AA1078 /* Preview Content */ = { 65 | isa = PBXGroup; 66 | children = ( 67 | 7E7B6BE825D0E7BA00AA1078 /* Preview Assets.xcassets */, 68 | ); 69 | path = "Preview Content"; 70 | sourceTree = ""; 71 | }; 72 | /* End PBXGroup section */ 73 | 74 | /* Begin PBXNativeTarget section */ 75 | 7E7B6BDD25D0E7B900AA1078 /* DraggableGridView */ = { 76 | isa = PBXNativeTarget; 77 | buildConfigurationList = 7E7B6BED25D0E7BA00AA1078 /* Build configuration list for PBXNativeTarget "DraggableGridView" */; 78 | buildPhases = ( 79 | 7E7B6BDA25D0E7B900AA1078 /* Sources */, 80 | 7E7B6BDB25D0E7B900AA1078 /* Frameworks */, 81 | 7E7B6BDC25D0E7B900AA1078 /* Resources */, 82 | ); 83 | buildRules = ( 84 | ); 85 | dependencies = ( 86 | ); 87 | name = DraggableGridView; 88 | productName = DraggableGridView; 89 | productReference = 7E7B6BDE25D0E7B900AA1078 /* DraggableGridView.app */; 90 | productType = "com.apple.product-type.application"; 91 | }; 92 | /* End PBXNativeTarget section */ 93 | 94 | /* Begin PBXProject section */ 95 | 7E7B6BD625D0E7B900AA1078 /* Project object */ = { 96 | isa = PBXProject; 97 | attributes = { 98 | LastSwiftUpdateCheck = 1230; 99 | LastUpgradeCheck = 1230; 100 | TargetAttributes = { 101 | 7E7B6BDD25D0E7B900AA1078 = { 102 | CreatedOnToolsVersion = 12.3; 103 | }; 104 | }; 105 | }; 106 | buildConfigurationList = 7E7B6BD925D0E7B900AA1078 /* Build configuration list for PBXProject "DraggableGridView" */; 107 | compatibilityVersion = "Xcode 9.3"; 108 | developmentRegion = en; 109 | hasScannedForEncodings = 0; 110 | knownRegions = ( 111 | en, 112 | Base, 113 | ); 114 | mainGroup = 7E7B6BD525D0E7B900AA1078; 115 | productRefGroup = 7E7B6BDF25D0E7B900AA1078 /* Products */; 116 | projectDirPath = ""; 117 | projectRoot = ""; 118 | targets = ( 119 | 7E7B6BDD25D0E7B900AA1078 /* DraggableGridView */, 120 | ); 121 | }; 122 | /* End PBXProject section */ 123 | 124 | /* Begin PBXResourcesBuildPhase section */ 125 | 7E7B6BDC25D0E7B900AA1078 /* Resources */ = { 126 | isa = PBXResourcesBuildPhase; 127 | buildActionMask = 2147483647; 128 | files = ( 129 | 7E7B6BE925D0E7BA00AA1078 /* Preview Assets.xcassets in Resources */, 130 | 7E7B6BE625D0E7BA00AA1078 /* Assets.xcassets in Resources */, 131 | ); 132 | runOnlyForDeploymentPostprocessing = 0; 133 | }; 134 | /* End PBXResourcesBuildPhase section */ 135 | 136 | /* Begin PBXSourcesBuildPhase section */ 137 | 7E7B6BDA25D0E7B900AA1078 /* Sources */ = { 138 | isa = PBXSourcesBuildPhase; 139 | buildActionMask = 2147483647; 140 | files = ( 141 | 7E7B6BF225D0E7DD00AA1078 /* DraggableGridView.swift in Sources */, 142 | 7E7B6BE225D0E7B900AA1078 /* DraggableGridViewApp.swift in Sources */, 143 | ); 144 | runOnlyForDeploymentPostprocessing = 0; 145 | }; 146 | /* End PBXSourcesBuildPhase section */ 147 | 148 | /* Begin XCBuildConfiguration section */ 149 | 7E7B6BEB25D0E7BA00AA1078 /* Debug */ = { 150 | isa = XCBuildConfiguration; 151 | buildSettings = { 152 | ALWAYS_SEARCH_USER_PATHS = NO; 153 | CLANG_ANALYZER_NONNULL = YES; 154 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 155 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 156 | CLANG_CXX_LIBRARY = "libc++"; 157 | CLANG_ENABLE_MODULES = YES; 158 | CLANG_ENABLE_OBJC_ARC = YES; 159 | CLANG_ENABLE_OBJC_WEAK = YES; 160 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 161 | CLANG_WARN_BOOL_CONVERSION = YES; 162 | CLANG_WARN_COMMA = YES; 163 | CLANG_WARN_CONSTANT_CONVERSION = YES; 164 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 165 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 166 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 167 | CLANG_WARN_EMPTY_BODY = YES; 168 | CLANG_WARN_ENUM_CONVERSION = YES; 169 | CLANG_WARN_INFINITE_RECURSION = YES; 170 | CLANG_WARN_INT_CONVERSION = YES; 171 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 172 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 173 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 174 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 175 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 176 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 177 | CLANG_WARN_STRICT_PROTOTYPES = YES; 178 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 179 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 180 | CLANG_WARN_UNREACHABLE_CODE = YES; 181 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 182 | COPY_PHASE_STRIP = NO; 183 | DEBUG_INFORMATION_FORMAT = dwarf; 184 | ENABLE_STRICT_OBJC_MSGSEND = YES; 185 | ENABLE_TESTABILITY = YES; 186 | GCC_C_LANGUAGE_STANDARD = gnu11; 187 | GCC_DYNAMIC_NO_PIC = NO; 188 | GCC_NO_COMMON_BLOCKS = YES; 189 | GCC_OPTIMIZATION_LEVEL = 0; 190 | GCC_PREPROCESSOR_DEFINITIONS = ( 191 | "DEBUG=1", 192 | "$(inherited)", 193 | ); 194 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 195 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 196 | GCC_WARN_UNDECLARED_SELECTOR = YES; 197 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 198 | GCC_WARN_UNUSED_FUNCTION = YES; 199 | GCC_WARN_UNUSED_VARIABLE = YES; 200 | IPHONEOS_DEPLOYMENT_TARGET = 13.1; 201 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 202 | MTL_FAST_MATH = YES; 203 | ONLY_ACTIVE_ARCH = YES; 204 | SDKROOT = iphoneos; 205 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 206 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 207 | }; 208 | name = Debug; 209 | }; 210 | 7E7B6BEC25D0E7BA00AA1078 /* Release */ = { 211 | isa = XCBuildConfiguration; 212 | buildSettings = { 213 | ALWAYS_SEARCH_USER_PATHS = NO; 214 | CLANG_ANALYZER_NONNULL = YES; 215 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 216 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 217 | CLANG_CXX_LIBRARY = "libc++"; 218 | CLANG_ENABLE_MODULES = YES; 219 | CLANG_ENABLE_OBJC_ARC = YES; 220 | CLANG_ENABLE_OBJC_WEAK = YES; 221 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 222 | CLANG_WARN_BOOL_CONVERSION = YES; 223 | CLANG_WARN_COMMA = YES; 224 | CLANG_WARN_CONSTANT_CONVERSION = YES; 225 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 226 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 227 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 228 | CLANG_WARN_EMPTY_BODY = YES; 229 | CLANG_WARN_ENUM_CONVERSION = YES; 230 | CLANG_WARN_INFINITE_RECURSION = YES; 231 | CLANG_WARN_INT_CONVERSION = YES; 232 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 233 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 234 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 235 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 236 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 237 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 238 | CLANG_WARN_STRICT_PROTOTYPES = YES; 239 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 240 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 241 | CLANG_WARN_UNREACHABLE_CODE = YES; 242 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 243 | COPY_PHASE_STRIP = NO; 244 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 245 | ENABLE_NS_ASSERTIONS = NO; 246 | ENABLE_STRICT_OBJC_MSGSEND = YES; 247 | GCC_C_LANGUAGE_STANDARD = gnu11; 248 | GCC_NO_COMMON_BLOCKS = YES; 249 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 250 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 251 | GCC_WARN_UNDECLARED_SELECTOR = YES; 252 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 253 | GCC_WARN_UNUSED_FUNCTION = YES; 254 | GCC_WARN_UNUSED_VARIABLE = YES; 255 | IPHONEOS_DEPLOYMENT_TARGET = 13.1; 256 | MTL_ENABLE_DEBUG_INFO = NO; 257 | MTL_FAST_MATH = YES; 258 | SDKROOT = iphoneos; 259 | SWIFT_COMPILATION_MODE = wholemodule; 260 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 261 | VALIDATE_PRODUCT = YES; 262 | }; 263 | name = Release; 264 | }; 265 | 7E7B6BEE25D0E7BA00AA1078 /* Debug */ = { 266 | isa = XCBuildConfiguration; 267 | buildSettings = { 268 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 269 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 270 | CODE_SIGN_STYLE = Automatic; 271 | DEVELOPMENT_ASSET_PATHS = "\"DraggableGridView/Preview Content\""; 272 | DEVELOPMENT_TEAM = 7BB9FPDVDN; 273 | ENABLE_PREVIEWS = YES; 274 | INFOPLIST_FILE = DraggableGridView/Info.plist; 275 | IPHONEOS_DEPLOYMENT_TARGET = 14.0; 276 | LD_RUNPATH_SEARCH_PATHS = ( 277 | "$(inherited)", 278 | "@executable_path/Frameworks", 279 | ); 280 | PRODUCT_BUNDLE_IDENTIFIER = com.fullc0de.DraggableGridView; 281 | PRODUCT_NAME = "$(TARGET_NAME)"; 282 | SWIFT_VERSION = 5.0; 283 | TARGETED_DEVICE_FAMILY = "1,2"; 284 | }; 285 | name = Debug; 286 | }; 287 | 7E7B6BEF25D0E7BA00AA1078 /* Release */ = { 288 | isa = XCBuildConfiguration; 289 | buildSettings = { 290 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 291 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 292 | CODE_SIGN_STYLE = Automatic; 293 | DEVELOPMENT_ASSET_PATHS = "\"DraggableGridView/Preview Content\""; 294 | DEVELOPMENT_TEAM = 7BB9FPDVDN; 295 | ENABLE_PREVIEWS = YES; 296 | INFOPLIST_FILE = DraggableGridView/Info.plist; 297 | IPHONEOS_DEPLOYMENT_TARGET = 14.0; 298 | LD_RUNPATH_SEARCH_PATHS = ( 299 | "$(inherited)", 300 | "@executable_path/Frameworks", 301 | ); 302 | PRODUCT_BUNDLE_IDENTIFIER = com.fullc0de.DraggableGridView; 303 | PRODUCT_NAME = "$(TARGET_NAME)"; 304 | SWIFT_VERSION = 5.0; 305 | TARGETED_DEVICE_FAMILY = "1,2"; 306 | }; 307 | name = Release; 308 | }; 309 | /* End XCBuildConfiguration section */ 310 | 311 | /* Begin XCConfigurationList section */ 312 | 7E7B6BD925D0E7B900AA1078 /* Build configuration list for PBXProject "DraggableGridView" */ = { 313 | isa = XCConfigurationList; 314 | buildConfigurations = ( 315 | 7E7B6BEB25D0E7BA00AA1078 /* Debug */, 316 | 7E7B6BEC25D0E7BA00AA1078 /* Release */, 317 | ); 318 | defaultConfigurationIsVisible = 0; 319 | defaultConfigurationName = Release; 320 | }; 321 | 7E7B6BED25D0E7BA00AA1078 /* Build configuration list for PBXNativeTarget "DraggableGridView" */ = { 322 | isa = XCConfigurationList; 323 | buildConfigurations = ( 324 | 7E7B6BEE25D0E7BA00AA1078 /* Debug */, 325 | 7E7B6BEF25D0E7BA00AA1078 /* Release */, 326 | ); 327 | defaultConfigurationIsVisible = 0; 328 | defaultConfigurationName = Release; 329 | }; 330 | /* End XCConfigurationList section */ 331 | }; 332 | rootObject = 7E7B6BD625D0E7B900AA1078 /* Project object */; 333 | } 334 | -------------------------------------------------------------------------------- /DraggableGridView.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DraggableGridView.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /DraggableGridView.xcodeproj/xcuserdata/heath.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | DraggableGridView.xcscheme_^#shared#^_ 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /DraggableGridView/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 | -------------------------------------------------------------------------------- /DraggableGridView/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "scale" : "2x", 6 | "size" : "20x20" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "scale" : "3x", 11 | "size" : "20x20" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "scale" : "2x", 16 | "size" : "29x29" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "scale" : "3x", 21 | "size" : "29x29" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "scale" : "2x", 26 | "size" : "40x40" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "scale" : "3x", 31 | "size" : "40x40" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "scale" : "2x", 36 | "size" : "60x60" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "scale" : "3x", 41 | "size" : "60x60" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "scale" : "1x", 46 | "size" : "20x20" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "scale" : "2x", 51 | "size" : "20x20" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "scale" : "1x", 56 | "size" : "29x29" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "scale" : "2x", 61 | "size" : "29x29" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "scale" : "1x", 66 | "size" : "40x40" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "scale" : "2x", 71 | "size" : "40x40" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "scale" : "1x", 76 | "size" : "76x76" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "scale" : "2x", 81 | "size" : "76x76" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "scale" : "2x", 86 | "size" : "83.5x83.5" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "scale" : "1x", 91 | "size" : "1024x1024" 92 | } 93 | ], 94 | "info" : { 95 | "author" : "xcode", 96 | "version" : 1 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /DraggableGridView/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /DraggableGridView/Assets.xcassets/img_twice_50.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "filename" : "img_twice_50.png", 5 | "idiom" : "universal", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "author" : "xcode", 19 | "version" : 1 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /DraggableGridView/Assets.xcassets/img_twice_50.imageset/img_twice_50.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fullc0de/DraggableGridView/8e0f371d66ff1cd7af80b6d458b3ea20e22e7d30/DraggableGridView/Assets.xcassets/img_twice_50.imageset/img_twice_50.png -------------------------------------------------------------------------------- /DraggableGridView/Assets.xcassets/img_twice_51.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "filename" : "img_twice_51.png", 5 | "idiom" : "universal", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "author" : "xcode", 19 | "version" : 1 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /DraggableGridView/Assets.xcassets/img_twice_51.imageset/img_twice_51.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fullc0de/DraggableGridView/8e0f371d66ff1cd7af80b6d458b3ea20e22e7d30/DraggableGridView/Assets.xcassets/img_twice_51.imageset/img_twice_51.png -------------------------------------------------------------------------------- /DraggableGridView/Assets.xcassets/img_twice_52.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "filename" : "img_twice_52.png", 5 | "idiom" : "universal", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "author" : "xcode", 19 | "version" : 1 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /DraggableGridView/Assets.xcassets/img_twice_52.imageset/img_twice_52.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fullc0de/DraggableGridView/8e0f371d66ff1cd7af80b6d458b3ea20e22e7d30/DraggableGridView/Assets.xcassets/img_twice_52.imageset/img_twice_52.png -------------------------------------------------------------------------------- /DraggableGridView/Assets.xcassets/img_twice_53.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "filename" : "img_twice_53.png", 5 | "idiom" : "universal", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "author" : "xcode", 19 | "version" : 1 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /DraggableGridView/Assets.xcassets/img_twice_53.imageset/img_twice_53.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fullc0de/DraggableGridView/8e0f371d66ff1cd7af80b6d458b3ea20e22e7d30/DraggableGridView/Assets.xcassets/img_twice_53.imageset/img_twice_53.png -------------------------------------------------------------------------------- /DraggableGridView/Assets.xcassets/img_twice_54.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "filename" : "img_twice_54.png", 5 | "idiom" : "universal", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "author" : "xcode", 19 | "version" : 1 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /DraggableGridView/Assets.xcassets/img_twice_54.imageset/img_twice_54.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fullc0de/DraggableGridView/8e0f371d66ff1cd7af80b6d458b3ea20e22e7d30/DraggableGridView/Assets.xcassets/img_twice_54.imageset/img_twice_54.png -------------------------------------------------------------------------------- /DraggableGridView/Assets.xcassets/img_twice_55.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "filename" : "img_twice_55.png", 5 | "idiom" : "universal", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "author" : "xcode", 19 | "version" : 1 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /DraggableGridView/Assets.xcassets/img_twice_55.imageset/img_twice_55.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fullc0de/DraggableGridView/8e0f371d66ff1cd7af80b6d458b3ea20e22e7d30/DraggableGridView/Assets.xcassets/img_twice_55.imageset/img_twice_55.png -------------------------------------------------------------------------------- /DraggableGridView/DraggableGridView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DraggableGridView.swift 3 | // DraggableGridView 4 | // 5 | // Created by Heath Hwang on 2/8/21. 6 | // 7 | 8 | import SwiftUI 9 | 10 | struct DraggableGridViewConfiguration { 11 | 12 | /// the number of column in a row 13 | var column: Int 14 | 15 | /// a space between items in horizontal 16 | var hSpace: CGFloat = 10.0 17 | 18 | /// a space between items in vertical 19 | var vSpace: CGFloat = 10.0 20 | 21 | /// a H/W ratio of each cell 22 | var cellRatio: CGFloat = 1.0 23 | 24 | /// This indicates whether to turn on dragging or not. 25 | var draggable: Bool = true 26 | 27 | /// a minimum time to press any item to trigger dragging. 28 | var pressDuration: Double = 0.3 29 | 30 | /// a scale factor to apply to a picked item on dragging. (experiment) 31 | var scaleOnPress: CGFloat = 1.0 32 | 33 | /// This calculates the width of an item cell based on the width of its container. 34 | /// - Parameter width: The width of its container. 35 | /// - Returns: The width of an item cell 36 | func cellWidth(width: CGFloat) -> CGFloat { 37 | return (width - (self.hSpace * CGFloat(self.column - 1))) / CGFloat(self.column) 38 | } 39 | 40 | /// This calculates the width of an item cell based on the width of its container and `cellRatio`. 41 | /// - Parameter width: The width of its container. 42 | /// - Returns: The width of an item cell 43 | func cellHeight(width: CGFloat) -> CGFloat { 44 | return self.cellWidth(width: width) * self.cellRatio 45 | } 46 | } 47 | 48 | struct DraggableGridView: View { 49 | @State private var viewBounds: CGRect = .zero 50 | @State private var itemBoundsData: [ItemBoundsPreferencesData] = [] 51 | @State private var expectedIndexes: [Item.ID: Int] 52 | 53 | @Binding var items: [Item] 54 | 55 | let config: DraggableGridViewConfiguration 56 | let content: (Item, Int, CGSize) -> Content 57 | 58 | @State private var dragState: DraggableGridViewDragState = .inactive 59 | /// This indicates maximum containable bounds that items can be contained depending on the current view bounds. 60 | /// The number of it will be equal or greater than the number of items. 61 | /// e.g) If config.column = `3`, the number of items = `4`, then the number of it is `6` 62 | @State private var availableItemBounds: [CGRect] = [] 63 | 64 | fileprivate var onDragged: ((Int, Int) -> Void)? = nil 65 | 66 | private let hapticFeedback = UIImpactFeedbackGenerator(style: .rigid) 67 | 68 | init(_ item: Binding<[Item]>, 69 | config: DraggableGridViewConfiguration = DraggableGridViewConfiguration(column: 3), 70 | @ViewBuilder content: @escaping (_ item: Item, _ index: Int, _ cellSize: CGSize) -> Content) { 71 | self._items = item 72 | self.config = config 73 | self.content = content 74 | var temp: [Item.ID: Int] = [:] 75 | item.wrappedValue.enumerated().forEach { 76 | temp[$0.element.id] = $0.offset 77 | } 78 | self._expectedIndexes = State(initialValue: temp) 79 | } 80 | 81 | var body: some View { 82 | GeometryReader { geo in 83 | ZStack(alignment: .topLeading) { 84 | // This fixer keeps ZStack's bounds still regardless of changing each item's position in it. 85 | self.alignFixer 86 | 87 | ForEach(self.items) { item in 88 | self.content(item, self.items.firstIndex(where: { $0.id == item.id })!, self.cellSize(geo)) 89 | .frame(width: self.cellSize(geo).width, height: self.cellSize(geo).height) 90 | //.border(Color.purple, width: 2) 91 | .zIndex(self.isDragging(id: item.id) ? 1 : 0) 92 | .alignmentGuide(.leading) { dimension in 93 | -(dimension.width + self.config.hSpace) * CGFloat(self.expectedIndexOf(dataId: item.id) % self.config.column) 94 | } 95 | .alignmentGuide(.top) { dimension in 96 | -(dimension.height + self.config.vSpace) * CGFloat(self.expectedIndexOf(dataId: item.id) / self.config.column) 97 | } 98 | .scaleEffect(isDragging(id: item.id) ? config.scaleOnPress : 1.0) 99 | .offset(x: self.isDragging(id: item.id) ? self.dragState.translation.width : 0, 100 | y: self.isDragging(id: item.id) ? self.dragState.translation.height : 0) 101 | .anchorPreference(key: ItemBoundsPreferencesKey.self, 102 | value: .bounds, 103 | transform: { [ItemBoundsPreferencesData(id: item.id, bound: geo[$0])] }) 104 | .gesture(LongPressGesture(minimumDuration: self.config.pressDuration) 105 | .sequenced(before: DragGesture()) 106 | .onChanged { value in 107 | switch value { 108 | case .second(true, let drag): 109 | if let drag = drag { 110 | var draggedRect = self.itemBoundsData.first { $0.id == item.id as AnyHashable }?.bound ?? .zero 111 | draggedRect.origin.x += drag.translation.width 112 | draggedRect.origin.y += drag.translation.height 113 | 114 | var newState: DraggableGridViewDragState! 115 | if self.dragState.translation == .zero { 116 | newState = .dragging(id: item.id, translation: drag.translation, delta: .zero, draggedRect: draggedRect) 117 | } else { 118 | let deltaX = drag.translation.width - self.dragState.translation.width 119 | let deltaY = drag.translation.height - self.dragState.translation.height 120 | newState = .dragging(id: item.id, translation: drag.translation, delta: CGPoint(x: deltaX, y: deltaY), draggedRect: draggedRect) 121 | } 122 | 123 | withAnimation(.easeInOut(duration: 0.2)) { 124 | self.dragState = newState 125 | } 126 | } else { 127 | self.hapticFeedback.impactOccurred() 128 | withAnimation(.easeInOut(duration: 0.2)) { 129 | self.dragState = .dragging(id: item.id, translation: .zero, delta: .zero, draggedRect: .zero) 130 | } 131 | } 132 | default: 133 | break 134 | //self.dragState = .inactive 135 | } 136 | } 137 | .onEnded { value in 138 | switch value { 139 | case .second(true, let drag): 140 | if drag != nil { 141 | let currentIndex = self.actualIndexOf(dataId: self.dragState.id!) 142 | let newIndex = self.expectedIndexOfDraggedItem() 143 | 144 | self.items.insert(self.items.remove(at: currentIndex), at: newIndex) 145 | 146 | let dstRect = self.availableItemBounds[newIndex] 147 | let draggedRect = self.dragState.draggedRect 148 | 149 | let remainedDistanceX = draggedRect.origin.x - dstRect.origin.x 150 | let remainedDistanceY = draggedRect.origin.y - dstRect.origin.y 151 | self.dragState = .dragging(id: item.id, translation: CGSize(width: remainedDistanceX, height: remainedDistanceY), delta: .zero, draggedRect: draggedRect) 152 | 153 | self.onDragged?(currentIndex, newIndex) 154 | 155 | withAnimation(.easeInOut(duration: 0.15)) { 156 | self.dragState = .inactive 157 | } 158 | } 159 | default: 160 | break 161 | } 162 | }, including: self.config.draggable ? .all : .none) 163 | } 164 | } 165 | .frame(width: geo.size.width, alignment: .topLeading) 166 | .background(DragViewBoundPreferenceSetter()) 167 | .onPreferenceChange(ItemBoundsPreferencesKey.self) { 168 | self.itemBoundsData = $0 169 | } 170 | // // for debugging 171 | // .overlayPreferenceValue(ItemBoundsPreferencesKey.self) { bounds in 172 | // GeometryReader { geometry in 173 | // ZStack(alignment: .topLeading) { 174 | // ForEach(0.. 1.0 { 204 | print("bounds = \($0.bounds)") 205 | self.viewBounds = $0.bounds 206 | self.updateFixedBounds() 207 | } 208 | } 209 | } 210 | .frame(height: viewBounds.size.height) 211 | // .overlay( 212 | // Color.clear 213 | // .border(Color.yellow, width: 2.0) 214 | // .frame(width: viewBounds.size.width, height: viewBounds.size.height) 215 | // ) 216 | .onAppear{ 217 | self.hapticFeedback.prepare() 218 | } 219 | } 220 | 221 | // MARK: - Private Methods 222 | private func cellSize(_ gp: GeometryProxy) -> CGSize { 223 | CGSize(width: config.cellWidth(width: gp.size.width), height: config.cellHeight(width: gp.size.width)) 224 | } 225 | 226 | var alignFixer: some View { 227 | Color.clear 228 | .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity) 229 | } 230 | 231 | private func actualIndexOf(dataId: Item.ID) -> Int { 232 | return items.firstIndex(where: { $0.id == dataId })! 233 | } 234 | 235 | private func expectedIndexOf(dataId: Item.ID) -> Int { 236 | let index = actualIndexOf(dataId: dataId) 237 | if let draggedId = dragState.id { 238 | let draggedSrcIndex = actualIndexOf(dataId: draggedId) 239 | let draggedDstIndex = expectedIndexOfDraggedItem() 240 | if index == draggedSrcIndex { 241 | return index 242 | } else { 243 | if draggedSrcIndex == draggedDstIndex { 244 | return index 245 | } else if draggedSrcIndex > draggedDstIndex { 246 | if index >= draggedDstIndex, index < draggedSrcIndex { 247 | return index + 1 248 | } else { 249 | return index 250 | } 251 | } else { 252 | if index > draggedSrcIndex, index <= draggedDstIndex { 253 | return index - 1 254 | } else { 255 | return index 256 | } 257 | } 258 | } 259 | } else { 260 | return index 261 | } 262 | } 263 | 264 | private func isDragging(id: Item.ID) -> Bool { 265 | switch dragState { 266 | case .inactive: 267 | return false 268 | case .dragging(let draggedId, _, _, _): 269 | return draggedId == id 270 | } 271 | } 272 | 273 | private func expectedIndexOfDraggedItem() -> Int { 274 | let foundIndex = Array(0.. 0.7 279 | } 280 | if let index = foundIndex { 281 | return index 282 | } else if let id = dragState.id { 283 | return actualIndexOf(dataId: id) 284 | } else { 285 | return 0 286 | } 287 | } 288 | 289 | private func updateFixedBounds() { 290 | let viewWidth = self.viewBounds.size.width 291 | let cellWidth = self.config.cellWidth(width: viewWidth) 292 | let cellHeight = self.config.cellHeight(width: viewWidth) 293 | let rowCount = (items.count / config.column) + 1 294 | 295 | 296 | self.availableItemBounds = Array(0..<(rowCount * config.column)).map { 297 | CGRect(x: (cellWidth + self.config.hSpace) * CGFloat($0 % self.config.column), 298 | y: (cellHeight + self.config.vSpace) * CGFloat($0 / self.config.column), 299 | width: cellWidth, 300 | height: cellHeight) 301 | } 302 | } 303 | } 304 | 305 | extension DraggableGridView { 306 | public func onDrag(perform action: ((Int, Int) -> Void)? = nil) -> DraggableGridView { 307 | var view = self 308 | view.onDragged = action 309 | return view 310 | } 311 | } 312 | 313 | extension CGRect { 314 | fileprivate func overlappedRate(overlappingRect: CGRect) -> CGFloat { 315 | let volume = self.width * self.height 316 | let intersect = self.intersection(overlappingRect) 317 | if intersect.isNull { 318 | return 0.0 319 | } 320 | return (intersect.width * intersect.height) / volume 321 | } 322 | } 323 | 324 | enum DraggableGridViewDragState { 325 | case inactive 326 | case dragging(id: ID, translation: CGSize, delta: CGPoint, draggedRect: CGRect) 327 | 328 | var isDragging: Bool { 329 | switch self { 330 | case .inactive: 331 | return false 332 | case .dragging: 333 | return true 334 | } 335 | } 336 | 337 | var id: ID? { 338 | switch self { 339 | case .inactive: 340 | return nil 341 | case .dragging(let id, _, _, _): 342 | return id 343 | } 344 | } 345 | 346 | var translation: CGSize { 347 | switch self { 348 | case .inactive: 349 | return .zero 350 | case .dragging(_, let translation, _, _): 351 | return translation 352 | } 353 | } 354 | 355 | var delta: CGPoint { 356 | switch self { 357 | case .inactive: 358 | return .zero 359 | case .dragging(_, _, let delta, _): 360 | return delta 361 | } 362 | } 363 | 364 | var draggedRect: CGRect { 365 | switch self { 366 | case .inactive: 367 | return .zero 368 | case .dragging(_, _, _, let rect): 369 | return rect 370 | } 371 | } 372 | } 373 | 374 | private struct ItemBoundsPreferencesData: Identifiable, Equatable { 375 | let id: AnyHashable 376 | let bound: CGRect 377 | } 378 | 379 | private struct ItemBoundsPreferencesKey: PreferenceKey { 380 | public static var defaultValue: [ItemBoundsPreferencesData] = [] 381 | 382 | public static func reduce(value: inout [ItemBoundsPreferencesData], nextValue: () -> [ItemBoundsPreferencesData]) { 383 | value.append(contentsOf: nextValue()) 384 | } 385 | } 386 | 387 | private struct BoundsData: Equatable { 388 | var bounds: CGRect = .zero 389 | 390 | static func ==(lhs: BoundsData, rhs: BoundsData) -> Bool { 391 | return lhs.bounds == rhs.bounds 392 | } 393 | } 394 | 395 | private struct DragViewBoundPreferenceKey: PreferenceKey { 396 | static let defaultValue: BoundsData = BoundsData() 397 | 398 | static func reduce(value: inout BoundsData, nextValue: () -> BoundsData) { 399 | let data = nextValue() 400 | var newBounds = value.bounds 401 | newBounds.origin.x += data.bounds.origin.x 402 | newBounds.origin.y += data.bounds.origin.y 403 | newBounds.size.width += data.bounds.size.width 404 | newBounds.size.height += data.bounds.size.height 405 | value.bounds = newBounds 406 | } 407 | } 408 | 409 | private struct DragViewBoundPreferenceSetter: View { 410 | var body: some View { 411 | GeometryReader { geometry in 412 | return Color.clear.anchorPreference(key: DragViewBoundPreferenceKey.self, value: .bounds, transform: { BoundsData(bounds: geometry[$0]) }) 413 | } 414 | } 415 | } 416 | 417 | private struct OverlayView: View { 418 | private let image: UIImage 419 | 420 | init(image: UIImage) { 421 | self.image = image 422 | } 423 | 424 | var body: some View { 425 | Image(uiImage: image) 426 | .resizable() 427 | .aspectRatio(contentMode: .fill) 428 | } 429 | } 430 | 431 | struct TestImageItem: Identifiable { 432 | let id: Int 433 | let image: UIImage 434 | } 435 | 436 | struct PreviewBaseView: View { 437 | @State var images: [TestImageItem] = [ 438 | TestImageItem(id: 0, image: #imageLiteral(resourceName: "img_twice_51")), 439 | TestImageItem(id: 1, image: #imageLiteral(resourceName: "img_twice_52")), 440 | TestImageItem(id: 2, image: #imageLiteral(resourceName: "img_twice_53")), 441 | TestImageItem(id: 3, image: #imageLiteral(resourceName: "img_twice_54")), 442 | TestImageItem(id: 4, image: #imageLiteral(resourceName: "img_twice_55")), 443 | TestImageItem(id: 5, image: #imageLiteral(resourceName: "img_twice_50")) 444 | ] 445 | @State var selectedIndex: Int? = nil 446 | 447 | private var gridConfig: DraggableGridViewConfiguration { 448 | DraggableGridViewConfiguration(column: 3, hSpace: 10, vSpace: 10) 449 | } 450 | 451 | var body: some View { 452 | VStack { 453 | DraggableGridView($images, config: gridConfig) { item, index, size in 454 | OverlayView(image: item.image) 455 | .frame(width: size.width, height: size.height) 456 | .clipped() 457 | } 458 | .padding(.bottom, 40) 459 | Text("grid count = \(images.count)") 460 | Text("order(id) = \(images.map { "\($0.id)" }.joined(separator: ","))") 461 | } 462 | } 463 | } 464 | 465 | struct ButtonGridView_Previews: PreviewProvider { 466 | 467 | static var previews: some View { 468 | PreviewBaseView() 469 | } 470 | } 471 | 472 | -------------------------------------------------------------------------------- /DraggableGridView/DraggableGridViewApp.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DraggableGridViewApp.swift 3 | // DraggableGridView 4 | // 5 | // Created by Heath Hwang on 2/8/21. 6 | // 7 | 8 | import SwiftUI 9 | 10 | @main 11 | struct DraggableGridViewApp: App { 12 | var body: some Scene { 13 | WindowGroup { 14 | PreviewBaseView() 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /DraggableGridView/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 | $(PRODUCT_BUNDLE_PACKAGE_TYPE) 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UIApplicationSceneManifest 24 | 25 | UIApplicationSupportsMultipleScenes 26 | 27 | 28 | UIApplicationSupportsIndirectInputEvents 29 | 30 | UILaunchScreen 31 | 32 | UIRequiredDeviceCapabilities 33 | 34 | armv7 35 | 36 | UISupportedInterfaceOrientations 37 | 38 | UIInterfaceOrientationPortrait 39 | UIInterfaceOrientationLandscapeLeft 40 | UIInterfaceOrientationLandscapeRight 41 | 42 | UISupportedInterfaceOrientations~ipad 43 | 44 | UIInterfaceOrientationPortrait 45 | UIInterfaceOrientationPortraitUpsideDown 46 | UIInterfaceOrientationLandscapeLeft 47 | UIInterfaceOrientationLandscapeRight 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /DraggableGridView/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Heath Hwang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DraggableGridView 2 | A grid style collection view written in SwiftUI that supports to change a position of any item on it by dragging. 3 | 4 | ![](https://github.com/fullc0de/DraggableGridView/blob/main/draggableview_small.gif) 5 | 6 | ## Requirements 7 | SwiftUI 8 | iOS 13.1 or higher 9 | 10 | ## How to Use 11 | `DraggableGridView.swift` involves a test view named `PreviewBaseView` to make it testable on Preview. 12 | I am sure that it's enough for the test view to show how to use the grid view for now. 13 | 14 | ## Install 15 | I haven't made this project support any dependency manager because this is just for sharing my idea how to create this kind of collection view. 16 | If you, therefore, would like to use this view on your project, just simply copy `DraggableGridView.swift` and put it into your project. Or you may be able to make a PR for your preferred manager. 17 | -------------------------------------------------------------------------------- /draggableview_small.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fullc0de/DraggableGridView/8e0f371d66ff1cd7af80b6d458b3ea20e22e7d30/draggableview_small.gif --------------------------------------------------------------------------------