[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"$flaLr53TzKKqfFHgl1VWnN2eyYB9NOcYkNM7-91AXwrE":3},{"code":4,"message":5,"data":6},200,"成功",{"id":7,"createdAt":8,"title":9,"content":10,"summary":11,"image":12,"uid":13,"user":14,"categoryId":21,"category":22,"subCategoryId":24,"subCategory":25,"comments":27,"status":17,"reason":12,"notice":12,"visitCount":28,"commentCount":29,"keywords":30},53,"2024-01-30T15:16:15.611Z","iOS中实现一个手写板，类似签名那样的效果，简单实现。","下面的代码是好多年前的，仅供参考：\n先展示一下这个文件的.h文件中的代码：\n```\n//\n//  HandSignatureView.h\n//  badminton\n//\n//  Created by 韩卫星 on 2017/8/1.\n//  Copyright © 2017年 huayu. All rights reserved.\n//\n\n#import \u003CUIKit/UIKit.h>\n\n/**\n “手写签名”视图\n */\n@interface HandSignatureView : UIView\n\n/*\n 手势涂鸦或者绘制的颜色\n */\n@property (nonatomic,strong)UIColor *penColor;\n\n/**\n 清楚“手写签名”\n */\n- (void)clear;\n\n/*\n 获取当前的屏幕绘制的图片\n */\n- (UIImage *)getHandSignatureImage;\n\n@end\n\n\n//\n\n```\n\n然后是.m中的代码，如下：\n```\n//\n//  HandSignatureView.m\n//  badminton\n//\n//  Created by 韩卫星 on 2017/8/1.\n//  Copyright © 2017年 huayu. All rights reserved.\n//\n\n#import \"HandSignatureView.h\"\n\n@interface HandSignatureView()\n\n@property (nonatomic,strong)UIBezierPath *oneDrawPath;         //记录某一次的路径绘制\n@property (nonatomic,strong)NSMutableArray *allPathArray;  //用来记录当前触摸的所有点\n\n@property (nonatomic, copy) NSMutableArray *ptsArr;\n\n@end\n\n@implementation HandSignatureView\n\n// Only override drawRect: if you perform custom drawing.\n// An empty implementation adversely affects performance during animation.\n- (void)drawRect:(CGRect)rect {\n    // Drawing code\n    \n    //将数组中的所有的点绘制出来\n    for (UIBezierPath *path in self.allPathArray) {\n        \n        UIColor *color = nil;\n        if (_penColor) {\n            color =_penColor;\n        }else{\n            color = [UIColor blackColor];\n        }\n        [color set];\n        [path stroke];\n    }\n}\n\n/*\n 每次触摸屏幕,生成一个记录路径的UIBezierPath\n */\n- (void)touchesBegan:(NSSet\u003CUITouch *> *)touches withEvent:(UIEvent *)event{\n    UITouch *touch = [touches anyObject];\n    CGPoint point = [touch locationInView:self];\n    _oneDrawPath = [UIBezierPath bezierPath];\n    [_oneDrawPath setLineWidth:5];\n    [_oneDrawPath moveToPoint:point];\n    [self.allPathArray addObject:_oneDrawPath];\n    \n    NSValue *ptValue = [NSValue valueWithCGPoint:point];\n    [self.ptsArr addObject:ptValue];\n}\n/*\n 结合生成的path,将移动轨迹绘制出来\n */\n\n- (void)touchesMoved:(NSSet\u003CUITouch *> *)touches withEvent:(UIEvent *)event{\n    UITouch *touch = [touches anyObject];\n    CGPoint point = [touch locationInView:self];\n    [_oneDrawPath addLineToPoint:point];\n    [self setNeedsDisplay];\n    \n    NSValue *ptValue = [NSValue valueWithCGPoint:point];\n    [self.ptsArr addObject:ptValue];\n}\n\n- (void)clear {\n    [_allPathArray removeAllObjects];\n    [self setNeedsDisplay];\n}\n\n/*\n 根据当前view的大小,从上下文内容得出图片\n */\n- (UIImage *)getHandSignatureImage{\n    \n    // 如果没有笔画痕迹，图片为nil\n    if(self.ptsArr.count == 0) {\n        return nil;\n    }\n    \n    // 签名笔划所在的矩形框（笔画的最左点，最高点，最右点，最低点）\n    CGRect rect = [self getPenZoneRect];\n    \n    UIGraphicsBeginImageContext(self.bounds.size);\n    \n    CGContextRef context = UIGraphicsGetCurrentContext();\n    \n    //设置当前绘图环境到矩形框（主要是截取签名文笔所在矩形框）\n    CGContextClipToRect(context, rect);\n    \n    [self.layer renderInContext:context];\n    \n    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();\n    \n    \n    UIGraphicsEndImageContext();\n    return theImage;\n}\n\n/*\n 将存储的数组初始化\n */\n- (NSMutableArray *)allPathArray{\n    if (!_allPathArray) {\n        _allPathArray = [NSMutableArray array];\n    }\n    return _allPathArray;\n}\n\n- (NSMutableArray *)ptsArr {\n    if(!_ptsArr) {\n        _ptsArr = [NSMutableArray array];\n    }\n    return _ptsArr;\n}\n\n/**\n 获取到笔画所在的矩形区域\n */\n- (CGRect)getPenZoneRect {\n    \n    NSValue *firstPtValue = [self.ptsArr firstObject];\n    CGPoint firstPt = [firstPtValue CGPointValue];\n    \n    float leftPtX = firstPt.x;\n    float topPtY = firstPt.y;\n    float rightPtX = firstPt.x;\n    float bottomPtY = firstPt.y;\n    \n    for(int i = 0; i \u003C self.ptsArr.count; i++) {\n        NSValue *value = self.ptsArr[i];\n        CGPoint pt = [value CGPointValue];\n        \n        if(pt.x \u003C leftPtX) {\n            leftPtX = pt.x;\n        }\n        \n        if(pt.x > rightPtX) {\n            rightPtX = pt.x;\n        }\n        \n        if(pt.y \u003C topPtY) {\n            topPtY = pt.y;\n        }\n        \n        if(pt.y > bottomPtY) {\n            bottomPtY = pt.y;\n        }\n    }\n    \n    return CGRectMake(leftPtX,\n                      topPtY,\n                      rightPtX - leftPtX,\n                      bottomPtY - topPtY);\n}\n\n\n@end\n```\n","在iOS中实现一个手写板，通过手势的一些方法，做一个粗糙的实现，仅供大家参考。","",499668042977349,{"phone":15,"userId":13,"nickName":16,"vipType":17,"avatar":18,"sign":19,"createdAt":20},"13121171998","全栈老韩",1,"https://image.xinwei.ltd/images/IMG_5430.JPG","全栈工程师，擅长iOS App开发、前端（vue、react、nuxt、小程序&Taro）开发、Flutter、React Native、后端（midwayjs、golang、express、koa）开发、docker容器、seo优化等。","2024-01-01T16:14:30.305Z",2,{"id":21,"name":23},"IT技术",3,{"id":24,"name":26,"parentId":21},"iOS",[],30,0,"ios开发,ios手势,ios手写板,ios canvas"]