Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to UI Rotation #29

Merged
merged 1 commit into from
Apr 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 13 additions & 21 deletions Pod/Classes/EPSignatureView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ open class EPSignatureView: UIView {
fileprivate var bezierPoints = [CGPoint](repeating: CGPoint(), count: 5)
fileprivate var bezierPath = UIBezierPath()
fileprivate var bezierCounter : Int = 0
fileprivate var maxPoint = CGPoint.zero
fileprivate var minPoint = CGPoint.zero

// MARK: - Public Vars

Expand All @@ -31,15 +29,13 @@ open class EPSignatureView: UIView {
self.backgroundColor = UIColor.clear
bezierPath.lineWidth = strokeWidth
addLongPressGesture()
minPoint = CGPoint(x: self.frame.size.width,y: self.frame.size.height)
}

public override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.clear
bezierPath.lineWidth = strokeWidth
addLongPressGesture()
minPoint = CGPoint(x: self.frame.size.width,y: self.frame.size.height)
}

override open func draw(_ rect: CGRect) {
Expand All @@ -61,7 +57,7 @@ open class EPSignatureView: UIView {

func longPressed(_ gesture: UILongPressGestureRecognizer) {
let touchPoint = gesture.location(in: self)
let endAngle = CGFloat(2.0 * M_PI)
let endAngle: CGFloat = .pi * 2.0
bezierPath.move(to: touchPoint)
bezierPath.addArc(withCenter: touchPoint, radius: 2, startAngle: 0, endAngle: endAngle, clockwise: true)
setNeedsDisplay()
Expand Down Expand Up @@ -100,21 +96,7 @@ open class EPSignatureView: UIView {

func touchPoint(_ touches: Set<UITouch>) -> CGPoint? {
if let touch = touches.first {
let point = touch.location(in: self)
//Track the signature bounding area
if point.x > maxPoint.x {
maxPoint.x = point.x
}
if point.y > maxPoint.y {
maxPoint.y = point.y
}
if point.x < minPoint.x {
minPoint.x = point.x
}
if point.y < minPoint.y {
minPoint.y = point.y
}
return point
return touch.location(in: self)
}
return nil
}
Expand All @@ -129,6 +111,16 @@ open class EPSignatureView: UIView {
setNeedsDisplay()
}

/** scales and repositions the path
*/
open func reposition() {
var ratio = min(self.bounds.width / bezierPath.bounds.width, 1)
ratio = min((self.bounds.height - 64) / bezierPath.bounds.height, ratio)
bezierPath.apply(CGAffineTransform(scaleX: ratio, y: ratio))
bezierPath.apply(CGAffineTransform(translationX: -bezierPath.bounds.origin.x, y: -bezierPath.bounds.origin.y + 64))
setNeedsDisplay()
}

/** Returns the drawn path as Image. Adding subview to this view will also get returned in this image.
*/
open func getSignatureAsImage() -> UIImage? {
Expand All @@ -146,7 +138,7 @@ open class EPSignatureView: UIView {
*/

open func getSignatureBoundsInCanvas() -> CGRect {
return CGRect(x: minPoint.x, y: minPoint.y, width: maxPoint.x - minPoint.x, height: maxPoint.y - minPoint.y)
return bezierPath.bounds
}

//MARK: Save load signature methods
Expand Down
3 changes: 3 additions & 0 deletions Pod/Classes/EPSignatureViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,7 @@ open class EPSignatureViewController: UIViewController {
signatureView.clear()
}

override open func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
signatureView.reposition()
}
}