Skip to content

Commit

Permalink
Merge pull request #4 from Awalz/master
Browse files Browse the repository at this point in the history
Pull latest changes
  • Loading branch information
metabren authored Oct 28, 2020
2 parents f56862d + 11fae52 commit f9cb6cc
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
16 changes: 16 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// swift-tools-version:5.3

import PackageDescription

let package = Package(
name: "SwiftyDraw",
platforms: [
.iOS(.v9)
],
products: [
.library(name: "SwiftyDraw", targets: ["SwiftyDraw"])
],
targets: [
.target(name: "SwiftyDraw", path: "Source")
]
)
35 changes: 35 additions & 0 deletions Source/SwiftyDraw.swift
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,38 @@ extension SwiftyDrawView : UIPencilInteractionDelegate{
}
}
}

extension SwiftyDrawView.DrawItem: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

let pathData = try container.decode(Data.self, forKey: .path)
let uiBezierPath = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(pathData) as! UIBezierPath
path = uiBezierPath.cgPath as! CGMutablePath

brush = try container.decode(Brush.self, forKey: .brush)
isFillPath = try container.decode(Bool.self, forKey: .isFillPath)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

let uiBezierPath = UIBezierPath(cgPath: path)
var pathData: Data?
if #available(iOS 11.0, *) {
pathData = try NSKeyedArchiver.archivedData(withRootObject: uiBezierPath, requiringSecureCoding: false)
} else {
pathData = NSKeyedArchiver.archivedData(withRootObject: uiBezierPath)
}
try container.encode(pathData!, forKey: .path)

try container.encode(brush, forKey: .brush)
try container.encode(isFillPath, forKey: .isFillPath)
}

enum CodingKeys: String, CodingKey {
case brush
case path
case isFillPath
}
}
4 changes: 2 additions & 2 deletions SwiftyDraw.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Pod::Spec.new do |s|
s.name = 'SwiftyDraw'
s.version = '2.3'
s.version = '2.4.1'
s.summary = 'A simple, core graphics drawing framework written in Swift'

s.description = <<-DESC
Expand All @@ -14,7 +14,7 @@ Pod::Spec.new do |s|
s.source = { :git => 'https://github.com/Awalz/SwiftyDraw.git', :tag => s.version }
s.social_media_url = 'https://twitter.com/linusgeffarth'

s.ios.deployment_target = '8.0'
s.ios.deployment_target = '9.0'
s.swift_version = '5.0'

s.source_files = 'Source/**/*'
Expand Down
24 changes: 16 additions & 8 deletions SwiftyDrawExample/SwiftyDrawExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ViewController: UIViewController {
@IBAction func selectedColor(_ button: UIButton) {
guard let color = button.backgroundColor else { return }
drawView.brush.color = Color(color)
drawView.brush.blendMode = .normal
deactivateEraser()
}

@IBAction func undo() {
Expand All @@ -55,20 +55,16 @@ class ViewController: UIViewController {
@IBAction func toggleEraser() {
if drawView.brush.blendMode == .normal {
//Switch to clear
drawView.brush.blendMode = .clear
eraserButton.tintColor = .red
eraserButton.setTitle("deactivate eraser", for: .normal)
activateEraser()
} else {
//Switch to normal
drawView.brush.blendMode = .normal
eraserButton.tintColor = self.view.tintColor
eraserButton.setTitle("activate eraser", for: .normal)
deactivateEraser()
}
}

@IBAction func clearCanvas() {
drawView.clear()
drawView.brush.blendMode = .normal
deactivateEraser()
}

@IBAction func setDrawMode() {
Expand Down Expand Up @@ -109,7 +105,19 @@ class ViewController: UIViewController {

@IBAction func changedOpacity(_ slider: UISlider) {
drawView.brush.opacity = CGFloat(slider.value)
deactivateEraser()
}

func activateEraser() {
drawView.brush.blendMode = .clear
eraserButton.tintColor = .red
eraserButton.setTitle("deactivate eraser", for: .normal)
}

func deactivateEraser() {
drawView.brush.blendMode = .normal
eraserButton.tintColor = self.view.tintColor
eraserButton.setTitle("activate eraser", for: .normal)
}
}

0 comments on commit f9cb6cc

Please sign in to comment.