如何在UIView上绘制签名 [英] How to draw Signature on UIView

查看:122
本文介绍了如何在UIView上绘制签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ios的新手,我需要创建一个可以在其中签名的textview或标签.

I am new in ios.And I need to create a textview or label in which i can sign.

喜欢这张图片.

推荐答案

您可以在UIView上为第一个subclass UIView绘制签名,并且UIView的子类应该类似于

You can draw signature on UIView for that first subclass UIView and your subclass of UIView should be something like,

SignatureView.h

 #import <UIKit/UIKit.h>

@interface SignatureView : UIView{

UIBezierPath *_path;
}
- (void)erase;
@end

SignatureView.m

 #import "SignatureView.h"

@implementation SignatureView


- (void)drawRect:(CGRect)rect {

_path.lineCapStyle = kCGLineCapRound;
[_path stroke];
}
- (id)initWithFrame:(CGRect)frame{

self = [super initWithFrame: frame];

if (self) {


    [self setMultipleTouchEnabled: NO];
    _path = [UIBezierPath bezierPath];
    [_path setLineWidth:2.0];


}
return self;
 }

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {



UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[_path moveToPoint:[mytouch locationInView:self]];
[_path addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];


}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {



UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[_path addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];



 }


 - (void)erase {

_path   = nil;  //Set current path nil

_path   = [UIBezierPath bezierPath]; //Create new path
[_path setLineWidth:2.0];
[self setNeedsDisplay];



  }

然后,您可以在任何视图控制器中import SignatureView.h并可以实例化签名视图,

Then you can import SignatureView.h in any your view controller and can instantiate signature view something like,

   SignatureView *signView= [[ SignatureView alloc] initWithFrame: CGRectMake(10, 10, self.view.frame.size.width-40, 200)];
[signView setBackgroundColor:[UIColor whiteColor]];
signView.layer.borderColor = [[UIColor lightGrayColor]CGColor];
signView.layer.borderWidth = 1.0;
[self.view addSubview:signView];

然后在该视图上可以绘制您的签名!

And on that view you can draw your signature!

您可以调用erase方法来erase签名!

And you can call erase method to erase the signature!

这篇关于如何在UIView上绘制签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