Skip to content

Commit

Permalink
Merge pull request #36 from GJNilsen/Development
Browse files Browse the repository at this point in the history
Bugfix, stroke color was not saved. Now it works correctly.
  • Loading branch information
GJ Nilsen authored May 23, 2017
2 parents 601891f + 0738a27 commit b8b0c1b
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 46 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ YPDrawSignatureView is available under the MIT license. See the [LICENSE](LICENS

## Update history

### v1.1 - 5/3/17
### v1.1.1 - 5/23/17

* Bugfix

#### v1.1 - 5/3/17

* Added PDF support for exporting signature as high resolution vector graphics
* Deprecated methods and properties are properly marked
Expand Down
2 changes: 1 addition & 1 deletion SignatureTest/SignatureTest/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// YPDrawSignatureView is open source
// Version 1.1
// Version 1.1.1
//
// Copyright (c) 2014 - 2017 The YPDrawSignatureView Project Contributors
// Available under the MIT license
Expand Down
7 changes: 6 additions & 1 deletion SignatureTest/SignatureTest/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16F73" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
Expand Down Expand Up @@ -31,6 +31,11 @@
<constraint firstAttribute="height" constant="300" id="I4C-mE-4SQ"/>
<constraint firstAttribute="width" constant="400" id="Wtv-bb-sg2"/>
</constraints>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="color" keyPath="strokeColor">
<color key="value" red="0.87984528559999997" green="0.052443682329999998" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<stackView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="QC8-3Q-hSJ">
<rect key="frame" x="0.0" y="300" width="400" height="30"/>
Expand Down
2 changes: 1 addition & 1 deletion SignatureTest/SignatureTest/ViewController.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// YPDrawSignatureView is open source
// Version 1.1
// Version 1.1.1
//
// Copyright (c) 2014 - 2017 The YPDrawSignatureView Project Contributors
// Available under the MIT license
Expand Down
68 changes: 35 additions & 33 deletions SignatureTest/SignatureTest/YPDrawSignatureView.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// YPDrawSignatureView is open source
// Version 1.1
// Version 1.1.1
//
// Copyright (c) 2014 - 2017 The YPDrawSignatureView Project Contributors
// Available under the MIT license
Expand Down Expand Up @@ -32,28 +32,28 @@ final public class YPDrawSignatureView: UIView {
// MARK: - Public properties
@IBInspectable public var strokeWidth: CGFloat = 2.0 {
didSet {
self.path.lineWidth = strokeWidth
path.lineWidth = strokeWidth
}
}

@IBInspectable public var strokeColor: UIColor = .black {
didSet {
self.strokeColor.setStroke()
strokeColor.setStroke()
}
}

@objc
@available(*, deprecated, renamed: "backgroundColor")
@IBInspectable public var signatureBackgroundColor: UIColor = .white {
didSet {
self.backgroundColor = signatureBackgroundColor
backgroundColor = signatureBackgroundColor
}
}

// Computed Property returns true if the view actually contains a signature
public var doesContainSignature: Bool {
get {
if self.path.isEmpty {
if path.isEmpty {
return false
} else {
return true
Expand All @@ -70,15 +70,15 @@ final public class YPDrawSignatureView: UIView {
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

self.path.lineWidth = self.strokeWidth
self.path.lineJoinStyle = CGLineJoin.round
path.lineWidth = strokeWidth
path.lineJoinStyle = CGLineJoin.round
}

override public init(frame: CGRect) {
super.init(frame: frame)

self.path.lineWidth = self.strokeWidth
self.path.lineJoinStyle = CGLineJoin.round
path.lineWidth = strokeWidth
path.lineJoinStyle = CGLineJoin.round
}

// MARK: - Draw
Expand All @@ -91,46 +91,46 @@ final public class YPDrawSignatureView: UIView {
override public func touchesBegan(_ touches: Set <UITouch>, with event: UIEvent?) {
if let firstTouch = touches.first {
let touchPoint = firstTouch.location(in: self)
self.controlPoint = 0
self.points[0] = touchPoint
controlPoint = 0
points[0] = touchPoint
}

if let delegate = self.delegate {
if let delegate = delegate {
delegate.didStart()
}
}

override public func touchesMoved(_ touches: Set <UITouch>, with event: UIEvent?) {
if let firstTouch = touches.first {
let touchPoint = firstTouch.location(in: self)
self.controlPoint += 1
self.points[self.controlPoint] = touchPoint
if (self.controlPoint == 4) {
self.points[3] = CGPoint(x: (self.points[2].x + self.points[4].x)/2.0, y: (self.points[2].y + self.points[4].y)/2.0)
self.path.move(to: self.points[0])
self.path.addCurve(to: self.points[3], controlPoint1:self.points[1], controlPoint2:self.points[2])
controlPoint += 1
points[controlPoint] = touchPoint
if (controlPoint == 4) {
points[3] = CGPoint(x: (points[2].x + points[4].x)/2.0, y: (points[2].y + points[4].y)/2.0)
path.move(to: points[0])
path.addCurve(to: points[3], controlPoint1: points[1], controlPoint2: points[2])

self.setNeedsDisplay()
self.points[0] = self.points[3]
self.points[1] = self.points[4]
self.controlPoint = 1
setNeedsDisplay()
points[0] = points[3]
points[1] = points[4]
controlPoint = 1
}

self.setNeedsDisplay()
setNeedsDisplay()
}
}

override public func touchesEnded(_ touches: Set <UITouch>, with event: UIEvent?) {
if self.controlPoint < 4 {
let touchPoint = self.points[0]
self.path.move(to: CGPoint(x: touchPoint.x-1.0,y: touchPoint.y))
self.path.addLine(to: CGPoint(x: touchPoint.x+1.0,y: touchPoint.y))
self.setNeedsDisplay()
if controlPoint < 4 {
let touchPoint = points[0]
path.move(to: CGPoint(x: touchPoint.x-1.0,y: touchPoint.y))
path.addLine(to: CGPoint(x: touchPoint.x+1.0,y: touchPoint.y))
setNeedsDisplay()
} else {
self.controlPoint = 0
controlPoint = 0
}

if let delegate = self.delegate {
if let delegate = delegate {
delegate.didFinish()
}
}
Expand All @@ -147,6 +147,7 @@ final public class YPDrawSignatureView: UIView {
public func getSignature(scale:CGFloat = 1) -> UIImage? {
if !doesContainSignature { return nil }
UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, scale)
self.strokeColor.setStroke()
self.path.stroke()
let signature = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Expand Down Expand Up @@ -179,14 +180,15 @@ final public class YPDrawSignatureView: UIView {

guard let dataConsumer = CGDataConsumer.init(data: mutableData!) else { fatalError() }

var rect = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)
var rect = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)

guard let pdfContext = CGContext(consumer: dataConsumer, mediaBox: &rect, nil) else { fatalError() }

pdfContext.beginPDFPage(nil)
pdfContext.translateBy(x: 0, y: self.frame.height)
pdfContext.translateBy(x: 0, y: frame.height)
pdfContext.scaleBy(x: 1, y: -1)
pdfContext.addPath(self.path.cgPath)
pdfContext.addPath(path.cgPath)
pdfContext.setStrokeColor(strokeColor.cgColor)
pdfContext.strokePath()
pdfContext.saveGState()
pdfContext.endPDFPage()
Expand Down
6 changes: 3 additions & 3 deletions SignatureTest/SignatureTestTests/SignatureViewTest.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// YPDrawSignatureView is open source
// Version 1.0.2
// Version 1.1.1
//
// Copyright (c) 2014 - 2017 The YPDrawSignatureView Project Contributors
// Available under the MIT license
Expand Down Expand Up @@ -39,7 +39,7 @@ class SignatureViewTest: XCTestCase {
signatureView.strokeColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
signatureView.injectBezierPath(doodle())
signatureView.draw(signatureView.frame)
XCTAssertNotEqual(signatureView.signatureBackgroundColor, getPixelColor(view2Image(signatureView), at: CGPoint(x: 4, y: 4)))
XCTAssertNotEqual(signatureView.backgroundColor, getPixelColor(view2Image(signatureView), at: CGPoint(x: 4, y: 4)))
}

func testGetSignature() {
Expand All @@ -49,7 +49,7 @@ class SignatureViewTest: XCTestCase {
signatureView.draw(signatureView.frame)

let signature = signatureView.getSignature()
XCTAssertNotEqual(signatureView.signatureBackgroundColor, getPixelColor(signature!, at: CGPoint(x: 4, y: 4)))
XCTAssertNotEqual(signatureView.backgroundColor, getPixelColor(signature!, at: CGPoint(x: 4, y: 4)))
}


Expand Down
11 changes: 6 additions & 5 deletions SignatureTest/SignatureTestUITests/SignatureTestUITests.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// YPDrawSignatureView is open source
// Version 1.1.1
//
// SignatureTestUITests.swift
// SignatureTestUITests
//
// Created by Geert-Jan Korsbø Nilsen on 19/12/15.
// Copyright © 2015 Yuppielabel. All rights reserved.
// Copyright (c) 2014 - 2017 The YPDrawSignatureView Project Contributors
// Available under the MIT license
//
// https://github.com/GJNilsen/YPDrawSignatureView/blob/master/LICENSE License Information
// https://github.com/GJNilsen/YPDrawSignatureView/blob/master/README.md Project Contributors

import XCTest

Expand Down
4 changes: 3 additions & 1 deletion Sources/YPDrawSignatureView.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// YPDrawSignatureView is open source
// Version 1.1
// Version 1.1.1
//
// Copyright (c) 2014 - 2017 The YPDrawSignatureView Project Contributors
// Available under the MIT license
Expand Down Expand Up @@ -147,6 +147,7 @@ final public class YPDrawSignatureView: UIView {
public func getSignature(scale:CGFloat = 1) -> UIImage? {
if !doesContainSignature { return nil }
UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, scale)
self.strokeColor.setStroke()
self.path.stroke()
let signature = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Expand Down Expand Up @@ -187,6 +188,7 @@ final public class YPDrawSignatureView: UIView {
pdfContext.translateBy(x: 0, y: self.frame.height)
pdfContext.scaleBy(x: 1, y: -1)
pdfContext.addPath(self.path.cgPath)
pdfContext.setStrokeColor(strokeColor.cgColor)
pdfContext.strokePath()
pdfContext.saveGState()
pdfContext.endPDFPage()
Expand Down

0 comments on commit b8b0c1b

Please sign in to comment.