iOSUI工具

iOS 手写签名

2021-11-03  本文已影响0人  _Waiting_

手写签名

.h

// 手写签名视图

#import <UIKit/UIKit.h>

@protocol HandwritenSignatureDelegate <NSObject>

@optional
- (void)handwritenSignViewDidFinish:(UIImage*)signedImage;

@optional
- (void)changeStatusBarState:(BOOL)hidden;
@end


#pragma mark - HandwritenSignatureView
@interface HandwritenSignatureView : UIView

@property(nonatomic, weak) id<HandwritenSignatureDelegate> delegate;

/**
 初始化方法
 
 @param frame frame
 @param lineWidth 签名时线条宽度,默认为2.0,最大为20
 */
- (id)initWithFrame:(CGRect)frame lineWidth:(CGFloat)lineWidth;

- (void)startHandwriteSign; //初始化签名页面
- (void)clearSignature;     //清除签名

@end

.m

#import "HandwritenSignatureView.h"

#define ColorFromRGB(rgb) [UIColor colorWithRed:((float) ((rgb & 0xFF0000) >> 16)) / 255.0 \
                                          green:((float) ((rgb & 0xFF00) >> 8)) / 255.0    \
                                           blue:((float) (rgb & 0xFF)) / 255.0             \
                                          alpha:1.0]
#define kBgColorWhite           0xFFFFFF
#define kFontColorImp33         0x333333    //浅黑色
#define kNewSepLineColor        0xD7D7D7    //分割线颜色(5.0之后的样式)
#define kBgColorDisableGray     0xCCCCCC    //灰色不可点击
#define kSystemFont18       [UIFont fontWithName:@"Helvetica" size:18]

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#pragma mark -
#pragma mark - CanvasView
@interface CanvasView : UIView
{
    UIBezierPath *path; //签名笔迹
    CGPoint points[5];  //签名时移动的轨迹记录
    uint pointIndex;    //
    
    BOOL _isSigned;     //是否签了名
}
@property(nonatomic, assign) CGFloat lineWidth;  //签名线条宽度

- (id)initWithFrame:(CGRect)frame lineWidth:(CGFloat)lineWidth;

- (void)clearCanvas;  //清空画布
- (UIImage*)getSignatureImage; //获取签名的图片
- (BOOL)isSigned;   //是否已经签了名
@end


@implementation CanvasView
- (id)initWithFrame:(CGRect)frame lineWidth:(CGFloat)lineWidth
{
    if( (self = [super initWithFrame:frame]) ){
        _lineWidth = (lineWidth <= CGFLOAT_MIN || lineWidth > 20.0) ? 2.0 :lineWidth;
        [self initContent];
    }

    return self;
}

- (void)drawRect:(CGRect)rect {
    //    [signedImage drawInRect:rect];
    [path stroke];
}

#pragma mark - public method
- (void)clearCanvas
{
    _isSigned = NO;
    pointIndex = 0;
    bzero(points, sizeof(points));
    [path removeAllPoints];
    [self setNeedsDisplay];
}

- (UIImage*)getSignatureImage
{
    UIImage *image = nil;
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
    if( !image ){
        UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds];
        [[UIColor whiteColor] setFill];
        [rectpath fill];
    }
    [image drawAtPoint:CGPointZero];
    [[UIColor blackColor] setStroke];
    [path stroke];
    image = UIGraphicsGetImageFromCurrentImageContext(); //UIGraphicsgetSignatureImageFromCurrentImageContext()
    UIGraphicsEndImageContext();
    
    return image;
}

- (BOOL)isSigned
{
    return _isSigned;
}

#pragma mark - touch events
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    pointIndex = 0;
    UITouch *touch = [touches anyObject];
    points[0] = [touch locationInView:self];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    pointIndex++;
    points[pointIndex] = p;
    if(pointIndex == 4)
    {
        points[3] = CGPointMake( (points[2].x + points[4].x) / 2.0, (points[2].y + points[4].y)/2.0 );
        
        [path moveToPoint:points[0]];
        [path addCurveToPoint:points[3] controlPoint1:points[1] controlPoint2:points[2]];
        
        [self setNeedsDisplay];
        points[0] = points[3];
        points[1] = points[4];
        pointIndex = 1;
    }
    
    if(!_isSigned)
        _isSigned = YES;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //    [self getSignatureImage];
    //    [path removeAllPoints];
    pointIndex = 0;
    [self setNeedsDisplay];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self touchesEnded:touches withEvent:event];
}

#pragma mark - init
- (void)initContent
{
    _isSigned = NO;
    self.backgroundColor = [UIColor whiteColor];
    [self setMultipleTouchEnabled:NO];
    path = [UIBezierPath bezierPath];
    [path setLineWidth:_lineWidth];
}

@end

#pragma mark -
#pragma mark - HandwritenSignatureView
@interface HandwritenSignatureView ()

