Skip to content

Commit

Permalink
Merge pull request #29 from fraserscottmorrison/master
Browse files Browse the repository at this point in the history
Support to UI Rotation
  • Loading branch information
ipraba committed Apr 14, 2017
2 parents 0063a3f + 2b07a53 commit e7c0dfe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
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()
}
}

0 comments on commit e7c0dfe

Please sign in to comment.