├── .gitignore ├── DrawPathView.podspec ├── DrawPathView.swift ├── DrawPathViewSwiftDemo ├── DrawPathViewSwiftDemo.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata ├── DrawPathViewSwiftDemo │ ├── AppDelegate.swift │ ├── Assets.xcassets │ │ ├── AppIcon.appiconset │ │ │ └── Contents.json │ │ └── Brand Assets.launchimage │ │ │ └── Contents.json │ ├── Base.lproj │ │ ├── LaunchScreen.storyboard │ │ └── Main.storyboard │ ├── DrawPathViewSwift │ │ └── DrawPathView.swift │ ├── Info.plist │ └── ViewController.swift └── DrawPathViewSwiftDemoTests │ ├── DrawPathViewSwiftDemoTests.swift │ └── Info.plist ├── LICENSE ├── README.md └── anim.gif /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | build/ 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | *.xccheckout 14 | *.moved-aside 15 | DerivedData 16 | *.hmap 17 | *.ipa 18 | *.xcuserstate 19 | 20 | # CocoaPods 21 | # 22 | # We recommend against adding the Pods directory to your .gitignore. However 23 | # you should judge for yourself, the pros and cons are mentioned at: 24 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 25 | # 26 | # Pods/ 27 | 28 | # Carthage 29 | # 30 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 31 | # Carthage/Checkouts 32 | 33 | Carthage/Build 34 | -------------------------------------------------------------------------------- /DrawPathView.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = "DrawPathView" 3 | s.version = "1.0.0" 4 | s.summary = "Drawable View written in pure Swift 4" 5 | s.description = <<-DESC 6 | Easy to use drawable custom View written in pure Swift 4. You can delete paths or delete last drawn path. You can also draw paths with any colors you want and change this color whenever you want. 7 | DESC 8 | 9 | s.homepage = "https://github.com/ahmetkgunay/DrawPathView" 10 | s.license = 'MIT' 11 | s.author = { "Ahmet Kazım Günay" => "ahmetkgunay@gmail.com" } 12 | s.source = { :git => "https://github.com/ahmetkgunay/DrawPathView.git", :tag => s.version.to_s } 13 | s.social_media_url = 'https://twitter.com/ahmtgny' 14 | s.platform = :ios, '8.0' 15 | s.requires_arc = true 16 | 17 | s.source_files = 'DrawPathView.swift' 18 | end 19 | -------------------------------------------------------------------------------- /DrawPathView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DrawPathView.swift 3 | // DrawPathView 4 | // 5 | // Created by Ahmet Kazım Günay on 08/10/15. 6 | // Copyright © 2015 Ahmet Kazım Günay. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @objc public protocol DrawPathViewDelegate { 12 | /// Triggered when user just started drawing 13 | @objc optional func viewDrawStartedDrawing() 14 | /// Triggered when user just finished drawing 15 | @objc optional func viewDrawEndedDrawing() 16 | } 17 | 18 | open class DrawPathView: UIView { 19 | 20 | /// A counter to determine if there are enough points to make a quadcurve 21 | fileprivate var ctr = 0 22 | 23 | /// The path to stroke 24 | fileprivate var path : UIBezierPath? 25 | 26 | /// After the user lifts their finger and the line has been finished the same line is rendered to an image and the UIBezierPath is cleared to prevent performance degradation when lots of lines are on screen 27 | fileprivate var incrementalImage : UIImage? 28 | 29 | /// Initial Image If user needs to draw lines on image firstly 30 | fileprivate var initialImage : UIImage? 31 | 32 | /// This array stores the points that make each line 33 | fileprivate lazy var pts = Array(repeating: nil, count: 5) 34 | 35 | open var delegate : DrawPathViewDelegate? 36 | 37 | /// Stroke color of drawing path, default is red. 38 | fileprivate var strokeColor = UIColor.red 39 | 40 | /// Stores all ımages to get back to last - 1 image. Becase erase last needs this :) 41 | fileprivate var allImages = Array() 42 | 43 | public var lineWidth: CGFloat = 2.0 { 44 | 45 | didSet { 46 | createPath() 47 | } 48 | } 49 | 50 | 51 | // MARK: - Initialize - 52 | 53 | required public init(coder aDecoder: NSCoder) { 54 | super.init(coder: aDecoder)! 55 | 56 | self.isMultipleTouchEnabled = true 57 | self.backgroundColor = UIColor.white 58 | createPath() 59 | } 60 | 61 | required override public init(frame: CGRect) { 62 | super.init(frame: frame) 63 | self.isMultipleTouchEnabled = true 64 | self.backgroundColor = UIColor.white 65 | createPath() 66 | } 67 | 68 | public init(initialImage: UIImage) { 69 | self.init() 70 | self.incrementalImage = initialImage 71 | self.initialImage = initialImage; 72 | self.isMultipleTouchEnabled = true 73 | self.backgroundColor = UIColor.white 74 | if let img = incrementalImage { 75 | img.draw(in: self.bounds) 76 | } 77 | createPath() 78 | } 79 | 80 | // MARK: - Setup - 81 | 82 | fileprivate func createPath() { 83 | path = nil 84 | path = UIBezierPath() 85 | path!.lineWidth = lineWidth 86 | } 87 | 88 | /// Erases All paths 89 | open func clearAll() { 90 | allImages.removeAll() 91 | ctr = 0 92 | path?.removeAllPoints() 93 | path = nil 94 | incrementalImage = initialImage 95 | createPath() 96 | setNeedsDisplay() 97 | } 98 | 99 | /// Erases Last Path 100 | open func clearLast() { 101 | if allImages.count == 0 { 102 | return 103 | } 104 | ctr = 0 105 | path?.removeAllPoints() 106 | path = nil 107 | allImages.removeLast() 108 | incrementalImage = allImages.last 109 | createPath() 110 | setNeedsDisplay() 111 | } 112 | 113 | // MARK: - Change Stroke Color - 114 | 115 | open func changeStrokeColor(_ color:UIColor!) { 116 | strokeColor = color 117 | } 118 | 119 | // MARK: - Draw Method - 120 | 121 | override open func draw(_ rect: CGRect) { 122 | if let img = incrementalImage { 123 | img.draw(in: rect) 124 | strokeColor.setStroke() 125 | if let pth = path { 126 | pth.stroke() 127 | } 128 | } else { 129 | let rectPth = UIBezierPath(rect: self.bounds) 130 | UIColor.white.setFill() 131 | rectPth.fill() 132 | } 133 | } 134 | 135 | // MARK: - Touch Events - 136 | 137 | override open func touchesBegan(_ touches: Set, with event: UIEvent?) { 138 | 139 | delegate?.viewDrawStartedDrawing?() 140 | 141 | ctr = 0 142 | let touch = touches.first 143 | let p = (touch?.location(in: self))! 144 | pts[0] = p 145 | if let pth = path { 146 | pth.move(to: p) 147 | } 148 | drawBitmap(false) 149 | } 150 | 151 | override open func touchesMoved(_ touches: Set, with event: UIEvent?) { 152 | 153 | let touch = touches.first 154 | let p = (touch?.location(in: self))! 155 | ctr += 1 156 | pts[ctr] = p 157 | 158 | if ctr == 4 { 159 | // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment 160 | pts[3] = CGPoint(x: (pts[2].x + pts[4].x)/2.0, y: (pts[2].y + pts[4].y)/2.0) 161 | if let pth = path { 162 | pth.move(to: pts[0]) 163 | pth.addCurve(to: pts[3], controlPoint1: pts[1], controlPoint2: pts[2]) 164 | } 165 | setNeedsDisplay() 166 | pts[0] = pts[3] 167 | pts[1] = pts[4] 168 | ctr = 1 169 | } 170 | } 171 | 172 | override open func touchesCancelled(_ touches: Set, with event: UIEvent?) { 173 | touchesEnded(touches, with: event) 174 | } 175 | 176 | override open func touchesEnded(_ touches: Set, with event: UIEvent?) { 177 | 178 | delegate?.viewDrawEndedDrawing?() 179 | drawBitmap(true) 180 | setNeedsDisplay() 181 | if let pth = path { 182 | pth.removeAllPoints() 183 | } 184 | ctr = 0 185 | } 186 | 187 | // MARK: - Bitmap - 188 | 189 | fileprivate func drawBitmap(_ endDrawing:Bool) { 190 | UIGraphicsBeginImageContextWithOptions(self.bounds.size, true, 0.0) 191 | draw(self.bounds) 192 | if let pth = path { 193 | pth.stroke() 194 | } 195 | incrementalImage = UIGraphicsGetImageFromCurrentImageContext() 196 | if endDrawing { 197 | if let _ = incrementalImage { 198 | allImages.append(incrementalImage!) 199 | } 200 | } 201 | UIGraphicsEndImageContext() 202 | } 203 | } 204 | -------------------------------------------------------------------------------- /DrawPathViewSwiftDemo/DrawPathViewSwiftDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 72FCA4561C2060AD00D84813 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 72FCA4551C2060AD00D84813 /* AppDelegate.swift */; }; 11 | 72FCA4581C2060AD00D84813 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 72FCA4571C2060AD00D84813 /* ViewController.swift */; }; 12 | 72FCA45B1C2060AD00D84813 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 72FCA4591C2060AD00D84813 /* Main.storyboard */; }; 13 | 72FCA45D1C2060AD00D84813 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 72FCA45C1C2060AD00D84813 /* Assets.xcassets */; }; 14 | 72FCA4601C2060AD00D84813 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 72FCA45E1C2060AD00D84813 /* LaunchScreen.storyboard */; }; 15 | 72FCA46B1C2060AD00D84813 /* DrawPathViewSwiftDemoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 72FCA46A1C2060AD00D84813 /* DrawPathViewSwiftDemoTests.swift */; }; 16 | 72FCA4771C20611800D84813 /* DrawPathView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 72FCA4761C20611800D84813 /* DrawPathView.swift */; }; 17 | 72FCA4781C20611800D84813 /* DrawPathView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 72FCA4761C20611800D84813 /* DrawPathView.swift */; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXContainerItemProxy section */ 21 | 72FCA4671C2060AD00D84813 /* PBXContainerItemProxy */ = { 22 | isa = PBXContainerItemProxy; 23 | containerPortal = 72FCA44A1C2060AD00D84813 /* Project object */; 24 | proxyType = 1; 25 | remoteGlobalIDString = 72FCA4511C2060AD00D84813; 26 | remoteInfo = DrawPathViewSwiftDemo; 27 | }; 28 | /* End PBXContainerItemProxy section */ 29 | 30 | /* Begin PBXFileReference section */ 31 | 72FCA4521C2060AD00D84813 /* DrawPathViewSwiftDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DrawPathViewSwiftDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 32 | 72FCA4551C2060AD00D84813 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 33 | 72FCA4571C2060AD00D84813 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 34 | 72FCA45A1C2060AD00D84813 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 35 | 72FCA45C1C2060AD00D84813 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 36 | 72FCA45F1C2060AD00D84813 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 37 | 72FCA4611C2060AD00D84813 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 38 | 72FCA4661C2060AD00D84813 /* DrawPathViewSwiftDemoTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = DrawPathViewSwiftDemoTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 39 | 72FCA46A1C2060AD00D84813 /* DrawPathViewSwiftDemoTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DrawPathViewSwiftDemoTests.swift; sourceTree = ""; }; 40 | 72FCA46C1C2060AD00D84813 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 41 | 72FCA4761C20611800D84813 /* DrawPathView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DrawPathView.swift; sourceTree = ""; }; 42 | /* End PBXFileReference section */ 43 | 44 | /* Begin PBXFrameworksBuildPhase section */ 45 | 72FCA44F1C2060AD00D84813 /* Frameworks */ = { 46 | isa = PBXFrameworksBuildPhase; 47 | buildActionMask = 2147483647; 48 | files = ( 49 | ); 50 | runOnlyForDeploymentPostprocessing = 0; 51 | }; 52 | 72FCA4631C2060AD00D84813 /* Frameworks */ = { 53 | isa = PBXFrameworksBuildPhase; 54 | buildActionMask = 2147483647; 55 | files = ( 56 | ); 57 | runOnlyForDeploymentPostprocessing = 0; 58 | }; 59 | /* End PBXFrameworksBuildPhase section */ 60 | 61 | /* Begin PBXGroup section */ 62 | 72FCA4491C2060AD00D84813 = { 63 | isa = PBXGroup; 64 | children = ( 65 | 72FCA4541C2060AD00D84813 /* DrawPathViewSwiftDemo */, 66 | 72FCA4691C2060AD00D84813 /* DrawPathViewSwiftDemoTests */, 67 | 72FCA4531C2060AD00D84813 /* Products */, 68 | ); 69 | sourceTree = ""; 70 | }; 71 | 72FCA4531C2060AD00D84813 /* Products */ = { 72 | isa = PBXGroup; 73 | children = ( 74 | 72FCA4521C2060AD00D84813 /* DrawPathViewSwiftDemo.app */, 75 | 72FCA4661C2060AD00D84813 /* DrawPathViewSwiftDemoTests.xctest */, 76 | ); 77 | name = Products; 78 | sourceTree = ""; 79 | }; 80 | 72FCA4541C2060AD00D84813 /* DrawPathViewSwiftDemo */ = { 81 | isa = PBXGroup; 82 | children = ( 83 | 72FCA4751C2060E100D84813 /* DrawPathViewSwift */, 84 | 72FCA4551C2060AD00D84813 /* AppDelegate.swift */, 85 | 72FCA4571C2060AD00D84813 /* ViewController.swift */, 86 | 72FCA4591C2060AD00D84813 /* Main.storyboard */, 87 | 72FCA45C1C2060AD00D84813 /* Assets.xcassets */, 88 | 72FCA45E1C2060AD00D84813 /* LaunchScreen.storyboard */, 89 | 72FCA4611C2060AD00D84813 /* Info.plist */, 90 | ); 91 | path = DrawPathViewSwiftDemo; 92 | sourceTree = ""; 93 | }; 94 | 72FCA4691C2060AD00D84813 /* DrawPathViewSwiftDemoTests */ = { 95 | isa = PBXGroup; 96 | children = ( 97 | 72FCA46A1C2060AD00D84813 /* DrawPathViewSwiftDemoTests.swift */, 98 | 72FCA46C1C2060AD00D84813 /* Info.plist */, 99 | ); 100 | path = DrawPathViewSwiftDemoTests; 101 | sourceTree = ""; 102 | }; 103 | 72FCA4751C2060E100D84813 /* DrawPathViewSwift */ = { 104 | isa = PBXGroup; 105 | children = ( 106 | 72FCA4761C20611800D84813 /* DrawPathView.swift */, 107 | ); 108 | path = DrawPathViewSwift; 109 | sourceTree = ""; 110 | }; 111 | /* End PBXGroup section */ 112 | 113 | /* Begin PBXNativeTarget section */ 114 | 72FCA4511C2060AD00D84813 /* DrawPathViewSwiftDemo */ = { 115 | isa = PBXNativeTarget; 116 | buildConfigurationList = 72FCA46F1C2060AD00D84813 /* Build configuration list for PBXNativeTarget "DrawPathViewSwiftDemo" */; 117 | buildPhases = ( 118 | 72FCA44E1C2060AD00D84813 /* Sources */, 119 | 72FCA44F1C2060AD00D84813 /* Frameworks */, 120 | 72FCA4501C2060AD00D84813 /* Resources */, 121 | ); 122 | buildRules = ( 123 | ); 124 | dependencies = ( 125 | ); 126 | name = DrawPathViewSwiftDemo; 127 | productName = DrawPathViewSwiftDemo; 128 | productReference = 72FCA4521C2060AD00D84813 /* DrawPathViewSwiftDemo.app */; 129 | productType = "com.apple.product-type.application"; 130 | }; 131 | 72FCA4651C2060AD00D84813 /* DrawPathViewSwiftDemoTests */ = { 132 | isa = PBXNativeTarget; 133 | buildConfigurationList = 72FCA4721C2060AD00D84813 /* Build configuration list for PBXNativeTarget "DrawPathViewSwiftDemoTests" */; 134 | buildPhases = ( 135 | 72FCA4621C2060AD00D84813 /* Sources */, 136 | 72FCA4631C2060AD00D84813 /* Frameworks */, 137 | 72FCA4641C2060AD00D84813 /* Resources */, 138 | ); 139 | buildRules = ( 140 | ); 141 | dependencies = ( 142 | 72FCA4681C2060AD00D84813 /* PBXTargetDependency */, 143 | ); 144 | name = DrawPathViewSwiftDemoTests; 145 | productName = DrawPathViewSwiftDemoTests; 146 | productReference = 72FCA4661C2060AD00D84813 /* DrawPathViewSwiftDemoTests.xctest */; 147 | productType = "com.apple.product-type.bundle.unit-test"; 148 | }; 149 | /* End PBXNativeTarget section */ 150 | 151 | /* Begin PBXProject section */ 152 | 72FCA44A1C2060AD00D84813 /* Project object */ = { 153 | isa = PBXProject; 154 | attributes = { 155 | LastSwiftUpdateCheck = 0720; 156 | LastUpgradeCheck = 0900; 157 | ORGANIZATIONNAME = "Ahmet Kazim Günay"; 158 | TargetAttributes = { 159 | 72FCA4511C2060AD00D84813 = { 160 | CreatedOnToolsVersion = 7.2; 161 | LastSwiftMigration = 0900; 162 | }; 163 | 72FCA4651C2060AD00D84813 = { 164 | CreatedOnToolsVersion = 7.2; 165 | LastSwiftMigration = 0900; 166 | TestTargetID = 72FCA4511C2060AD00D84813; 167 | }; 168 | }; 169 | }; 170 | buildConfigurationList = 72FCA44D1C2060AD00D84813 /* Build configuration list for PBXProject "DrawPathViewSwiftDemo" */; 171 | compatibilityVersion = "Xcode 3.2"; 172 | developmentRegion = English; 173 | hasScannedForEncodings = 0; 174 | knownRegions = ( 175 | en, 176 | Base, 177 | ); 178 | mainGroup = 72FCA4491C2060AD00D84813; 179 | productRefGroup = 72FCA4531C2060AD00D84813 /* Products */; 180 | projectDirPath = ""; 181 | projectRoot = ""; 182 | targets = ( 183 | 72FCA4511C2060AD00D84813 /* DrawPathViewSwiftDemo */, 184 | 72FCA4651C2060AD00D84813 /* DrawPathViewSwiftDemoTests */, 185 | ); 186 | }; 187 | /* End PBXProject section */ 188 | 189 | /* Begin PBXResourcesBuildPhase section */ 190 | 72FCA4501C2060AD00D84813 /* Resources */ = { 191 | isa = PBXResourcesBuildPhase; 192 | buildActionMask = 2147483647; 193 | files = ( 194 | 72FCA4601C2060AD00D84813 /* LaunchScreen.storyboard in Resources */, 195 | 72FCA45D1C2060AD00D84813 /* Assets.xcassets in Resources */, 196 | 72FCA45B1C2060AD00D84813 /* Main.storyboard in Resources */, 197 | ); 198 | runOnlyForDeploymentPostprocessing = 0; 199 | }; 200 | 72FCA4641C2060AD00D84813 /* Resources */ = { 201 | isa = PBXResourcesBuildPhase; 202 | buildActionMask = 2147483647; 203 | files = ( 204 | ); 205 | runOnlyForDeploymentPostprocessing = 0; 206 | }; 207 | /* End PBXResourcesBuildPhase section */ 208 | 209 | /* Begin PBXSourcesBuildPhase section */ 210 | 72FCA44E1C2060AD00D84813 /* Sources */ = { 211 | isa = PBXSourcesBuildPhase; 212 | buildActionMask = 2147483647; 213 | files = ( 214 | 72FCA4771C20611800D84813 /* DrawPathView.swift in Sources */, 215 | 72FCA4581C2060AD00D84813 /* ViewController.swift in Sources */, 216 | 72FCA4561C2060AD00D84813 /* AppDelegate.swift in Sources */, 217 | ); 218 | runOnlyForDeploymentPostprocessing = 0; 219 | }; 220 | 72FCA4621C2060AD00D84813 /* Sources */ = { 221 | isa = PBXSourcesBuildPhase; 222 | buildActionMask = 2147483647; 223 | files = ( 224 | 72FCA46B1C2060AD00D84813 /* DrawPathViewSwiftDemoTests.swift in Sources */, 225 | 72FCA4781C20611800D84813 /* DrawPathView.swift in Sources */, 226 | ); 227 | runOnlyForDeploymentPostprocessing = 0; 228 | }; 229 | /* End PBXSourcesBuildPhase section */ 230 | 231 | /* Begin PBXTargetDependency section */ 232 | 72FCA4681C2060AD00D84813 /* PBXTargetDependency */ = { 233 | isa = PBXTargetDependency; 234 | target = 72FCA4511C2060AD00D84813 /* DrawPathViewSwiftDemo */; 235 | targetProxy = 72FCA4671C2060AD00D84813 /* PBXContainerItemProxy */; 236 | }; 237 | /* End PBXTargetDependency section */ 238 | 239 | /* Begin PBXVariantGroup section */ 240 | 72FCA4591C2060AD00D84813 /* Main.storyboard */ = { 241 | isa = PBXVariantGroup; 242 | children = ( 243 | 72FCA45A1C2060AD00D84813 /* Base */, 244 | ); 245 | name = Main.storyboard; 246 | sourceTree = ""; 247 | }; 248 | 72FCA45E1C2060AD00D84813 /* LaunchScreen.storyboard */ = { 249 | isa = PBXVariantGroup; 250 | children = ( 251 | 72FCA45F1C2060AD00D84813 /* Base */, 252 | ); 253 | name = LaunchScreen.storyboard; 254 | sourceTree = ""; 255 | }; 256 | /* End PBXVariantGroup section */ 257 | 258 | /* Begin XCBuildConfiguration section */ 259 | 72FCA46D1C2060AD00D84813 /* Debug */ = { 260 | isa = XCBuildConfiguration; 261 | buildSettings = { 262 | ALWAYS_SEARCH_USER_PATHS = NO; 263 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 264 | CLANG_CXX_LIBRARY = "libc++"; 265 | CLANG_ENABLE_MODULES = YES; 266 | CLANG_ENABLE_OBJC_ARC = YES; 267 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 268 | CLANG_WARN_BOOL_CONVERSION = YES; 269 | CLANG_WARN_COMMA = YES; 270 | CLANG_WARN_CONSTANT_CONVERSION = YES; 271 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 272 | CLANG_WARN_EMPTY_BODY = YES; 273 | CLANG_WARN_ENUM_CONVERSION = YES; 274 | CLANG_WARN_INFINITE_RECURSION = YES; 275 | CLANG_WARN_INT_CONVERSION = YES; 276 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 277 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 278 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 279 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 280 | CLANG_WARN_STRICT_PROTOTYPES = YES; 281 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 282 | CLANG_WARN_UNREACHABLE_CODE = YES; 283 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 284 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 285 | COPY_PHASE_STRIP = NO; 286 | DEBUG_INFORMATION_FORMAT = dwarf; 287 | ENABLE_STRICT_OBJC_MSGSEND = YES; 288 | ENABLE_TESTABILITY = YES; 289 | GCC_C_LANGUAGE_STANDARD = gnu99; 290 | GCC_DYNAMIC_NO_PIC = NO; 291 | GCC_NO_COMMON_BLOCKS = YES; 292 | GCC_OPTIMIZATION_LEVEL = 0; 293 | GCC_PREPROCESSOR_DEFINITIONS = ( 294 | "DEBUG=1", 295 | "$(inherited)", 296 | ); 297 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 298 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 299 | GCC_WARN_UNDECLARED_SELECTOR = YES; 300 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 301 | GCC_WARN_UNUSED_FUNCTION = YES; 302 | GCC_WARN_UNUSED_VARIABLE = YES; 303 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 304 | MTL_ENABLE_DEBUG_INFO = YES; 305 | ONLY_ACTIVE_ARCH = YES; 306 | SDKROOT = iphoneos; 307 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 308 | TARGETED_DEVICE_FAMILY = "1,2"; 309 | }; 310 | name = Debug; 311 | }; 312 | 72FCA46E1C2060AD00D84813 /* Release */ = { 313 | isa = XCBuildConfiguration; 314 | buildSettings = { 315 | ALWAYS_SEARCH_USER_PATHS = NO; 316 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 317 | CLANG_CXX_LIBRARY = "libc++"; 318 | CLANG_ENABLE_MODULES = YES; 319 | CLANG_ENABLE_OBJC_ARC = YES; 320 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 321 | CLANG_WARN_BOOL_CONVERSION = YES; 322 | CLANG_WARN_COMMA = YES; 323 | CLANG_WARN_CONSTANT_CONVERSION = YES; 324 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 325 | CLANG_WARN_EMPTY_BODY = YES; 326 | CLANG_WARN_ENUM_CONVERSION = YES; 327 | CLANG_WARN_INFINITE_RECURSION = YES; 328 | CLANG_WARN_INT_CONVERSION = YES; 329 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 330 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 331 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 332 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 333 | CLANG_WARN_STRICT_PROTOTYPES = YES; 334 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 335 | CLANG_WARN_UNREACHABLE_CODE = YES; 336 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 337 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 338 | COPY_PHASE_STRIP = NO; 339 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 340 | ENABLE_NS_ASSERTIONS = NO; 341 | ENABLE_STRICT_OBJC_MSGSEND = YES; 342 | GCC_C_LANGUAGE_STANDARD = gnu99; 343 | GCC_NO_COMMON_BLOCKS = YES; 344 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 345 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 346 | GCC_WARN_UNDECLARED_SELECTOR = YES; 347 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 348 | GCC_WARN_UNUSED_FUNCTION = YES; 349 | GCC_WARN_UNUSED_VARIABLE = YES; 350 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 351 | MTL_ENABLE_DEBUG_INFO = NO; 352 | SDKROOT = iphoneos; 353 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 354 | TARGETED_DEVICE_FAMILY = "1,2"; 355 | VALIDATE_PRODUCT = YES; 356 | }; 357 | name = Release; 358 | }; 359 | 72FCA4701C2060AD00D84813 /* Debug */ = { 360 | isa = XCBuildConfiguration; 361 | buildSettings = { 362 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 363 | ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = "Brand Assets"; 364 | INFOPLIST_FILE = DrawPathViewSwiftDemo/Info.plist; 365 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 366 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 367 | PRODUCT_BUNDLE_IDENTIFIER = com.mobven.DrawPathViewSwiftDemo; 368 | PRODUCT_NAME = "$(TARGET_NAME)"; 369 | SWIFT_SWIFT3_OBJC_INFERENCE = Default; 370 | SWIFT_VERSION = 4.0; 371 | }; 372 | name = Debug; 373 | }; 374 | 72FCA4711C2060AD00D84813 /* Release */ = { 375 | isa = XCBuildConfiguration; 376 | buildSettings = { 377 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 378 | ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = "Brand Assets"; 379 | INFOPLIST_FILE = DrawPathViewSwiftDemo/Info.plist; 380 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 381 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 382 | PRODUCT_BUNDLE_IDENTIFIER = com.mobven.DrawPathViewSwiftDemo; 383 | PRODUCT_NAME = "$(TARGET_NAME)"; 384 | SWIFT_SWIFT3_OBJC_INFERENCE = Default; 385 | SWIFT_VERSION = 4.0; 386 | }; 387 | name = Release; 388 | }; 389 | 72FCA4731C2060AD00D84813 /* Debug */ = { 390 | isa = XCBuildConfiguration; 391 | buildSettings = { 392 | BUNDLE_LOADER = "$(TEST_HOST)"; 393 | INFOPLIST_FILE = DrawPathViewSwiftDemoTests/Info.plist; 394 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 395 | PRODUCT_BUNDLE_IDENTIFIER = com.mobven.DrawPathViewSwiftDemoTests; 396 | PRODUCT_NAME = "$(TARGET_NAME)"; 397 | SWIFT_SWIFT3_OBJC_INFERENCE = On; 398 | SWIFT_VERSION = 4.0; 399 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/DrawPathViewSwiftDemo.app/DrawPathViewSwiftDemo"; 400 | }; 401 | name = Debug; 402 | }; 403 | 72FCA4741C2060AD00D84813 /* Release */ = { 404 | isa = XCBuildConfiguration; 405 | buildSettings = { 406 | BUNDLE_LOADER = "$(TEST_HOST)"; 407 | INFOPLIST_FILE = DrawPathViewSwiftDemoTests/Info.plist; 408 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 409 | PRODUCT_BUNDLE_IDENTIFIER = com.mobven.DrawPathViewSwiftDemoTests; 410 | PRODUCT_NAME = "$(TARGET_NAME)"; 411 | SWIFT_SWIFT3_OBJC_INFERENCE = On; 412 | SWIFT_VERSION = 4.0; 413 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/DrawPathViewSwiftDemo.app/DrawPathViewSwiftDemo"; 414 | }; 415 | name = Release; 416 | }; 417 | /* End XCBuildConfiguration section */ 418 | 419 | /* Begin XCConfigurationList section */ 420 | 72FCA44D1C2060AD00D84813 /* Build configuration list for PBXProject "DrawPathViewSwiftDemo" */ = { 421 | isa = XCConfigurationList; 422 | buildConfigurations = ( 423 | 72FCA46D1C2060AD00D84813 /* Debug */, 424 | 72FCA46E1C2060AD00D84813 /* Release */, 425 | ); 426 | defaultConfigurationIsVisible = 0; 427 | defaultConfigurationName = Release; 428 | }; 429 | 72FCA46F1C2060AD00D84813 /* Build configuration list for PBXNativeTarget "DrawPathViewSwiftDemo" */ = { 430 | isa = XCConfigurationList; 431 | buildConfigurations = ( 432 | 72FCA4701C2060AD00D84813 /* Debug */, 433 | 72FCA4711C2060AD00D84813 /* Release */, 434 | ); 435 | defaultConfigurationIsVisible = 0; 436 | defaultConfigurationName = Release; 437 | }; 438 | 72FCA4721C2060AD00D84813 /* Build configuration list for PBXNativeTarget "DrawPathViewSwiftDemoTests" */ = { 439 | isa = XCConfigurationList; 440 | buildConfigurations = ( 441 | 72FCA4731C2060AD00D84813 /* Debug */, 442 | 72FCA4741C2060AD00D84813 /* Release */, 443 | ); 444 | defaultConfigurationIsVisible = 0; 445 | defaultConfigurationName = Release; 446 | }; 447 | /* End XCConfigurationList section */ 448 | }; 449 | rootObject = 72FCA44A1C2060AD00D84813 /* Project object */; 450 | } 451 | -------------------------------------------------------------------------------- /DrawPathViewSwiftDemo/DrawPathViewSwiftDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DrawPathViewSwiftDemo/DrawPathViewSwiftDemo/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // DrawPathViewSwiftDemo 4 | // 5 | // Created by Ahmet Kazim Günay on 15/12/15. 6 | // Copyright © 2015 Ahmet Kazim Günay. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | private func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // 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. 24 | // 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. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // 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. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // 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. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /DrawPathViewSwiftDemo/DrawPathViewSwiftDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /DrawPathViewSwiftDemo/DrawPathViewSwiftDemo/Assets.xcassets/Brand Assets.launchimage/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "orientation" : "portrait", 5 | "idiom" : "ipad", 6 | "minimum-system-version" : "7.0", 7 | "extent" : "full-screen", 8 | "scale" : "2x" 9 | }, 10 | { 11 | "orientation" : "landscape", 12 | "idiom" : "ipad", 13 | "minimum-system-version" : "7.0", 14 | "extent" : "full-screen", 15 | "scale" : "1x" 16 | }, 17 | { 18 | "orientation" : "landscape", 19 | "idiom" : "ipad", 20 | "minimum-system-version" : "7.0", 21 | "extent" : "full-screen", 22 | "scale" : "2x" 23 | }, 24 | { 25 | "orientation" : "portrait", 26 | "idiom" : "iphone", 27 | "minimum-system-version" : "7.0", 28 | "scale" : "2x" 29 | }, 30 | { 31 | "orientation" : "portrait", 32 | "idiom" : "iphone", 33 | "minimum-system-version" : "7.0", 34 | "subtype" : "retina4", 35 | "scale" : "2x" 36 | }, 37 | { 38 | "orientation" : "portrait", 39 | "idiom" : "ipad", 40 | "minimum-system-version" : "7.0", 41 | "extent" : "full-screen", 42 | "scale" : "1x" 43 | } 44 | ], 45 | "info" : { 46 | "version" : 1, 47 | "author" : "xcode" 48 | } 49 | } -------------------------------------------------------------------------------- /DrawPathViewSwiftDemo/DrawPathViewSwiftDemo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /DrawPathViewSwiftDemo/DrawPathViewSwiftDemo/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 | -------------------------------------------------------------------------------- /DrawPathViewSwiftDemo/DrawPathViewSwiftDemo/DrawPathViewSwift/DrawPathView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DrawPathView.swift 3 | // DrawPathView 4 | // 5 | // Created by Ahmet Kazım Günay on 08/10/15. 6 | // Copyright © 2015 Ahmet Kazım Günay. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @objc public protocol DrawPathViewDelegate { 12 | /// Triggered when user just started drawing 13 | @objc optional func viewDrawStartedDrawing() 14 | /// Triggered when user just finished drawing 15 | @objc optional func viewDrawEndedDrawing() 16 | } 17 | 18 | open class DrawPathView: UIView { 19 | 20 | /// A counter to determine if there are enough points to make a quadcurve 21 | fileprivate var ctr = 0 22 | 23 | /// The path to stroke 24 | fileprivate var path : UIBezierPath? 25 | 26 | /// After the user lifts their finger and the line has been finished the same line is rendered to an image and the UIBezierPath is cleared to prevent performance degradation when lots of lines are on screen 27 | fileprivate var incrementalImage : UIImage? 28 | 29 | /// Initial Image If user needs to draw lines on image firstly 30 | fileprivate var initialImage : UIImage? 31 | 32 | /// This array stores the points that make each line 33 | fileprivate lazy var pts = Array(repeating: nil, count: 5) 34 | 35 | open var delegate : DrawPathViewDelegate? 36 | 37 | /// Stroke color of drawing path, default is red. 38 | fileprivate var strokeColor = UIColor.red 39 | 40 | /// Stores all ımages to get back to last - 1 image. Becase erase last needs this :) 41 | fileprivate var allImages = Array() 42 | 43 | public var lineWidth: CGFloat = 2.0 { 44 | 45 | didSet { 46 | createPath() 47 | } 48 | } 49 | 50 | 51 | // MARK: - Initialize - 52 | 53 | required public init(coder aDecoder: NSCoder) { 54 | super.init(coder: aDecoder)! 55 | 56 | self.isMultipleTouchEnabled = true 57 | self.backgroundColor = UIColor.white 58 | createPath() 59 | } 60 | 61 | required override public init(frame: CGRect) { 62 | super.init(frame: frame) 63 | self.isMultipleTouchEnabled = true 64 | self.backgroundColor = UIColor.white 65 | createPath() 66 | } 67 | 68 | public init(initialImage: UIImage) { 69 | self.init() 70 | self.incrementalImage = initialImage 71 | self.initialImage = initialImage; 72 | self.isMultipleTouchEnabled = true 73 | self.backgroundColor = UIColor.white 74 | if let img = incrementalImage { 75 | img.draw(in: self.bounds) 76 | } 77 | createPath() 78 | } 79 | 80 | // MARK: - Setup - 81 | 82 | fileprivate func createPath() { 83 | path = nil 84 | path = UIBezierPath() 85 | path!.lineWidth = lineWidth 86 | } 87 | 88 | /// Erases All paths 89 | open func clearAll() { 90 | allImages.removeAll() 91 | ctr = 0 92 | path?.removeAllPoints() 93 | path = nil 94 | incrementalImage = initialImage 95 | createPath() 96 | setNeedsDisplay() 97 | } 98 | 99 | /// Erases Last Path 100 | open func clearLast() { 101 | if allImages.count == 0 { 102 | return 103 | } 104 | ctr = 0 105 | path?.removeAllPoints() 106 | path = nil 107 | allImages.removeLast() 108 | incrementalImage = allImages.last 109 | createPath() 110 | setNeedsDisplay() 111 | } 112 | 113 | // MARK: - Change Stroke Color - 114 | 115 | open func changeStrokeColor(_ color:UIColor!) { 116 | strokeColor = color 117 | } 118 | 119 | // MARK: - Draw Method - 120 | 121 | override open func draw(_ rect: CGRect) { 122 | if let img = incrementalImage { 123 | img.draw(in: rect) 124 | strokeColor.setStroke() 125 | if let pth = path { 126 | pth.stroke() 127 | } 128 | } else { 129 | let rectPth = UIBezierPath(rect: self.bounds) 130 | UIColor.white.setFill() 131 | rectPth.fill() 132 | } 133 | } 134 | 135 | // MARK: - Touch Events - 136 | 137 | override open func touchesBegan(_ touches: Set, with event: UIEvent?) { 138 | 139 | delegate?.viewDrawStartedDrawing?() 140 | 141 | ctr = 0 142 | let touch = touches.first 143 | let p = (touch?.location(in: self))! 144 | pts[0] = p 145 | if let pth = path { 146 | pth.move(to: p) 147 | } 148 | drawBitmap(false) 149 | } 150 | 151 | override open func touchesMoved(_ touches: Set, with event: UIEvent?) { 152 | 153 | let touch = touches.first 154 | let p = (touch?.location(in: self))! 155 | ctr += 1 156 | pts[ctr] = p 157 | 158 | if ctr == 4 { 159 | // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment 160 | pts[3] = CGPoint(x: (pts[2].x + pts[4].x)/2.0, y: (pts[2].y + pts[4].y)/2.0) 161 | if let pth = path { 162 | pth.move(to: pts[0]) 163 | pth.addCurve(to: pts[3], controlPoint1: pts[1], controlPoint2: pts[2]) 164 | } 165 | setNeedsDisplay() 166 | pts[0] = pts[3] 167 | pts[1] = pts[4] 168 | ctr = 1 169 | } 170 | } 171 | 172 | override open func touchesCancelled(_ touches: Set, with event: UIEvent?) { 173 | touchesEnded(touches, with: event) 174 | } 175 | 176 | override open func touchesEnded(_ touches: Set, with event: UIEvent?) { 177 | 178 | delegate?.viewDrawEndedDrawing?() 179 | drawBitmap(true) 180 | setNeedsDisplay() 181 | if let pth = path { 182 | pth.removeAllPoints() 183 | } 184 | ctr = 0 185 | } 186 | 187 | // MARK: - Bitmap - 188 | 189 | fileprivate func drawBitmap(_ endDrawing:Bool) { 190 | UIGraphicsBeginImageContextWithOptions(self.bounds.size, true, 0.0) 191 | draw(self.bounds) 192 | if let pth = path { 193 | pth.stroke() 194 | } 195 | incrementalImage = UIGraphicsGetImageFromCurrentImageContext() 196 | if endDrawing { 197 | if let _ = incrementalImage { 198 | allImages.append(incrementalImage!) 199 | } 200 | } 201 | UIGraphicsEndImageContext() 202 | } 203 | } 204 | -------------------------------------------------------------------------------- /DrawPathViewSwiftDemo/DrawPathViewSwiftDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /DrawPathViewSwiftDemo/DrawPathViewSwiftDemo/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // DrawPathViewSwiftDemo 4 | // 5 | // Created by Ahmet Kazim Günay on 15/12/15. 6 | // Copyright © 2015 Ahmet Kazim Günay. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController, DrawPathViewDelegate { 12 | 13 | lazy var drawView : DrawPathView = { 14 | let dv = DrawPathView(frame: self.view.bounds) 15 | dv.lineWidth = 10.0 16 | dv.delegate = self 17 | return dv 18 | }() 19 | 20 | final let selectedBorderWidth = CGFloat(3) 21 | final let bottomViewHeight = CGFloat(44) 22 | final let buttonHeight = CGFloat(40) 23 | final let colors = [UIColor.red, .green, .orange, .yellow, .purple] 24 | 25 | // MARK: - View lifecycle - 26 | 27 | override func viewDidLoad() { 28 | super.viewDidLoad() 29 | view.addSubview(drawView) 30 | addBottomButtons() 31 | addTopButtons() 32 | } 33 | 34 | // MARK: - Private - 35 | 36 | private func addBottomButtons() { 37 | 38 | for i in 0.. 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 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Ahmet Günay 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 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DrawPathView 2 | [![Version](https://img.shields.io/cocoapods/v/DrawPathView.svg?style=flat)](http://cocoapods.org/pods/DrawPathView) 3 | [![License](https://img.shields.io/cocoapods/l/DrawPathView.svg?style=flat)](http://cocoapods.org/pods/DrawPathView) 4 | [![Platform](https://img.shields.io/cocoapods/p/DrawPathView.svg?style=flat)](http://cocoapods.org/pods/DrawPathView) 5 | 6 | Drawable View with any colors you want to fill and can be erased last path or all paths anytime 7 | 8 | ![Anim](https://github.com/ahmetkgunay/DrawPathView/blob/master/anim.gif) 9 | 10 | ## Usage 11 | 12 | Usage is simple, you can just add as a subview DrawPathView which is inheritted from UIView to any Custom view. 13 | 14 | ```swift 15 | 16 | lazy var drawView : DrawPathView = { 17 | let dv = DrawPathView(frame: self.view.bounds) 18 | dv.lineWidth = 10.0 19 | dv.delegate = self 20 | return dv 21 | }() 22 | 23 | override func viewDidLoad() { 24 | super.viewDidLoad() 25 | view.addSubview(drawView) 26 | } 27 | ``` 28 | 29 | User also can delete paths as last drawn path or all paths at the same time. 30 | 31 | ```swift 32 | internal func eraseLast() { 33 | drawView.clearLast() 34 | } 35 | 36 | internal func eraseAll() { 37 | drawView.clearAll() 38 | } 39 | ``` 40 | #### Delegates 41 | 42 | DrawPathView has also two delegate methods : 43 | 44 | - viewDrawStartedDrawing : Calls when user started drawing 45 | - viewDrawEndedDrawing : Calls when user ended drawing 46 | 47 | 48 | ```swift 49 | 50 | // MARK: - DrawPathView Delegate - 51 | 52 | func viewDrawStartedDrawing() { 53 | print("Started Drawing") 54 | } 55 | 56 | func viewDrawEndedDrawing() { 57 | print("Ended Drawing") 58 | } 59 | 60 | ``` 61 | 62 | ## Requirements 63 | 64 | 65 | ## Installation 66 | 67 | DrawPathView is available through [CocoaPods](http://cocoapods.org). To install 68 | it, simply add the following line to your Podfile: 69 | 70 | ```ruby 71 | pod "DrawPathView" 72 | ``` 73 | 74 | ## Author 75 | 76 | Ahmet Kazım Günay, ahmetkgunay@gmail.com 77 | 78 | ## License 79 | 80 | DrawPathView is available under the MIT license. See the LICENSE file for more info. 81 | -------------------------------------------------------------------------------- /anim.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetkgunay/DrawPathView/f0ebb47a0188ce5e8a3f0eb1d72b9c8fa2ee721b/anim.gif --------------------------------------------------------------------------------