├── IMG_0799.PNG ├── IMG_0800.PNG ├── README.md ├── SFZoomableCanvas.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── xcuserdata │ └── chenwanfei.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── SFZoomableCanvas.xcscheme │ └── xcschememanagement.plist └── SFZoomableCanvas ├── AppDelegate.swift ├── Assets.xcassets ├── AppIcon.appiconset │ ├── Contents.json │ └── appicon@2x.png ├── Contents.json ├── action.imageset │ ├── Contents.json │ ├── action@2x.png │ └── action@3x.png ├── circle_icon.imageset │ ├── Contents.json │ ├── circle_icon@2x.png │ └── circle_icon@3x.png ├── colormap.imageset │ ├── Contents.json │ └── colormap.png ├── delete_icon.imageset │ ├── Contents.json │ ├── delete_icon@2x.png │ └── delete_icon@3x.png ├── drawing_tools.imageset │ ├── Contents.json │ ├── drawing_tools@2x.png │ └── drawing_tools@3x.png ├── drawing_tools_selected.imageset │ ├── Contents.json │ ├── drawing_tools_selected@2x.png │ └── drawing_tools_selected@3x.png ├── finger.imageset │ ├── Contents.json │ ├── finger@2x.png │ └── finger@3x.png ├── finger_selected.imageset │ ├── Contents.json │ ├── finger_selected@2x.png │ └── finger_selected@3x.png ├── redo.imageset │ ├── Contents.json │ ├── redo@2x.png │ └── redo@3x.png └── undo.imageset │ ├── Contents.json │ ├── undo@2x.png │ └── undo@3x.png ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Info.plist ├── SFCanvasView.swift ├── SFColorPickerView.swift ├── SFGradientView.swift ├── SFModels.swift ├── SFSettingVC.swift ├── SFZoomableCanvasVC.swift └── UIImage+SF.swift /IMG_0799.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/IMG_0799.PNG -------------------------------------------------------------------------------- /IMG_0800.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/IMG_0800.PNG -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #SFZoomableCanvas 2 | 3 | 4 | This project illustrates how to implement a zoomable drawing canvas on iOS 5 | 6 | ![ScreenShot](https://raw.github.com/JagieChen/SFZoomableCanvas/master/IMG_0799.PNG) 7 | 8 | 9 | ![ScreenShot](https://raw.github.com/JagieChen/SFZoomableCanvas/master/IMG_0800.PNG) 10 | 11 | ## Features 12 | 13 | * Based on Swift 3 and Xcode 8; 14 | * The drawing canvas is zoomable; 15 | * Two modes to differentiate pan gestures; 16 | * Support undo/redo operations; 17 | * Strokes' thickness and color is configurable; 18 | * Share the final image; 19 | 20 | ## License 21 | This code is distributed under the terms and conditions of the [MIT license](LICENSE). 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /SFZoomableCanvas.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 7B13D8111D5ACE8D006A1787 /* SFColorPickerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B13D8101D5ACE8D006A1787 /* SFColorPickerView.swift */; }; 11 | 7B13D8131D5AEFF9006A1787 /* SFGradientView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B13D8121D5AEFF9006A1787 /* SFGradientView.swift */; }; 12 | 7B46B7051D58E2D3004296A8 /* SFModels.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B46B7041D58E2D3004296A8 /* SFModels.swift */; }; 13 | 7B5B65A01D5B49310075975B /* README.md in Sources */ = {isa = PBXBuildFile; fileRef = 7B5B659F1D5B49310075975B /* README.md */; }; 14 | 7B5B65A31D5B49400075975B /* IMG_0800.PNG in Resources */ = {isa = PBXBuildFile; fileRef = 7B5B65A11D5B49400075975B /* IMG_0800.PNG */; }; 15 | 7B5B65A41D5B49400075975B /* IMG_0799.PNG in Resources */ = {isa = PBXBuildFile; fileRef = 7B5B65A21D5B49400075975B /* IMG_0799.PNG */; }; 16 | 7B85C2F41D5AB35000972FCC /* SFSettingVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B85C2F31D5AB35000972FCC /* SFSettingVC.swift */; }; 17 | 7BB1B0161D5BFF8100D63B4F /* UIImage+SF.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BB1B0151D5BFF8100D63B4F /* UIImage+SF.swift */; }; 18 | 7BE7EAE31D58DBAB00BF0538 /* SFCanvasView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BE7EAE21D58DBAB00BF0538 /* SFCanvasView.swift */; }; 19 | 7BE828BD1D58691A00F4DBB8 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BE828BC1D58691A00F4DBB8 /* AppDelegate.swift */; }; 20 | 7BE828BF1D58691A00F4DBB8 /* SFZoomableCanvasVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BE828BE1D58691A00F4DBB8 /* SFZoomableCanvasVC.swift */; }; 21 | 7BE828C21D58691A00F4DBB8 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 7BE828C01D58691A00F4DBB8 /* Main.storyboard */; }; 22 | 7BE828C41D58691A00F4DBB8 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7BE828C31D58691A00F4DBB8 /* Assets.xcassets */; }; 23 | 7BE828C71D58691A00F4DBB8 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 7BE828C51D58691A00F4DBB8 /* LaunchScreen.storyboard */; }; 24 | /* End PBXBuildFile section */ 25 | 26 | /* Begin PBXCopyFilesBuildPhase section */ 27 | 7B114ECE1D5A2E5D0031ED4F /* Embed Frameworks */ = { 28 | isa = PBXCopyFilesBuildPhase; 29 | buildActionMask = 2147483647; 30 | dstPath = ""; 31 | dstSubfolderSpec = 10; 32 | files = ( 33 | ); 34 | name = "Embed Frameworks"; 35 | runOnlyForDeploymentPostprocessing = 0; 36 | }; 37 | /* End PBXCopyFilesBuildPhase section */ 38 | 39 | /* Begin PBXFileReference section */ 40 | 7B13D8101D5ACE8D006A1787 /* SFColorPickerView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SFColorPickerView.swift; sourceTree = ""; }; 41 | 7B13D8121D5AEFF9006A1787 /* SFGradientView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SFGradientView.swift; sourceTree = ""; }; 42 | 7B46B7041D58E2D3004296A8 /* SFModels.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SFModels.swift; sourceTree = ""; }; 43 | 7B5B659F1D5B49310075975B /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 44 | 7B5B65A11D5B49400075975B /* IMG_0800.PNG */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = IMG_0800.PNG; sourceTree = ""; }; 45 | 7B5B65A21D5B49400075975B /* IMG_0799.PNG */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = IMG_0799.PNG; sourceTree = ""; }; 46 | 7B85C2F31D5AB35000972FCC /* SFSettingVC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SFSettingVC.swift; sourceTree = ""; }; 47 | 7BB1B0151D5BFF8100D63B4F /* UIImage+SF.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+SF.swift"; sourceTree = ""; }; 48 | 7BE7EAE21D58DBAB00BF0538 /* SFCanvasView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SFCanvasView.swift; sourceTree = ""; }; 49 | 7BE828B91D58691A00F4DBB8 /* SFZoomableCanvas.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SFZoomableCanvas.app; sourceTree = BUILT_PRODUCTS_DIR; }; 50 | 7BE828BC1D58691A00F4DBB8 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 51 | 7BE828BE1D58691A00F4DBB8 /* SFZoomableCanvasVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SFZoomableCanvasVC.swift; sourceTree = ""; }; 52 | 7BE828C11D58691A00F4DBB8 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 53 | 7BE828C31D58691A00F4DBB8 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 54 | 7BE828C61D58691A00F4DBB8 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 55 | 7BE828C81D58691A00F4DBB8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 56 | /* End PBXFileReference section */ 57 | 58 | /* Begin PBXFrameworksBuildPhase section */ 59 | 7BE828B61D58691A00F4DBB8 /* Frameworks */ = { 60 | isa = PBXFrameworksBuildPhase; 61 | buildActionMask = 2147483647; 62 | files = ( 63 | ); 64 | runOnlyForDeploymentPostprocessing = 0; 65 | }; 66 | /* End PBXFrameworksBuildPhase section */ 67 | 68 | /* Begin PBXGroup section */ 69 | 7BE828B01D58691A00F4DBB8 = { 70 | isa = PBXGroup; 71 | children = ( 72 | 7B5B65A11D5B49400075975B /* IMG_0800.PNG */, 73 | 7B5B65A21D5B49400075975B /* IMG_0799.PNG */, 74 | 7B5B659F1D5B49310075975B /* README.md */, 75 | 7BE828BB1D58691A00F4DBB8 /* SFZoomableCanvas */, 76 | 7BE828BA1D58691A00F4DBB8 /* Products */, 77 | ); 78 | sourceTree = ""; 79 | }; 80 | 7BE828BA1D58691A00F4DBB8 /* Products */ = { 81 | isa = PBXGroup; 82 | children = ( 83 | 7BE828B91D58691A00F4DBB8 /* SFZoomableCanvas.app */, 84 | ); 85 | name = Products; 86 | sourceTree = ""; 87 | }; 88 | 7BE828BB1D58691A00F4DBB8 /* SFZoomableCanvas */ = { 89 | isa = PBXGroup; 90 | children = ( 91 | 7BB1B0151D5BFF8100D63B4F /* UIImage+SF.swift */, 92 | 7BE828BC1D58691A00F4DBB8 /* AppDelegate.swift */, 93 | 7BE7EAE21D58DBAB00BF0538 /* SFCanvasView.swift */, 94 | 7B46B7041D58E2D3004296A8 /* SFModels.swift */, 95 | 7B13D8101D5ACE8D006A1787 /* SFColorPickerView.swift */, 96 | 7BE828BE1D58691A00F4DBB8 /* SFZoomableCanvasVC.swift */, 97 | 7B85C2F31D5AB35000972FCC /* SFSettingVC.swift */, 98 | 7B13D8121D5AEFF9006A1787 /* SFGradientView.swift */, 99 | 7BE828C01D58691A00F4DBB8 /* Main.storyboard */, 100 | 7BE828C31D58691A00F4DBB8 /* Assets.xcassets */, 101 | 7BE828C51D58691A00F4DBB8 /* LaunchScreen.storyboard */, 102 | 7BE828C81D58691A00F4DBB8 /* Info.plist */, 103 | ); 104 | path = SFZoomableCanvas; 105 | sourceTree = ""; 106 | }; 107 | /* End PBXGroup section */ 108 | 109 | /* Begin PBXNativeTarget section */ 110 | 7BE828B81D58691A00F4DBB8 /* SFZoomableCanvas */ = { 111 | isa = PBXNativeTarget; 112 | buildConfigurationList = 7BE828CB1D58691A00F4DBB8 /* Build configuration list for PBXNativeTarget "SFZoomableCanvas" */; 113 | buildPhases = ( 114 | 7BE828B51D58691A00F4DBB8 /* Sources */, 115 | 7BE828B61D58691A00F4DBB8 /* Frameworks */, 116 | 7BE828B71D58691A00F4DBB8 /* Resources */, 117 | 7B114ECE1D5A2E5D0031ED4F /* Embed Frameworks */, 118 | ); 119 | buildRules = ( 120 | ); 121 | dependencies = ( 122 | ); 123 | name = SFZoomableCanvas; 124 | productName = SFZoomableCanvas; 125 | productReference = 7BE828B91D58691A00F4DBB8 /* SFZoomableCanvas.app */; 126 | productType = "com.apple.product-type.application"; 127 | }; 128 | /* End PBXNativeTarget section */ 129 | 130 | /* Begin PBXProject section */ 131 | 7BE828B11D58691A00F4DBB8 /* Project object */ = { 132 | isa = PBXProject; 133 | attributes = { 134 | LastSwiftUpdateCheck = 0800; 135 | LastUpgradeCheck = 0800; 136 | ORGANIZATIONNAME = SwordFish; 137 | TargetAttributes = { 138 | 7BE828B81D58691A00F4DBB8 = { 139 | CreatedOnToolsVersion = 8.0; 140 | DevelopmentTeam = ZP39L9XLVC; 141 | DevelopmentTeamName = "Wanfei Chen"; 142 | ProvisioningStyle = Automatic; 143 | }; 144 | }; 145 | }; 146 | buildConfigurationList = 7BE828B41D58691A00F4DBB8 /* Build configuration list for PBXProject "SFZoomableCanvas" */; 147 | compatibilityVersion = "Xcode 3.2"; 148 | developmentRegion = English; 149 | hasScannedForEncodings = 0; 150 | knownRegions = ( 151 | en, 152 | Base, 153 | ); 154 | mainGroup = 7BE828B01D58691A00F4DBB8; 155 | productRefGroup = 7BE828BA1D58691A00F4DBB8 /* Products */; 156 | projectDirPath = ""; 157 | projectRoot = ""; 158 | targets = ( 159 | 7BE828B81D58691A00F4DBB8 /* SFZoomableCanvas */, 160 | ); 161 | }; 162 | /* End PBXProject section */ 163 | 164 | /* Begin PBXResourcesBuildPhase section */ 165 | 7BE828B71D58691A00F4DBB8 /* Resources */ = { 166 | isa = PBXResourcesBuildPhase; 167 | buildActionMask = 2147483647; 168 | files = ( 169 | 7BE828C71D58691A00F4DBB8 /* LaunchScreen.storyboard in Resources */, 170 | 7BE828C41D58691A00F4DBB8 /* Assets.xcassets in Resources */, 171 | 7BE828C21D58691A00F4DBB8 /* Main.storyboard in Resources */, 172 | 7B5B65A41D5B49400075975B /* IMG_0799.PNG in Resources */, 173 | 7B5B65A31D5B49400075975B /* IMG_0800.PNG in Resources */, 174 | ); 175 | runOnlyForDeploymentPostprocessing = 0; 176 | }; 177 | /* End PBXResourcesBuildPhase section */ 178 | 179 | /* Begin PBXSourcesBuildPhase section */ 180 | 7BE828B51D58691A00F4DBB8 /* Sources */ = { 181 | isa = PBXSourcesBuildPhase; 182 | buildActionMask = 2147483647; 183 | files = ( 184 | 7B85C2F41D5AB35000972FCC /* SFSettingVC.swift in Sources */, 185 | 7B13D8131D5AEFF9006A1787 /* SFGradientView.swift in Sources */, 186 | 7BE828BF1D58691A00F4DBB8 /* SFZoomableCanvasVC.swift in Sources */, 187 | 7BE828BD1D58691A00F4DBB8 /* AppDelegate.swift in Sources */, 188 | 7B13D8111D5ACE8D006A1787 /* SFColorPickerView.swift in Sources */, 189 | 7B46B7051D58E2D3004296A8 /* SFModels.swift in Sources */, 190 | 7B5B65A01D5B49310075975B /* README.md in Sources */, 191 | 7BE7EAE31D58DBAB00BF0538 /* SFCanvasView.swift in Sources */, 192 | 7BB1B0161D5BFF8100D63B4F /* UIImage+SF.swift in Sources */, 193 | ); 194 | runOnlyForDeploymentPostprocessing = 0; 195 | }; 196 | /* End PBXSourcesBuildPhase section */ 197 | 198 | /* Begin PBXVariantGroup section */ 199 | 7BE828C01D58691A00F4DBB8 /* Main.storyboard */ = { 200 | isa = PBXVariantGroup; 201 | children = ( 202 | 7BE828C11D58691A00F4DBB8 /* Base */, 203 | ); 204 | name = Main.storyboard; 205 | sourceTree = ""; 206 | }; 207 | 7BE828C51D58691A00F4DBB8 /* LaunchScreen.storyboard */ = { 208 | isa = PBXVariantGroup; 209 | children = ( 210 | 7BE828C61D58691A00F4DBB8 /* Base */, 211 | ); 212 | name = LaunchScreen.storyboard; 213 | sourceTree = ""; 214 | }; 215 | /* End PBXVariantGroup section */ 216 | 217 | /* Begin XCBuildConfiguration section */ 218 | 7BE828C91D58691A00F4DBB8 /* Debug */ = { 219 | isa = XCBuildConfiguration; 220 | buildSettings = { 221 | ALWAYS_SEARCH_USER_PATHS = NO; 222 | CLANG_ANALYZER_NONNULL = YES; 223 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 224 | CLANG_CXX_LIBRARY = "libc++"; 225 | CLANG_ENABLE_MODULES = YES; 226 | CLANG_ENABLE_OBJC_ARC = YES; 227 | CLANG_WARN_BOOL_CONVERSION = YES; 228 | CLANG_WARN_CONSTANT_CONVERSION = YES; 229 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 230 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 231 | CLANG_WARN_EMPTY_BODY = YES; 232 | CLANG_WARN_ENUM_CONVERSION = YES; 233 | CLANG_WARN_INT_CONVERSION = YES; 234 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 235 | CLANG_WARN_UNREACHABLE_CODE = YES; 236 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 237 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 238 | COPY_PHASE_STRIP = NO; 239 | DEBUG_INFORMATION_FORMAT = dwarf; 240 | ENABLE_STRICT_OBJC_MSGSEND = YES; 241 | ENABLE_TESTABILITY = YES; 242 | GCC_C_LANGUAGE_STANDARD = gnu99; 243 | GCC_DYNAMIC_NO_PIC = NO; 244 | GCC_NO_COMMON_BLOCKS = YES; 245 | GCC_OPTIMIZATION_LEVEL = 0; 246 | GCC_PREPROCESSOR_DEFINITIONS = ( 247 | "DEBUG=1", 248 | "$(inherited)", 249 | ); 250 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 251 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 252 | GCC_WARN_UNDECLARED_SELECTOR = YES; 253 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 254 | GCC_WARN_UNUSED_FUNCTION = YES; 255 | GCC_WARN_UNUSED_VARIABLE = YES; 256 | IPHONEOS_DEPLOYMENT_TARGET = 10.0; 257 | MTL_ENABLE_DEBUG_INFO = YES; 258 | ONLY_ACTIVE_ARCH = YES; 259 | SDKROOT = iphoneos; 260 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 261 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 262 | TARGETED_DEVICE_FAMILY = 2; 263 | }; 264 | name = Debug; 265 | }; 266 | 7BE828CA1D58691A00F4DBB8 /* Release */ = { 267 | isa = XCBuildConfiguration; 268 | buildSettings = { 269 | ALWAYS_SEARCH_USER_PATHS = NO; 270 | CLANG_ANALYZER_NONNULL = YES; 271 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 272 | CLANG_CXX_LIBRARY = "libc++"; 273 | CLANG_ENABLE_MODULES = YES; 274 | CLANG_ENABLE_OBJC_ARC = YES; 275 | CLANG_WARN_BOOL_CONVERSION = YES; 276 | CLANG_WARN_CONSTANT_CONVERSION = YES; 277 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 278 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 279 | CLANG_WARN_EMPTY_BODY = YES; 280 | CLANG_WARN_ENUM_CONVERSION = YES; 281 | CLANG_WARN_INT_CONVERSION = YES; 282 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 283 | CLANG_WARN_UNREACHABLE_CODE = YES; 284 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 285 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 286 | COPY_PHASE_STRIP = NO; 287 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 288 | ENABLE_NS_ASSERTIONS = NO; 289 | ENABLE_STRICT_OBJC_MSGSEND = YES; 290 | GCC_C_LANGUAGE_STANDARD = gnu99; 291 | GCC_NO_COMMON_BLOCKS = YES; 292 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 293 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 294 | GCC_WARN_UNDECLARED_SELECTOR = YES; 295 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 296 | GCC_WARN_UNUSED_FUNCTION = YES; 297 | GCC_WARN_UNUSED_VARIABLE = YES; 298 | IPHONEOS_DEPLOYMENT_TARGET = 10.0; 299 | MTL_ENABLE_DEBUG_INFO = NO; 300 | SDKROOT = iphoneos; 301 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 302 | TARGETED_DEVICE_FAMILY = 2; 303 | VALIDATE_PRODUCT = YES; 304 | }; 305 | name = Release; 306 | }; 307 | 7BE828CC1D58691A00F4DBB8 /* Debug */ = { 308 | isa = XCBuildConfiguration; 309 | buildSettings = { 310 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 311 | ENABLE_BITCODE = YES; 312 | FRAMEWORK_SEARCH_PATHS = ( 313 | "$(inherited)", 314 | "$(PROJECT_DIR)/Carthage/Build/iOS", 315 | "$(PROJECT_DIR)/Vendors", 316 | ); 317 | INFOPLIST_FILE = SFZoomableCanvas/Info.plist; 318 | IPHONEOS_DEPLOYMENT_TARGET = 8.4; 319 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 320 | PRODUCT_BUNDLE_IDENTIFIER = com.swordfish.SFZoomableCanvas; 321 | PRODUCT_NAME = "$(TARGET_NAME)"; 322 | SWIFT_VERSION = 3.0; 323 | }; 324 | name = Debug; 325 | }; 326 | 7BE828CD1D58691A00F4DBB8 /* Release */ = { 327 | isa = XCBuildConfiguration; 328 | buildSettings = { 329 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 330 | ENABLE_BITCODE = YES; 331 | FRAMEWORK_SEARCH_PATHS = ( 332 | "$(inherited)", 333 | "$(PROJECT_DIR)/Carthage/Build/iOS", 334 | "$(PROJECT_DIR)/Vendors", 335 | ); 336 | INFOPLIST_FILE = SFZoomableCanvas/Info.plist; 337 | IPHONEOS_DEPLOYMENT_TARGET = 8.4; 338 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 339 | PRODUCT_BUNDLE_IDENTIFIER = com.swordfish.SFZoomableCanvas; 340 | PRODUCT_NAME = "$(TARGET_NAME)"; 341 | SWIFT_VERSION = 3.0; 342 | }; 343 | name = Release; 344 | }; 345 | /* End XCBuildConfiguration section */ 346 | 347 | /* Begin XCConfigurationList section */ 348 | 7BE828B41D58691A00F4DBB8 /* Build configuration list for PBXProject "SFZoomableCanvas" */ = { 349 | isa = XCConfigurationList; 350 | buildConfigurations = ( 351 | 7BE828C91D58691A00F4DBB8 /* Debug */, 352 | 7BE828CA1D58691A00F4DBB8 /* Release */, 353 | ); 354 | defaultConfigurationIsVisible = 0; 355 | defaultConfigurationName = Release; 356 | }; 357 | 7BE828CB1D58691A00F4DBB8 /* Build configuration list for PBXNativeTarget "SFZoomableCanvas" */ = { 358 | isa = XCConfigurationList; 359 | buildConfigurations = ( 360 | 7BE828CC1D58691A00F4DBB8 /* Debug */, 361 | 7BE828CD1D58691A00F4DBB8 /* Release */, 362 | ); 363 | defaultConfigurationIsVisible = 0; 364 | defaultConfigurationName = Release; 365 | }; 366 | /* End XCConfigurationList section */ 367 | }; 368 | rootObject = 7BE828B11D58691A00F4DBB8 /* Project object */; 369 | } 370 | -------------------------------------------------------------------------------- /SFZoomableCanvas.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SFZoomableCanvas.xcodeproj/xcuserdata/chenwanfei.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 8 | 20 | 21 | 22 | 24 | 36 | 37 | 38 | 40 | 52 | 53 | 54 | 56 | 68 | 69 | 70 | 72 | 84 | 85 | 86 | 88 | 100 | 101 | 102 | 104 | 116 | 117 | 118 | 120 | 132 | 133 | 134 | 136 | 148 | 149 | 150 | 152 | 164 | 165 | 166 | 168 | 180 | 181 | 182 | 184 | 196 | 197 | 198 | 199 | 200 | -------------------------------------------------------------------------------- /SFZoomableCanvas.xcodeproj/xcuserdata/chenwanfei.xcuserdatad/xcschemes/SFZoomableCanvas.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /SFZoomableCanvas.xcodeproj/xcuserdata/chenwanfei.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | SFZoomableCanvas.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 7BE828B81D58691A00F4DBB8 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /SFZoomableCanvas/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // SFZoomableCanvas 4 | // 5 | // Created by CHENWANFEI on 8/8/16. 6 | // Copyright © 2016 SwordFish. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | 18 | 19 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool{ 20 | // Override point for customization after application launch. 21 | return true 22 | } 23 | 24 | func applicationWillResignActive(_ application: UIApplication) { 25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 26 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 27 | } 28 | 29 | func applicationDidEnterBackground(_ application: UIApplication) { 30 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 31 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 32 | } 33 | 34 | func applicationWillEnterForeground(_ application: UIApplication) { 35 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 36 | } 37 | 38 | func applicationDidBecomeActive(_ application: UIApplication) { 39 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 40 | } 41 | 42 | func applicationWillTerminate(_ application: UIApplication) { 43 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 44 | } 45 | 46 | 47 | } 48 | 49 | -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "ipad", 5 | "size" : "29x29", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "ipad", 10 | "size" : "29x29", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "ipad", 15 | "size" : "40x40", 16 | "scale" : "1x" 17 | }, 18 | { 19 | "idiom" : "ipad", 20 | "size" : "40x40", 21 | "scale" : "2x" 22 | }, 23 | { 24 | "idiom" : "ipad", 25 | "size" : "76x76", 26 | "scale" : "1x" 27 | }, 28 | { 29 | "size" : "76x76", 30 | "idiom" : "ipad", 31 | "filename" : "appicon@2x.png", 32 | "scale" : "2x" 33 | }, 34 | { 35 | "idiom" : "ipad", 36 | "size" : "83.5x83.5", 37 | "scale" : "2x" 38 | } 39 | ], 40 | "info" : { 41 | "version" : 1, 42 | "author" : "xcode" 43 | } 44 | } -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/AppIcon.appiconset/appicon@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/SFZoomableCanvas/Assets.xcassets/AppIcon.appiconset/appicon@2x.png -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/action.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "action@2x.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "filename" : "action@3x.png", 15 | "scale" : "3x" 16 | } 17 | ], 18 | "info" : { 19 | "version" : 1, 20 | "author" : "xcode" 21 | } 22 | } -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/action.imageset/action@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/SFZoomableCanvas/Assets.xcassets/action.imageset/action@2x.png -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/action.imageset/action@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/SFZoomableCanvas/Assets.xcassets/action.imageset/action@3x.png -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/circle_icon.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "circle_icon@2x.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "filename" : "circle_icon@3x.png", 15 | "scale" : "3x" 16 | } 17 | ], 18 | "info" : { 19 | "version" : 1, 20 | "author" : "xcode" 21 | } 22 | } -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/circle_icon.imageset/circle_icon@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/SFZoomableCanvas/Assets.xcassets/circle_icon.imageset/circle_icon@2x.png -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/circle_icon.imageset/circle_icon@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/SFZoomableCanvas/Assets.xcassets/circle_icon.imageset/circle_icon@3x.png -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/colormap.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "colormap.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/colormap.imageset/colormap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/SFZoomableCanvas/Assets.xcassets/colormap.imageset/colormap.png -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/delete_icon.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "delete_icon@2x.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "filename" : "delete_icon@3x.png", 15 | "scale" : "3x" 16 | } 17 | ], 18 | "info" : { 19 | "version" : 1, 20 | "author" : "xcode" 21 | } 22 | } -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/delete_icon.imageset/delete_icon@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/SFZoomableCanvas/Assets.xcassets/delete_icon.imageset/delete_icon@2x.png -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/delete_icon.imageset/delete_icon@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/SFZoomableCanvas/Assets.xcassets/delete_icon.imageset/delete_icon@3x.png -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/drawing_tools.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "drawing_tools@2x.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "filename" : "drawing_tools@3x.png", 15 | "scale" : "3x" 16 | } 17 | ], 18 | "info" : { 19 | "version" : 1, 20 | "author" : "xcode" 21 | } 22 | } -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/drawing_tools.imageset/drawing_tools@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/SFZoomableCanvas/Assets.xcassets/drawing_tools.imageset/drawing_tools@2x.png -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/drawing_tools.imageset/drawing_tools@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/SFZoomableCanvas/Assets.xcassets/drawing_tools.imageset/drawing_tools@3x.png -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/drawing_tools_selected.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "drawing_tools_selected@2x.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "filename" : "drawing_tools_selected@3x.png", 15 | "scale" : "3x" 16 | } 17 | ], 18 | "info" : { 19 | "version" : 1, 20 | "author" : "xcode" 21 | } 22 | } -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/drawing_tools_selected.imageset/drawing_tools_selected@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/SFZoomableCanvas/Assets.xcassets/drawing_tools_selected.imageset/drawing_tools_selected@2x.png -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/drawing_tools_selected.imageset/drawing_tools_selected@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/SFZoomableCanvas/Assets.xcassets/drawing_tools_selected.imageset/drawing_tools_selected@3x.png -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/finger.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "finger@2x.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "filename" : "finger@3x.png", 15 | "scale" : "3x" 16 | } 17 | ], 18 | "info" : { 19 | "version" : 1, 20 | "author" : "xcode" 21 | } 22 | } -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/finger.imageset/finger@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/SFZoomableCanvas/Assets.xcassets/finger.imageset/finger@2x.png -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/finger.imageset/finger@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/SFZoomableCanvas/Assets.xcassets/finger.imageset/finger@3x.png -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/finger_selected.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "finger_selected@2x.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "filename" : "finger_selected@3x.png", 15 | "scale" : "3x" 16 | } 17 | ], 18 | "info" : { 19 | "version" : 1, 20 | "author" : "xcode" 21 | } 22 | } -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/finger_selected.imageset/finger_selected@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/SFZoomableCanvas/Assets.xcassets/finger_selected.imageset/finger_selected@2x.png -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/finger_selected.imageset/finger_selected@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/SFZoomableCanvas/Assets.xcassets/finger_selected.imageset/finger_selected@3x.png -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/redo.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "redo@2x.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "filename" : "redo@3x.png", 15 | "scale" : "3x" 16 | } 17 | ], 18 | "info" : { 19 | "version" : 1, 20 | "author" : "xcode" 21 | } 22 | } -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/redo.imageset/redo@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/SFZoomableCanvas/Assets.xcassets/redo.imageset/redo@2x.png -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/redo.imageset/redo@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/SFZoomableCanvas/Assets.xcassets/redo.imageset/redo@3x.png -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/undo.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "undo@2x.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "filename" : "undo@3x.png", 15 | "scale" : "3x" 16 | } 17 | ], 18 | "info" : { 19 | "version" : 1, 20 | "author" : "xcode" 21 | } 22 | } -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/undo.imageset/undo@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/SFZoomableCanvas/Assets.xcassets/undo.imageset/undo@2x.png -------------------------------------------------------------------------------- /SFZoomableCanvas/Assets.xcassets/undo.imageset/undo@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Heilum/SFZoomableCanvas/2ec6e9a41b91036d7c4090fcf732dc216227da89/SFZoomableCanvas/Assets.xcassets/undo.imageset/undo@3x.png -------------------------------------------------------------------------------- /SFZoomableCanvas/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /SFZoomableCanvas/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 65 | 78 | 91 | 104 | 117 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 181 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | -------------------------------------------------------------------------------- /SFZoomableCanvas/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UIRequiresFullScreen 34 | 35 | UISupportedInterfaceOrientations~ipad 36 | 37 | UIInterfaceOrientationLandscapeLeft 38 | UIInterfaceOrientationLandscapeRight 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /SFZoomableCanvas/SFCanvasView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SFCanvasView.swift 3 | // SFZoomableCanvas 4 | // 5 | // Created by CHENWANFEI on 8/8/16. 6 | // Copyright © 2016 SwordFish. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | 12 | 13 | 14 | class SFCanvasView: UIView { 15 | var strokes:[SFStroke]?{ 16 | didSet{ 17 | self.setNeedsDisplay(); 18 | } 19 | } 20 | 21 | 22 | override func draw(_ rect: CGRect) { 23 | super.draw(rect); 24 | if let ss = self.strokes{ 25 | guard let context = UIGraphicsGetCurrentContext() else { 26 | return 27 | } 28 | 29 | 30 | for s in ss{ 31 | context.setStrokeColor(s.color) 32 | context.setLineWidth(s.lineWidth) 33 | context.addPath(s.path); 34 | context.drawPath(using: .stroke) 35 | } 36 | } 37 | 38 | 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /SFZoomableCanvas/SFColorPickerView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SFColorPickerView.swift 3 | // SFZoomableCanvas 4 | // 5 | // Created by CHENWANFEI on 8/10/16. 6 | // Copyright © 2016 SwordFish. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | 12 | 13 | 14 | 15 | class SFColorPickerView: UIView { 16 | 17 | 18 | private let gradientViewHeight = CGFloat(20); 19 | private let gradientViewGap = CGFloat(20); 20 | private let crossHairWidth = CGFloat(38); 21 | private let scrabSize = CGSize(width:15,height:40); 22 | 23 | 24 | private weak var hueSaturationImageView:UIImageView!; 25 | private weak var hueSaturationCrossHairs:UIView!; 26 | private weak var gradientView:SFGradientView! 27 | private weak var brightnessScrab:UIView! 28 | 29 | 30 | private var currentHue:CGFloat = 0 31 | private var currentSaturation:CGFloat = 0 32 | private var currentBrightness:CGFloat = 1; 33 | 34 | private var lastTouchLocation:CGPoint? 35 | private var isDragingCrossHair:Bool = false; 36 | private var isDragingScrab:Bool = false; 37 | 38 | var initColor:UIColor?{ 39 | didSet{ 40 | if let color = self.initColor{ 41 | color.getHue(&self.currentHue, saturation: &self.currentSaturation, brightness: ¤tBrightness, alpha: nil); 42 | } 43 | 44 | 45 | 46 | } 47 | } 48 | 49 | var colorSelection:((UIColor) -> Void)? 50 | 51 | private var color:UIColor{ 52 | return UIColor(hue: currentHue, saturation: currentSaturation, brightness: currentBrightness, alpha: 1); 53 | } 54 | 55 | 56 | 57 | 58 | override init(frame: CGRect) { 59 | super.init(frame: frame); 60 | self.doInit(); 61 | } 62 | 63 | required init?(coder aDecoder: NSCoder) { 64 | super.init(coder: aDecoder); 65 | self.doInit() 66 | } 67 | 68 | private func doInit(){ 69 | //set the container's minimum height for layout 70 | self.clipsToBounds = false; 71 | self.bounds = CGRect(x: 0, y: 0, width: 0, height: gradientViewHeight + gradientViewGap); 72 | 73 | let hueSaturationImageView = UIImageView(); 74 | hueSaturationImageView.image = #imageLiteral(resourceName: "colormap") 75 | 76 | 77 | hueSaturationImageView.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height - gradientViewHeight - gradientViewGap); 78 | 79 | hueSaturationImageView.autoresizingMask = [UIViewAutoresizing.flexibleHeight,UIViewAutoresizing.flexibleWidth]; 80 | 81 | 82 | self.addSubview(hueSaturationImageView); 83 | 84 | self.hueSaturationImageView = hueSaturationImageView; 85 | 86 | 87 | let hueSaturationCrossHairs = UIView(frame: CGRect(x: 0, y: 0, width: crossHairWidth, height: crossHairWidth)); 88 | 89 | hueSaturationImageView.addSubview(hueSaturationCrossHairs); 90 | 91 | hueSaturationCrossHairs.layer.cornerRadius = crossHairWidth / 2; 92 | hueSaturationCrossHairs.layer.borderColor = UIColor.white.withAlphaComponent(0.8).cgColor; 93 | hueSaturationCrossHairs.layer.borderWidth = 2; 94 | hueSaturationCrossHairs.layer.shadowColor = UIColor.black.cgColor 95 | hueSaturationCrossHairs.layer.shadowRadius = 1; 96 | hueSaturationCrossHairs.layer.shadowOpacity = 0.5 97 | hueSaturationCrossHairs.layer.shadowOffset = CGSize(width: 0, height: 0) 98 | 99 | hueSaturationCrossHairs.center = CGPoint(x:0,y:0); 100 | 101 | 102 | self.hueSaturationCrossHairs = hueSaturationCrossHairs; 103 | 104 | 105 | 106 | 107 | 108 | let gradientView = SFGradientView(frame: CGRect(x: 0, y: self.bounds.height - gradientViewHeight , width: self.bounds.width, height: gradientViewHeight)); 109 | gradientView.autoresizingMask = [UIViewAutoresizing.flexibleTopMargin,UIViewAutoresizing.flexibleWidth]; 110 | //test 111 | gradientView.backgroundColor = UIColor.red; 112 | 113 | gradientView.gradientLayer.startPoint = CGPoint(x: 0, y: 0.5); 114 | gradientView.gradientLayer.endPoint = CGPoint(x: 1, y: 0.5); 115 | 116 | self.addSubview(gradientView); 117 | self.gradientView = gradientView; 118 | 119 | let brightnessScrab = UIView(frame:CGRect(x: 0, y: 0, width: scrabSize.width, height: scrabSize.height)); 120 | 121 | brightnessScrab.layer.cornerRadius = scrabSize.width / 2; 122 | brightnessScrab.layer.borderColor = UIColor.white.withAlphaComponent(0.8).cgColor; 123 | brightnessScrab.layer.borderWidth = 2; 124 | 125 | gradientView.addSubview(brightnessScrab); 126 | brightnessScrab.center = CGPoint(x:0,y:gradientView.bounds.height / 2); 127 | self.brightnessScrab = brightnessScrab 128 | 129 | 130 | } 131 | 132 | override func touchesBegan(_ touches: Set, with event: UIEvent?) { 133 | if let t = touches.first{ 134 | self.lastTouchLocation = t.location(in: self); 135 | 136 | let gradientArea = self.gradientView.frame.insetBy(dx: -20, dy: -20); 137 | let hueSaturationImageViewArea = hueSaturationImageView.frame.insetBy(dx: -20, dy: -20); 138 | if hueSaturationImageViewArea.contains(self.lastTouchLocation!) { 139 | isDragingCrossHair = true; 140 | }else if gradientArea.contains(self.lastTouchLocation!) { 141 | isDragingScrab = true; 142 | } 143 | 144 | 145 | } 146 | } 147 | 148 | override func touchesMoved(_ touches: Set, with event: UIEvent?) { 149 | if let t = touches.first{ 150 | self.onTouchMove(location: t.location(in: self)) 151 | } 152 | } 153 | 154 | override func touchesEnded(_ touches: Set, with event: UIEvent?) { 155 | self.lastTouchLocation = nil; 156 | isDragingScrab = false; 157 | isDragingCrossHair = false; 158 | } 159 | 160 | override func touchesCancelled(_ touches: Set, with event: UIEvent?) { 161 | self.lastTouchLocation = nil; 162 | isDragingScrab = false; 163 | isDragingCrossHair = false; 164 | } 165 | 166 | private func onTouchMove(location:CGPoint){ 167 | 168 | if let lastTouchLocation = self.lastTouchLocation{ 169 | let thisLocation = location; 170 | 171 | let deltaX = thisLocation.x - lastTouchLocation.x; 172 | let deltaY = thisLocation.y - lastTouchLocation.y; 173 | 174 | if isDragingCrossHair{ 175 | 176 | var newCX = hueSaturationCrossHairs.center.x + deltaX; 177 | newCX = min(max(newCX, 0), hueSaturationImageView.bounds.width); 178 | 179 | var newCY = hueSaturationCrossHairs.center.y + deltaY; 180 | newCY = min(max(newCY, 0), hueSaturationImageView.bounds.height); 181 | 182 | let cp = CGPoint(x: newCX, y: newCY); 183 | 184 | 185 | updateHueSatWithMovement(location: cp); 186 | 187 | 188 | }else if isDragingScrab{ 189 | var newCX = brightnessScrab.center.x + deltaX; 190 | newCX = min(max(newCX, 0), gradientView.bounds.width); 191 | 192 | let newCY = gradientView.bounds.height / 2; 193 | 194 | let cp = CGPoint(x: newCX, y: newCY); 195 | 196 | 197 | updateScrabWithMovement(location: cp); 198 | } 199 | 200 | self.lastTouchLocation = thisLocation; 201 | 202 | } 203 | 204 | 205 | } 206 | 207 | private func onColorFactorChanged(callHandler:Bool = false){ 208 | let beginColor = UIColor(hue: currentHue, saturation: currentSaturation, brightness: 1, alpha: 1); 209 | let endColor = UIColor(hue: currentHue, saturation: currentSaturation, brightness: 0, alpha: 1); 210 | 211 | self.gradientView.gradientLayer.colors = [beginColor.cgColor,endColor.cgColor]; 212 | 213 | 214 | 215 | let c = self.color; 216 | hueSaturationCrossHairs.layer.backgroundColor = c.cgColor; 217 | brightnessScrab.layer.backgroundColor = hueSaturationCrossHairs.layer.backgroundColor; 218 | if callHandler { 219 | if let selection = self.colorSelection { 220 | selection(c); 221 | } 222 | } 223 | 224 | 225 | } 226 | 227 | 228 | private func updateHueSatWithMovement(location:CGPoint){ 229 | currentHue = location.x / hueSaturationImageView.bounds.width; 230 | currentSaturation = CGFloat(1.0) - (location.y / hueSaturationImageView.bounds.height); 231 | hueSaturationCrossHairs.center = location; 232 | 233 | self.onColorFactorChanged(callHandler: true) 234 | } 235 | 236 | private func updateScrabWithMovement(location:CGPoint){ 237 | 238 | currentBrightness = CGFloat(1.0) - (location.x / gradientView.bounds.width); 239 | brightnessScrab.center = location; 240 | 241 | self.onColorFactorChanged(callHandler: true) 242 | 243 | } 244 | 245 | override func layoutSubviews() { 246 | super.layoutSubviews(); 247 | if let _ = self.initColor{ 248 | 249 | let x = self.hueSaturationImageView.bounds.width * self.currentHue 250 | 251 | hueSaturationCrossHairs.center = CGPoint(x:x,y:hueSaturationImageView.bounds.size.height * (1 - self.currentSaturation)); 252 | brightnessScrab.center = CGPoint(x:self.gradientView.bounds.width * (1 - self.currentBrightness),y:self.gradientView.bounds.height / 2); 253 | 254 | self.onColorFactorChanged(); 255 | 256 | self.initColor = nil; 257 | 258 | } 259 | } 260 | 261 | 262 | 263 | } 264 | -------------------------------------------------------------------------------- /SFZoomableCanvas/SFGradientView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SFGradientView.swift 3 | // SFZoomableCanvas 4 | // 5 | // Created by CHENWANFEI on 8/10/16. 6 | // Copyright © 2016 SwordFish. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class SFGradientView: UIView { 12 | 13 | 14 | 15 | override class var layerClass: AnyClass { 16 | return CAGradientLayer.self 17 | } 18 | 19 | lazy var gradientLayer: CAGradientLayer = { 20 | return self.layer as! CAGradientLayer 21 | }() 22 | } 23 | -------------------------------------------------------------------------------- /SFZoomableCanvas/SFModels.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SFModels.swift 3 | // SFZoomableCanvas 4 | // 5 | // Created by CHENWANFEI on 8/8/16. 6 | // Copyright © 2016 SwordFish. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import CoreGraphics 11 | struct SFStroke{ 12 | let color:CGColor; 13 | let lineWidth:CGFloat; 14 | let path:CGPath; 15 | } 16 | 17 | 18 | public class SFStrokeArrayBox :NSObject { 19 | let unbox:[SFStroke] 20 | init(_ value: [SFStroke]) { 21 | self.unbox = value 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /SFZoomableCanvas/SFSettingVC.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SFSettingVC.swift 3 | // SFZoomableCanvas 4 | // 5 | // Created by CHENWANFEI on 8/10/16. 6 | // Copyright © 2016 SwordFish. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class SFSettingVC: UIViewController { 12 | fileprivate var windowTapgesture:UIGestureRecognizer! 13 | 14 | 15 | @IBOutlet weak var colorPickerView: SFColorPickerView! 16 | 17 | @IBOutlet weak var thicknessSlider: UISlider! 18 | 19 | var initColor:UIColor? 20 | var colorSelection:((UIColor) -> Void)? 21 | var initThickness:CGFloat?; 22 | var thickneessSelection:((CGFloat) -> Void)? 23 | 24 | override func viewDidAppear(_ animated: Bool) { 25 | super.viewDidAppear(animated) 26 | self.addWindowTapGesture(); 27 | self.windowTapgesture.delegate = self; 28 | 29 | 30 | } 31 | 32 | override func viewWillDisappear(_ animated: Bool) { 33 | 34 | super.viewWillDisappear(animated); 35 | self.view.window!.removeGestureRecognizer(self.windowTapgesture); 36 | } 37 | 38 | @IBAction func onSlide(_ sender: AnyObject) { 39 | 40 | if let handler = self.thickneessSelection{ 41 | handler(CGFloat(self.thicknessSlider.value)) 42 | } 43 | 44 | 45 | } 46 | 47 | override func viewDidLoad(){ 48 | super.viewDidLoad(); 49 | if let color = self.initColor{ 50 | colorPickerView.initColor = color; 51 | } 52 | 53 | colorPickerView.colorSelection = self.colorSelection; 54 | 55 | if let initThickness = self.initThickness{ 56 | self.thicknessSlider.value = Float(initThickness); 57 | } 58 | 59 | } 60 | } 61 | 62 | extension SFSettingVC:UIGestureRecognizerDelegate{ 63 | 64 | 65 | func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { 66 | return true 67 | } 68 | 69 | 70 | func addWindowTapGesture(){ 71 | let tapBGGesture = UITapGestureRecognizer(target: self, action: #selector(windowTaped)) 72 | 73 | tapBGGesture.numberOfTapsRequired = 1 74 | tapBGGesture.cancelsTouchesInView = false 75 | self.view.window!.addGestureRecognizer(tapBGGesture) 76 | 77 | self.windowTapgesture = tapBGGesture; 78 | 79 | } 80 | 81 | func removeWindowTapGesture(){ 82 | if let w = self.view.window,let g = self.windowTapgesture{ 83 | w.removeGestureRecognizer(g); 84 | } 85 | 86 | } 87 | 88 | private dynamic func windowTaped(sender: UITapGestureRecognizer){ 89 | if sender.state == UIGestureRecognizerState.ended{ 90 | guard let presentedView = self.view else { 91 | return 92 | } 93 | 94 | 95 | if !presentedView.bounds.contains(sender.location(in: presentedView)) { 96 | self.dismiss(animated: true, completion:nil) 97 | } 98 | } 99 | } 100 | 101 | 102 | 103 | } 104 | -------------------------------------------------------------------------------- /SFZoomableCanvas/SFZoomableCanvasVC.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // SFZoomableCanvas 4 | // 5 | // Created by CHENWANFEI on 8/8/16. 6 | // Copyright © 2016 SwordFish. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | 12 | enum SFCanvasMode :Int { 13 | case none = 0; 14 | case drawing = 1; 15 | case positioning = 2; 16 | } 17 | 18 | 19 | 20 | final class SFZoomableCanvasVC: UIViewController { 21 | 22 | 23 | private let tintColor = UIColor(red: 208.0/225, green: 2.0/225, blue: 27.0/225, alpha: 1.0); 24 | private let animationDuration = TimeInterval(0.25) 25 | 26 | private let toolBtnWidth = CGFloat(50); 27 | 28 | 29 | @IBOutlet weak var canvasView: SFCanvasView! 30 | @IBOutlet weak var scrollView: UIScrollView! 31 | 32 | @IBOutlet var canvasPanGesture: UIPanGestureRecognizer! 33 | 34 | private var strokes = [SFStroke](); 35 | 36 | private var currentPath:UIBezierPath?; 37 | 38 | @IBOutlet weak var toolBtn: UIButton! 39 | 40 | @IBOutlet weak var toolsPanel: UIView! 41 | 42 | @IBOutlet weak var toolPanelHeightConstraint: NSLayoutConstraint! 43 | 44 | 45 | 46 | 47 | 48 | @IBOutlet weak var modeBtn: UIButton! 49 | 50 | @IBOutlet weak var undoBtn: UIButton! 51 | 52 | @IBOutlet weak var redoBtn: UIButton! 53 | 54 | @IBOutlet weak var clearBtn: UIButton! 55 | 56 | @IBOutlet weak var settingBtn: UIButton! 57 | 58 | 59 | @IBOutlet weak var shareBtn: UIButton! 60 | 61 | @IBOutlet weak var infoLabel: UILabel! 62 | 63 | 64 | private var _undoManager = UndoManager() 65 | override var undoManager: UndoManager { 66 | return _undoManager 67 | } 68 | 69 | private var thickness = CGFloat(0.5) { 70 | didSet{ 71 | 72 | self.settingBtn.setImage(self.buildSettingIcon(), for: []); 73 | 74 | 75 | } 76 | } 77 | private var lineColor = UIColor(red: 208.0/225, green: 2.0/225, blue: 27.0/225, alpha: 1.0){ 78 | didSet{ 79 | self.settingBtn.setImage(self.buildSettingIcon(), for: []); 80 | } 81 | } 82 | 83 | private func buildSettingIcon() -> UIImage?{ 84 | 85 | 86 | let size = CGSize(width: toolBtnWidth, height: toolBtnWidth) 87 | 88 | let center = CGPoint(x: size.width / 2,y:size.height / 2); 89 | 90 | let radius = size.width * thickness / 2 91 | 92 | UIGraphicsBeginImageContextWithOptions(size,false,0) 93 | let context = UIGraphicsGetCurrentContext() 94 | context?.saveGState(); 95 | 96 | 97 | // flip the image 98 | 99 | context?.scaleBy(x: 1, y: -1.0); 100 | context?.translateBy(x: 0.0, y: -size.height) 101 | 102 | 103 | 104 | // multiply blend mode 105 | context?.setBlendMode(CGBlendMode.multiply) 106 | 107 | let rect = CGRect(x:0, y:0, width:size.width, height:size.height) 108 | 109 | 110 | context?.clip(to: rect, mask: UIImage(named: "circle_icon")!.cgImage!) 111 | 112 | tintColor.setFill() 113 | context?.fill(rect) 114 | 115 | 116 | context?.restoreGState(); 117 | 118 | let path = UIBezierPath(arcCenter:center, radius: radius, startAngle: 0, endAngle: CGFloat(M_PI * 2), clockwise: true); 119 | context?.setFillColor(self.lineColor.cgColor); 120 | 121 | context?.addPath(path.cgPath); 122 | context?.fillPath(); 123 | 124 | 125 | 126 | // create uiimage 127 | let newImage = UIGraphicsGetImageFromCurrentImageContext() 128 | UIGraphicsEndImageContext() 129 | 130 | return newImage 131 | 132 | } 133 | 134 | @IBAction func onClear(_ sender: AnyObject) { 135 | 136 | 137 | let alertVC = UIAlertController(title: "Warnning", message: "All content will be erased, this operation can't undo, continue?", preferredStyle: UIAlertControllerStyle.alert); 138 | alertVC.addAction(UIAlertAction(title: "Continue", style: UIAlertActionStyle.default, handler: {[weak self] (action) in 139 | 140 | if let this = self{ 141 | this.strokes.removeAll(); 142 | this.canvasView.strokes = []; 143 | 144 | this.undoManager.removeAllActions(); 145 | 146 | this.updateUndoAndRedoButtons(); 147 | 148 | this.scrollView.setZoomScale(this.scrollView.minimumZoomScale, animated: true); 149 | 150 | this.mode = .drawing; 151 | 152 | } 153 | 154 | 155 | 156 | 157 | })); 158 | alertVC.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)); 159 | self.present(alertVC, animated: true, completion: nil); 160 | 161 | } 162 | 163 | fileprivate var mode:SFCanvasMode = SFCanvasMode.none{ 164 | didSet{ 165 | if mode == SFCanvasMode.drawing{ 166 | self.scrollView.isScrollEnabled = false; 167 | self.canvasPanGesture.isEnabled = true; 168 | 169 | 170 | 171 | self.modeBtn.isSelected = false; 172 | 173 | 174 | }else if(mode == SFCanvasMode.positioning){ 175 | self.scrollView.isScrollEnabled = true; 176 | self.canvasPanGesture.isEnabled = false; 177 | 178 | self.modeBtn.isSelected = true; 179 | 180 | } 181 | } 182 | } 183 | 184 | 185 | @IBAction func onToggleToolsBtn(_ sender: AnyObject) { 186 | self.showToolPanel(show: !self.toolBtn.isSelected,animated: true) 187 | } 188 | 189 | private func showToolPanel(show:Bool,animated:Bool){ 190 | self.toolBtn.isSelected = show; 191 | if self.toolBtn.isSelected { 192 | 193 | self.toolPanelHeightConstraint.constant = 353; 194 | 195 | }else{ 196 | 197 | 198 | self.toolPanelHeightConstraint.constant = 50; 199 | } 200 | 201 | if animated { 202 | 203 | UIView.animate(withDuration: animationDuration) { 204 | self.view.layoutIfNeeded() 205 | } 206 | } 207 | 208 | 209 | } 210 | 211 | @IBAction func onModeSwitch(_ sender: AnyObject) { 212 | if self.mode == .drawing{ 213 | self.mode = .positioning; 214 | 215 | self.showInfo(info: "Positioning mode"); 216 | 217 | }else{ 218 | self.mode = .drawing; 219 | self.showInfo(info: "Drawing mode"); 220 | 221 | } 222 | 223 | 224 | } 225 | 226 | override func viewDidLoad() { 227 | super.viewDidLoad() 228 | 229 | 230 | self.showToolPanel(show: false,animated: false); 231 | mode = .drawing 232 | self.modeBtn.isEnabled = false; 233 | 234 | self.lineColor = tintColor; 235 | self.thickness = 0.5 236 | self.infoLabel.textColor = tintColor; 237 | self.infoLabel.alpha = 0; 238 | 239 | NotificationCenter.default.addObserver(self, selector: #selector(updateUndoAndRedoButtons), name: Notification.Name.NSUndoManagerDidUndoChange, object: self.undoManager) 240 | 241 | 242 | 243 | 244 | NotificationCenter.default.addObserver(self, selector: #selector(updateUndoAndRedoButtons), name: Notification.Name.NSUndoManagerDidRedoChange, object: self.undoManager); 245 | 246 | 247 | updateUndoAndRedoButtons(); 248 | 249 | 250 | // Do any additional setup after loading the view, typically from a nib. 251 | } 252 | 253 | private dynamic func updateUndoAndRedoButtons() { 254 | 255 | self.undoBtn.isEnabled = undoManager.canUndo; 256 | self.redoBtn.isEnabled = undoManager.canRedo; 257 | } 258 | 259 | override func didReceiveMemoryWarning() { 260 | super.didReceiveMemoryWarning() 261 | // Dispose of any resources that can be recreated. 262 | } 263 | 264 | func delay(_ delay: Double, closure: @escaping ()->()) { 265 | DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: closure); 266 | 267 | 268 | } 269 | 270 | 271 | override var prefersStatusBarHidden: Bool { 272 | return true; 273 | } 274 | 275 | 276 | 277 | private func convertPath(_ path:UIBezierPath) -> SFStroke{ 278 | let color = self.lineColor; 279 | let lineWidth = self.settingBtn.bounds.width * thickness; 280 | let path = path.cgPath; 281 | let stroke = SFStroke(color: color.cgColor, lineWidth: lineWidth, path: path); 282 | 283 | return stroke; 284 | 285 | } 286 | 287 | override var canBecomeFirstResponder:Bool { 288 | return true 289 | } 290 | 291 | @IBAction func onCanvasPaning(_ sender: AnyObject) { 292 | let g = sender as! UIPanGestureRecognizer; 293 | let point = g.location(in: self.canvasView); 294 | switch g.state { 295 | case .began: 296 | self.currentPath = UIBezierPath(); 297 | self.currentPath?.lineCapStyle = CGLineCap.round; 298 | self.currentPath?.lineJoinStyle = CGLineJoin.round; 299 | self.currentPath?.move(to: point); 300 | let stroke = self.convertPath(self.currentPath!) 301 | self.strokes.append(stroke); 302 | 303 | 304 | 305 | case .changed: 306 | 307 | 308 | 309 | 310 | if let path = self.currentPath{ 311 | path.addLine(to: point); 312 | let stroke = self.convertPath(path) 313 | 314 | self.strokes[self.strokes.count - 1] = stroke; 315 | self.canvasView.strokes = self.strokes; 316 | } 317 | 318 | 319 | break; 320 | 321 | case .ended,.cancelled: 322 | 323 | let oldStroks = [SFStroke](self.strokes.prefix(self.strokes.count - 1)); 324 | 325 | (undoManager.prepare(withInvocationTarget: self) as AnyObject).handleUndoRedo(SFStrokeArrayBox(oldStroks), newStrokes: SFStrokeArrayBox(self.strokes)); 326 | 327 | 328 | 329 | undoManager.setActionName("Add Stroke") 330 | self.updateUndoAndRedoButtons(); 331 | 332 | self.currentPath = nil; 333 | 334 | break; 335 | 336 | default: 337 | break; 338 | } 339 | 340 | } 341 | 342 | 343 | @objc func handleUndoRedo(_ oldStrokes:SFStrokeArrayBox,newStrokes:SFStrokeArrayBox){ 344 | 345 | //add undo 346 | 347 | self.strokes = oldStrokes.unbox; 348 | self.canvasView.strokes = self.strokes; 349 | 350 | //add redo 351 | (undoManager.prepare(withInvocationTarget: self) as AnyObject).handleUndoRedo(newStrokes,newStrokes: oldStrokes); 352 | undoManager.setActionName("Remove Stroke") 353 | } 354 | 355 | 356 | 357 | 358 | 359 | @IBAction func onDoubleTaped(_ sender: AnyObject) { 360 | //self.canvasView.transform = CGAffineTransform(scaleX: 2, y: 2); 361 | 362 | if self.scrollView.zoomScale == self.scrollView.minimumZoomScale { 363 | self.scrollView.setZoomScale(self.scrollView.maximumZoomScale, animated: true); 364 | let s = NSString(format: "%.01f X",scrollView.maximumZoomScale); 365 | self.showInfo(info: s as String) 366 | }else{ 367 | self.scrollView.setZoomScale(self.scrollView.minimumZoomScale, animated: true); 368 | 369 | let s = NSString(format: "%.01f X",scrollView.minimumZoomScale); 370 | self.showInfo(info: s as String) 371 | 372 | 373 | } 374 | 375 | 376 | } 377 | 378 | 379 | private func createSnapshotImage() -> UIImage?{ 380 | UIGraphicsBeginImageContextWithOptions(self.canvasView.bounds.size, false, 0); 381 | self.canvasView.drawHierarchy(in: self.canvasView.bounds, afterScreenUpdates: true) 382 | let image = UIGraphicsGetImageFromCurrentImageContext(); 383 | return image; 384 | } 385 | 386 | 387 | @IBAction func onShare(_ sender: AnyObject) { 388 | 389 | 390 | 391 | if let image = self.createSnapshotImage() { 392 | let vc = UIActivityViewController(activityItems: [image], applicationActivities: []) 393 | vc.popoverPresentationController?.sourceView = self.shareBtn; 394 | vc.popoverPresentationController?.sourceRect = self.shareBtn.bounds; 395 | present(vc, animated: true, completion: nil) 396 | } 397 | 398 | } 399 | 400 | 401 | override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 402 | if let settingVC = segue.destination as? SFSettingVC{ 403 | 404 | settingVC.initColor = self.lineColor; 405 | settingVC.colorSelection = { [weak self] (color:UIColor) in 406 | self?.lineColor = color; 407 | } 408 | 409 | settingVC.initThickness = self.thickness; 410 | settingVC.thickneessSelection = { [weak self] (thickness:CGFloat) in 411 | self?.thickness = thickness; 412 | } 413 | 414 | settingVC.view.tintColor = tintColor; 415 | 416 | } 417 | } 418 | 419 | fileprivate func showInfo(info:String){ 420 | self.infoLabel.text = info; 421 | self.infoLabel.alpha = 1.0; 422 | delay(2) { [weak self] in 423 | if let this = self{ 424 | UIView.animate(withDuration: this.animationDuration, animations: { 425 | this.infoLabel.alpha = 0; 426 | }) 427 | } 428 | 429 | } 430 | } 431 | 432 | 433 | } 434 | 435 | extension SFZoomableCanvasVC:UIScrollViewDelegate{ 436 | func viewForZooming(in scrollView: UIScrollView) -> UIView? { 437 | 438 | return self.canvasView; 439 | } 440 | 441 | func scrollViewDidZoom(_ scrollView: UIScrollView) { 442 | 443 | self.modeBtn.isEnabled = scrollView.zoomScale > scrollView.minimumZoomScale; 444 | if self.modeBtn.isEnabled == false && self.mode == .positioning{ 445 | self.mode = .drawing; 446 | } 447 | 448 | let s = NSString(format: "%.01f X",scrollView.zoomScale); 449 | self.showInfo(info: s as String) 450 | 451 | } 452 | } 453 | 454 | -------------------------------------------------------------------------------- /SFZoomableCanvas/UIImage+SF.swift: -------------------------------------------------------------------------------- 1 | // 2 | // UIImageExtensions.swift 3 | // BlasterZonePlayer 4 | // 5 | // Created by CHENWANFEI on 2/9/16. 6 | // Copyright © 2016 Creative. All rights reserved. 7 | // 8 | import UIKit; 9 | 10 | extension UIImage{ 11 | 12 | func tintWithColor(color:UIColor)->UIImage { 13 | 14 | UIGraphicsBeginImageContextWithOptions(self.size,false,0) 15 | let context = UIGraphicsGetCurrentContext() 16 | 17 | // flip the image 18 | context!.scaleBy(x: 1.0, y: -1.0) 19 | context!.translateBy(x: 0.0, y: -self.size.height) 20 | 21 | // multiply blend mode 22 | context!.setBlendMode(CGBlendMode.multiply) 23 | 24 | let rect = CGRect(x:0, y:0, width:self.size.width, height:self.size.height) 25 | context!.clip(to: rect, mask: self.cgImage!) 26 | 27 | color.setFill() 28 | context!.fill(rect) 29 | 30 | // create uiimage 31 | let newImage = UIGraphicsGetImageFromCurrentImageContext() 32 | UIGraphicsEndImageContext() 33 | 34 | return newImage! 35 | 36 | } 37 | 38 | func crop(_ rect: CGRect) -> UIImage { 39 | var rect = rect 40 | rect.origin.x*=self.scale 41 | rect.origin.y*=self.scale 42 | rect.size.width*=self.scale 43 | rect.size.height*=self.scale 44 | 45 | let imageRef = self.cgImage!.cropping(to: rect) 46 | let image = UIImage(cgImage: imageRef!, scale: self.scale, orientation: self.imageOrientation) 47 | return image 48 | } 49 | 50 | static func fromColor(_ color: UIColor) -> UIImage { 51 | let rect = CGRect(x: 0, y: 0, width: 10, height: 10) 52 | UIGraphicsBeginImageContext(rect.size) 53 | let context = UIGraphicsGetCurrentContext() 54 | context!.setFillColor(color.cgColor) 55 | context!.fill(rect) 56 | let img = UIGraphicsGetImageFromCurrentImageContext() 57 | UIGraphicsEndImageContext() 58 | return img! 59 | } 60 | 61 | var imageToDisplayBasedOnOrientation:UIImage{ 62 | if self.imageOrientation == UIImageOrientation.up{ 63 | return self; 64 | } 65 | 66 | 67 | UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale); 68 | self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)); 69 | 70 | 71 | let normalizedImage = UIGraphicsGetImageFromCurrentImageContext(); 72 | UIGraphicsEndImageContext(); 73 | return normalizedImage!; 74 | } 75 | 76 | 77 | } 78 | --------------------------------------------------------------------------------