手势(手写)签名

PJRSignatureView


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([lblSignature superview]){//检查父视图
        [lblSignature removeFromSuperview];//<span style="font-family: Arial; font-size: 14px; line-height: 26px;">把当前view从它的父view和窗口中移除,同时也把它从响应事件操作的响应者链中移除</span>
    }//开始书写时使提示标签消失的方法
    control = 0;
    UITouch *touch = [touches anyObject];//返回触摸对象集合
    points[0] = [touch locationInView:self];//返回当前触点坐标
    
    CGPoint startPoint = points[0];
    CGPoint endPoint = CGPointMake(startPoint.x + 1.5, startPoint.y
                              + 2);
    
    [beizerPath moveToPoint:startPoint];//绘制起点
    [beizerPath addLineToPoint:endPoint];//终点
    
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
    control++;
    points[control] = touchPoint;
    
    if (control == 4)//筹4个点绘制一次
    {
        points[3] = CGPointMake((points[2].x + points[4].x)/2.0, (points[2].y + points[4].y)/2.0);
        
        [beizerPath moveToPoint:points[0]];//起点
        [beizerPath addCurveToPoint:points[3] controlPoint1:points[1] controlPoint2:points[2]];//绘制贝塞尔路径
        
        [self setNeedsDisplay];//绘制
        
        points[0] = points[3];
        points[1] = points[4];
        control = 1;
    }
    
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self drawBitmapImage];//绘制签名图片
    [self setNeedsDisplay];
    [beizerPath removeAllPoints];//删除所有点,有效的删除路径
    control = 0;
}

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

- (void)drawBitmapImage
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);//根据指定选项创建位图图形上下文
    
    if (!incrImage)
    {
        UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds];//创建一个矩形路径
        [[UIColor whiteColor] setFill];
        [rectpath fill];
    }
    [incrImage drawAtPoint:CGPointZero];
    
    //Set final color for drawing
    UIColor *strokeColor = FINAL_COLOR;
    [strokeColor setStroke];
    [beizerPath stroke];
    incrImage = UIGraphicsGetImageFromCurrentImageContext();//根据图形上下文返回一个图像
    UIGraphicsEndImageContext();//关闭图形上下文
}

- (void)clearSignature//清空
{
    incrImage = nil;
    [self setNeedsDisplay];
}

- (UIImage *)getSignatureImage {
    
    if([lblSignature superview]){
        return nil;
    }
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
    
    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];//根据图形上下文绘制快照,利用view层次结构并将其绘制到当前的上下文中
    
    UIImage *signatureImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return signatureImage;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值