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;
}