1- //
1+ //
22// UviSignatureView.swift
33// signature
44//
88
99import UIKit
1010import QuartzCore
11-
11+
1212
1313public let USER_SIGNATURE_PATH = " user_signature_path "
14-
14+
1515@IBDesignable
1616public class UviSignatureView : UIView {
1717 var signPath : UIBezierPath = UIBezierPath ( )
@@ -51,24 +51,30 @@ public class UviSignatureView: UIView {
5151 let mid : CGPoint = CGPoint . init ( x: ( ( p0. x + p1. x) / 2.0 ) , y: ( ( p0. y + p1. y) / 2.0 ) )
5252 return mid;
5353 }
54-
55- // Configurate the line Width
56- internal func initialize( ) {
54+
55+ // Configurate the line Width
56+ internal func initialize( ) {
5757 signPath = UIBezierPath ( ) ;
5858 signPath. lineWidth = 2.0 ; // Configurate the line Width
5959 self . isUserInteractionEnabled = true ;
6060 previousPoint = CGPoint . init ( x: 0 , y: 0 ) ;
6161 pathArray = NSMutableArray ( ) ;
62-
62+
6363 // Added the Pan Reconginzer for capture the touches
6464 let panRecognizer = UIPanGestureRecognizer ( target: self , action: #selector( UviSignatureView . panRecognizer ( _: ) ) )
6565 panRecognizer. minimumNumberOfTouches = 1
6666 self . addGestureRecognizer ( panRecognizer) ;
67-
67+
68+ let tabRecognizer = UITapGestureRecognizer ( target: self , action: #selector( UviSignatureView . handleTap ( _: ) ) )
69+ tabRecognizer. numberOfTapsRequired = 1 ;
70+ tabRecognizer. numberOfTouchesRequired = 1 ;
71+ self . addGestureRecognizer ( tabRecognizer) ;
72+
6873
6974 // Erase when long press the view.
7075 let eraseRecognizer = UILongPressGestureRecognizer ( target: self , action: #selector( UviSignatureView . eraseRecognizer ( _: ) ) )
71- self . addGestureRecognizer ( eraseRecognizer) ;
76+
77+ self . addGestureRecognizer ( eraseRecognizer) ;
7278
7379
7480 // Erase the view when recieving a notification named "shake" from the NSNotificationCenter object
@@ -96,20 +102,20 @@ public class UviSignatureView: UIView {
96102 var bgLinges = NSArray ( ) ;
97103 let width : CGFloat = self . bounds. size. width;
98104 let height : CGFloat = self . bounds. size. height;
99-
105+
100106 let path : UIBezierPath = UIBezierPath ( ) ;
101-
107+
102108 let bottom : CGFloat = 0.90 ;
103-
109+
104110 let x1 : CGFloat = 0 ;
105111 let x2 : CGFloat = width;
106-
112+
107113 let y1 : CGFloat = height*bottom;
108114 let y2 : CGFloat = height*bottom;
109-
115+
110116 path. move ( to: CGPoint ( x: x1, y: y1) ) ;
111117 path. addLine ( to: CGPoint ( x: x2, y: y2) ) ;
112-
118+
113119 bgLinges = [ path] ;
114120 return bgLinges;
115121 }
@@ -126,31 +132,42 @@ public class UviSignatureView: UIView {
126132
127133 return pathArray;
128134 }
129-
135+
136+ func handleTap( _ recognizer: UITapGestureRecognizer ) {
137+ // handling code
138+ let currentPoint = recognizer. location ( in: self ) ;
139+ let previousPoint : CGPoint = CGPoint ( x: currentPoint. x, y: currentPoint. y- 3 ) ;
140+ let midPoint : CGPoint = midpoint ( previousPoint, p1: currentPoint)
141+ signPath. move ( to: currentPoint)
142+ signPath. addLine ( to: midPoint) ;
143+
144+ self . setNeedsDisplay ( ) ; // Update the view.
145+ }
146+
130147 // panReconizer method triggers while touch the view.
131148 func panRecognizer( _ recognizer: UIPanGestureRecognizer ) {
132149 let currentPoint = recognizer. location ( in: self ) ;
133150 let midPoint : CGPoint = midpoint ( previousPoint, p1: currentPoint)
134151
135152 switch ( recognizer. state) {
136- case UIGestureRecognizerState . began:
137- signPath. move ( to: currentPoint)
138- break ;
153+ case UIGestureRecognizerState . began:
154+ signPath. move ( to: currentPoint)
155+ break ;
139156
140- case UIGestureRecognizerState . changed:
141- signPath. addQuadCurve ( to: midPoint, controlPoint: previousPoint) ;
142- break ;
157+ case UIGestureRecognizerState . changed:
158+ signPath. addQuadCurve ( to: midPoint, controlPoint: previousPoint) ;
159+ break ;
143160
144- case UIGestureRecognizerState . ended:
145- signPath. addQuadCurve ( to: midPoint, controlPoint: previousPoint) ;
146- break ;
161+ case UIGestureRecognizerState . ended:
162+ signPath. addQuadCurve ( to: midPoint, controlPoint: previousPoint) ;
163+ break ;
147164
148- case UIGestureRecognizerState . possible:
149- signPath. addQuadCurve ( to: midPoint, controlPoint: previousPoint) ;
150- break ;
165+ case UIGestureRecognizerState . possible:
166+ signPath. addQuadCurve ( to: midPoint, controlPoint: previousPoint) ;
167+ break ;
151168
152- default :
153- break ;
169+ default :
170+ break ;
154171 }
155172
156173 previousPoint = currentPoint;
@@ -165,7 +182,7 @@ public class UviSignatureView: UIView {
165182
166183 // Erase the Siganture view by initial the new path.
167184 internal func erase( ) {
168- //signPath.closePath()
185+ //signPath.closePath()
169186 UserDefaults . standard. removeObject ( forKey: USER_SIGNATURE_PATH) ;
170187 UserDefaults . standard. synchronize ( ) ;
171188 pathArray. removeAllObjects ( ) ;
@@ -187,7 +204,7 @@ public class UviSignatureView: UIView {
187204
188205 public func signatureImage( _ text : NSString , position : CGPoint ) -> UIImage {
189206 UIGraphicsBeginImageContext ( self . bounds. size) ;
190-
207+
191208 if ( !text. isEqual ( to: " " ) ) {
192209 // Setup the font specific variables
193210 let attributes : [ String : AnyObject ] = [
@@ -197,21 +214,21 @@ public class UviSignatureView: UIView {
197214 ]
198215 text. draw ( at: position, withAttributes: attributes) ;
199216 }
200-
217+
201218
202219 for path in ( self . pathArray) {
203220 self . lineColor. setStroke ( ) ;
204221 ( path as AnyObject ) . stroke ( ) ;
205222 }
206-
207-
223+
224+
208225
209226 let image : UIImage = UIGraphicsGetImageFromCurrentImageContext ( ) !;
210227 UIGraphicsEndImageContext ( ) ;
211228 return image;
212229 }
213-
214-
230+
231+
215232 // Only override drawRect: if you perform custom drawing.
216233 // An empty implementation adversely affects performance during animation.
217234 override public func draw( _ rect: CGRect ) {
@@ -229,13 +246,13 @@ public class UviSignatureView: UIView {
229246
230247 if ( !self . signatureExists ( ) && self . signPath. isEmpty) {
231248
232- // Setup the font specific variables
233- let attributes : [ String : AnyObject ] = [
234- NSFontAttributeName : UIFont ( name: " Helvetica " , size: 12 ) !,
235- NSStrokeWidthAttributeName : 0 as AnyObject ,
236- NSForegroundColorAttributeName : UIColor . black. withAlphaComponent ( 0.2 )
237- ]
238- placeHolderString. draw ( at: self . placeholderPoint ( ) , withAttributes: attributes) ;
249+ // Setup the font specific variables
250+ let attributes : [ String : AnyObject ] = [
251+ NSFontAttributeName : UIFont ( name: " Helvetica " , size: 12 ) !,
252+ NSStrokeWidthAttributeName : 0 as AnyObject ,
253+ NSForegroundColorAttributeName : UIColor . black. withAlphaComponent ( 0.2 )
254+ ]
255+ placeHolderString. draw ( at: self . placeholderPoint ( ) , withAttributes: attributes) ;
239256
240257 }
241258
@@ -247,8 +264,8 @@ public class UviSignatureView: UIView {
247264
248265 self . lineColor. setStroke ( ) ;
249266 signPath. stroke ( ) ;
250-
267+
251268 }
252-
253-
269+
270+
254271}
0 commit comments