Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Source/PPSSignatureView.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
#import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>

@protocol PPSSignatureViewDelegate;

@interface PPSSignatureView : GLKView

@property (assign, nonatomic) UIColor *strokeColor;
@property (assign, nonatomic) BOOL hasSignature;
@property (strong, nonatomic) UIImage *signatureImage;
@property(assign, nonatomic) BOOL longPressToEraseEnabled;
@property(nonatomic,readonly) NSUInteger vertexCount;
@property(assign,nonatomic) float strokeMaxWidth;
@property(weak, nonatomic)id<PPSSignatureViewDelegate> signatureViewDelegate;

- (void)erase;

@end


@protocol PPSSignatureViewDelegate <NSObject>
-(void)signatureDidChange:(PPSSignatureView*)signatureView;
@end
20 changes: 16 additions & 4 deletions Source/PPSSignatureView.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#import <OpenGLES/ES2/glext.h>

#define STROKE_WIDTH_MIN 0.004 // Stroke width determined by touch velocity
#define STROKE_WIDTH_MAX 0.030
#define DEFAULT_STROKE_WIDTH_MAX 0.010
#define STROKE_WIDTH_SMOOTHING 0.5 // Low pass filter alpha

#define VELOCITY_CLAMP_MIN 20
Expand Down Expand Up @@ -128,6 +128,7 @@ - (void)commonInit {
self.context = context;
self.drawableDepthFormat = GLKViewDrawableDepthFormat24;
self.enableSetNeedsDisplay = YES;
self.strokeMaxWidth = DEFAULT_STROKE_WIDTH_MAX;

// Turn on antialiasing
self.drawableMultisample = GLKViewDrawableMultisample4X;
Expand Down Expand Up @@ -226,6 +227,13 @@ - (UIImage *)signatureImage
return screenshot;
}

#pragma mark - Properties

-(NSUInteger)vertexCount
{
return length;
}


#pragma mark - Gesture Recognizers

Expand Down Expand Up @@ -273,7 +281,8 @@ - (void)tap:(UITapGestureRecognizer *)t {


- (void)longPress:(UILongPressGestureRecognizer *)lp {
[self erase];
if(self.longPressToEraseEnabled)
[self erase];
}

- (void)pan:(UIPanGestureRecognizer *)p {
Expand All @@ -294,7 +303,7 @@ - (void)pan:(UIPanGestureRecognizer *)p {
float normalizedVelocity = (clampedVelocityMagnitude - VELOCITY_CLAMP_MIN) / (VELOCITY_CLAMP_MAX - VELOCITY_CLAMP_MIN);

float lowPassFilterAlpha = STROKE_WIDTH_SMOOTHING;
float newThickness = (STROKE_WIDTH_MAX - STROKE_WIDTH_MIN) * (1 - normalizedVelocity) + STROKE_WIDTH_MIN;
float newThickness = (self.strokeMaxWidth - STROKE_WIDTH_MIN) * (1 - normalizedVelocity) + STROKE_WIDTH_MIN;
penThickness = penThickness * lowPassFilterAlpha + newThickness * (1 - lowPassFilterAlpha);

if ([p state] == UIGestureRecognizerStateBegan) {
Expand Down Expand Up @@ -358,6 +367,9 @@ - (void)pan:(UIPanGestureRecognizer *)p {
}

[self setNeedsDisplay];

if(self.signatureViewDelegate)
[self.signatureViewDelegate signatureDidChange:self];
}


Expand All @@ -370,7 +382,7 @@ - (void)setStrokeColor:(UIColor *)strokeColor {
#pragma mark - Private

- (void)updateStrokeColor {
CGFloat red, green, blue, alpha, white;
CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0, white = 0.0;
if (effect && self.strokeColor && [self.strokeColor getRed:&red green:&green blue:&blue alpha:&alpha]) {
effect.constantColor = GLKVector4Make(red, green, blue, alpha);
} else if (effect && self.strokeColor && [self.strokeColor getWhite:&white alpha:&alpha]) {
Expand Down