Skip to content

Commit

Permalink
Merge pull request #28 from dcooley/straightLine
Browse files Browse the repository at this point in the history
straight line
  • Loading branch information
LinusGeffarth authored Nov 3, 2019
2 parents f20ec14 + bd51eae commit 55d7459
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
18 changes: 9 additions & 9 deletions Source/Brush.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,33 @@ public class Brush {
guard #available(iOS 9.1, *), touch.type == .pencil else { return originalWidth }
return (originalWidth*(1-adjustedWidthFactor/10*2)) + (adjustedWidthFactor/touch.altitudeAngle)
}

public func adjustWidth(for touch: UITouch) {
width = adjustedWidth(for: touch)
}

// MARK: - Static brushes

public static var `default`: Brush {
return Brush(color: .black, width: 3, opacity: 1)
}

public static var thin: Brush {
return Brush(color: .black, width: 2, opacity: 1)
}

public static var medium: Brush {
return Brush(color: .black, width: 7, opacity: 1)
}

public static var thick: Brush {
return Brush(color: .black, width: 10, opacity: 1)
}

public static var marker: Brush {
return Brush(color: #colorLiteral(red: 0.920953393, green: 0.447560966, blue: 0.4741248488, alpha: 1), width: 10, opacity: 0.3)
}

public static var eraser: Brush {
return Brush(adjustedWidthFactor: 5, blendMode: .clear)
}
Expand All @@ -77,8 +77,8 @@ extension Brush: Equatable, Comparable, CustomStringConvertible {
public static func ==(lhs: Brush, rhs: Brush) -> Bool {
return (
lhs.color == rhs.color &&
lhs.originalWidth == rhs.originalWidth &&
lhs.opacity == rhs.opacity
lhs.originalWidth == rhs.originalWidth &&
lhs.opacity == rhs.opacity
)
}

Expand Down
46 changes: 39 additions & 7 deletions Source/SwiftyDraw.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ open class SwiftyDrawView: UIView {
}
/// Sets whether touch gestures should be registered as drawing strokes on the current canvas
public var isEnabled = true

/// sets whether the line drawn should be straight
public var isStraight = false

/// Sets whether responde to Apple Pencil interactions, like the Double tap for Apple Pencil 2 to switch tools.
public var isPencilInteractive : Bool = true {
didSet{
Expand Down Expand Up @@ -95,9 +99,10 @@ open class SwiftyDrawView: UIView {
@available(iOS 9.1, *)
public lazy var allowedTouchTypes: [TouchType] = [.finger, .pencil]

public var lines: [Line] = []
public var drawingHistory: [Line] = []
private var currentPoint: CGPoint = .zero
public var lines: [Line] = []
public var drawingHistory: [Line] = []
public var firstPoint: CGPoint = .zero // created this variable
public var currentPoint: CGPoint = .zero // made public
private var previousPoint: CGPoint = .zero
private var previousPreviousPoint: CGPoint = .zero

Expand Down Expand Up @@ -167,6 +172,7 @@ open class SwiftyDrawView: UIView {
delegate?.swiftyDraw(didBeginDrawingIn: self, using: touch)

setTouchPoints(touch, view: self)
firstPoint = touch.location(in: self) // for drawing straight lines
let newLine = Line(path: CGMutablePath(),
brush: Brush(color: brush.color, width: brush.width, opacity: brush.opacity, blendMode: brush.blendMode))
lines.append(newLine)
Expand All @@ -182,9 +188,20 @@ open class SwiftyDrawView: UIView {
delegate?.swiftyDraw(isDrawingIn: self, using: touch)

updateTouchPoints(for: touch, in: self)
let newPath = createNewPath()
if let currentPath = lines.last {
currentPath.path.addPath(newPath)

if isStraight {
lines.removeLast()

let newLine = Line(path: CGMutablePath(),
brush: Brush(color: brush.color, width: brush.width, opacity: brush.opacity, blendMode: brush.blendMode))
newLine.path.addPath(createNewStraightPath())
lines.append(newLine)
drawingHistory = lines
} else {
let newPath = createNewPath()
if let currentPath = lines.last {
currentPath.path.addPath(newPath)
}
}
}

Expand Down Expand Up @@ -237,7 +254,7 @@ open class SwiftyDrawView: UIView {
setNeedsDisplay()
}

/********************************** Private Functions **********************************/
/********************************** Private Functions **********************************/

private func setTouchPoints(_ touch: UITouch,view: UIView) {
previousPoint = touch.previousLocation(in: view)
Expand All @@ -258,6 +275,14 @@ open class SwiftyDrawView: UIView {
return newPath
}

private func createNewStraightPath() -> CGMutablePath {
let pt1 : CGPoint = firstPoint
let pt2 : CGPoint = currentPoint
let subPath = createStraightSubPath(pt1, mid2: pt2)
let newPath = addSubPathToPath(subPath)
return newPath
}

private func calculateMidPoint(_ p1 : CGPoint, p2 : CGPoint) -> CGPoint {
return CGPoint(x: (p1.x + p2.x) * 0.5, y: (p1.y + p2.y) * 0.5);
}
Expand All @@ -275,6 +300,13 @@ open class SwiftyDrawView: UIView {
return subpath
}

private func createStraightSubPath(_ mid1: CGPoint, mid2: CGPoint) -> CGMutablePath {
let subpath : CGMutablePath = CGMutablePath()
subpath.move(to: mid1)
subpath.addLine(to: mid2)
return subpath
}

private func addSubPathToPath(_ subpath: CGMutablePath) -> CGMutablePath {
let bounds : CGRect = subpath.boundingBox
let drawBox : CGRect = bounds.insetBy(dx: -2.0 * brush.width, dy: -2.0 * brush.width)
Expand Down

0 comments on commit 55d7459

Please sign in to comment.