@property(nonatomic, strong) CanvasView *drawView;
@property(nonatomic, strong) UIView *btnContainer;
@property(nonatomic, strong) UIButton *clearBtn;
@property(nonatomic, strong) UIButton *confirmBtn;
@property(nonatomic, strong) UILabel *titleLabel;
@property(nonatomic, strong) UIView *sep;
@end

@implementation HandwritenSignatureView

- (id)initWithFrame:(CGRect)frame lineWidth:(CGFloat)lineWidth
{
    if( (self = [super initWithFrame:frame]) )
    {
        [self setLeftOrientation];
        [self initContentWithLineWidth:lineWidth];
    }
    
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    _btnContainer.frame = CGRectMake(0.0, 0.0, self.frame.size.height, 69.0);
    _clearBtn.frame = CGRectMake(20.0, 15.0, 100.0, 40.0);
    _titleLabel.frame = CGRectMake( (self.frame.size.height - 315.0)/2.0, (_btnContainer.frame.size.height - 22.0) / 2.0, 315.0 , 22.0);
    _confirmBtn.frame = CGRectMake( self.frame.size.height - 100.0 - 15.0, 15.0, 100.0, 40.0);
    _sep.frame = CGRectMake(0.0, _btnContainer.frame.size.height - 0.5, self.frame.size.height, 0.5);
    
    _drawView.frame = CGRectMake(0.0, _btnContainer.frame.size.height, self.frame.size.height, self.frame.size.width - _btnContainer.frame.size.height) ;
    
}

#pragma mark -
- (void)startHandwriteSign
{
    [self setLeftOrientation];
    if(self.delegate && [self.delegate respondsToSelector:@selector(changeStatusBarState:)]){
        [self.delegate changeStatusBarState:YES];
    }
}

- (void)clearSignature
{
    [_drawView clearCanvas];
}

#pragma mark - private method
- (void)initContentWithLineWidth:(CGFloat)lineWidth
{
    self.backgroundColor = ColorFromRGB(kBgColorWhite);
    
    _btnContainer = [self viewWithFrame:CGRectZero bgColor:ColorFromRGB(kBgColorWhite)];
    [self addSubview:_btnContainer];
    
    _clearBtn = [self buttonWithFrame:CGRectZero title:@"重签" action:@selector(clearAction:)];
    [_btnContainer addSubview:_clearBtn];
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _titleLabel.text = @"用正楷字从左往右签署身份证上的姓名";
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.font = kSystemFont18;
    _titleLabel.textColor = ColorFromRGB(kFontColorImp33);
    [_btnContainer addSubview:_titleLabel];
    
    _confirmBtn = [self buttonWithFrame:CGRectZero title:@"完成" action:@selector(confirmAction:)];
    [_btnContainer addSubview:_confirmBtn];
    
    _sep = [self viewWithFrame:CGRectZero bgColor:ColorFromRGB(kNewSepLineColor)];
    [_btnContainer addSubview:_sep];
    
    _drawView = [[CanvasView alloc] initWithFrame:CGRectZero lineWidth:lineWidth];
    [self addSubview:_drawView];
}

#pragma mark - actions
- (void)clearAction:(id)sender
{
    [_drawView clearCanvas];
}

- (void)confirmAction:(id)sender
{
    __weak typeof(self) this = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        if(this.delegate && [this.delegate respondsToSelector:@selector(changeStatusBarState:)]){
            [this.delegate changeStatusBarState:NO];
        }
        
        UIImage *image = nil;
        if( [this.drawView isSigned] )
            image = [this.drawView getSignatureImage];
        
        if( this.delegate && [this.delegate respondsToSelector:@selector(handwritenSignViewDidFinish:)] ){
            [this.delegate handwritenSignViewDidFinish:image];
        }

        [this removeFromSuperview];
    });
}

#pragma mark -
-(void)setLeftOrientation
{
    dispatch_async(dispatch_get_main_queue(), ^{
        self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI_2);//CGAffineTransformMakeRotation(M_PI_2);
        self.bounds = CGRectMake(0.0, 0.0, SCREEN_HEIGHT, SCREEN_WIDTH);
        [self setNeedsLayout];
    });
}

#pragma mark - getter / setter
- (UIButton*)buttonWithFrame:(CGRect)frame title:(NSString*)title action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = kSystemFont18;
    button.layer.borderWidth = 0.5;
    button.layer.borderColor = ColorFromRGB(kBgColorDisableGray).CGColor;
    button.layer.cornerRadius = 20.0;
    button.layer.masksToBounds = YES;
    [button setTitleColor:ColorFromRGB(kFontColorImp33) forState:UIControlStateNormal];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];

    return button;
}

- (UIView*)viewWithFrame:(CGRect)frame bgColor:(UIColor*)bgColor
{
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = bgColor;
    return view;
}

@end
上一篇 下一篇

猜你喜欢

热点阅读