【问题标题】:How to draw Signature on UIView如何在 UIView 上绘制签名
【发布时间】:2016-09-23 11:57:12
【问题描述】:

我是 ios 新手。我需要创建一个可以签名的文本视图或标签。

喜欢这张图片。

【问题讨论】:

标签: objective-c uiview uibezierpath


【解决方案1】:

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

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 并可以实例化签名视图,例如,

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

你可以在那个视图上画你的签名!

你可以调用erase方法对erase签名!

【讨论】:

  • IS SignatureView 是一个类别。
  • 它工作完美,谢谢。你能告诉我如何保存和清除它。
  • 为了清楚你可以调用擦除方法。为了保存,您可以通过UIGraphicsBeginImageContextUIGraphicsGetImageFromCurrentImageContext() 从视图中获取图像。参考http://stackoverflow.com/questions/746421/iphone-saving-current-view-as-image
  • 你需要用SignatureView的当前实例调用erase方法,你已经绘制签名并想要擦除!
  • @Lion 我已经尝试过这段代码,但每次我运行它都会得到excessBadAccess -[__NSCFType moveToPoint:]: unrecognized selector sent to instance。知道我可能会错过什么吗?
【解决方案2】:

这是我的解决方案。

首先我创建了 SignatureDrawView 类。在 SignatureDrawView 类中,我编写了绘制签名的函数。

SignatureDrawView.h

#import <UIKit/UIKit.h>

@interface SignatureDrawView : UIView

@property (nonatomic, retain) UIGestureRecognizer *theSwipeGesture;
@property (nonatomic, retain) UIImageView *drawImage;
@property (nonatomic, assign) CGPoint lastPoint;
@property (nonatomic, assign) BOOL mouseSwiped;
@property (nonatomic, assign) NSInteger mouseMoved;

- (void)erase;
- (void)setSignature:(NSData *)theLastData;
- (BOOL)isSignatureWrite;

@end

SignatureDrawView.m

#import "SignatureDrawView.h"

@implementation SignatureDrawView

@synthesize theSwipeGesture;
@synthesize drawImage;
@synthesize lastPoint;
@synthesize mouseSwiped;
@synthesize mouseMoved;

#pragma mark - View lifecycle

- (id)initWithFrame:(CGRect)frame
{
   self = [super initWithFrame:frame];
   if (self) {
    // Initialization code
    }
   return self;
}

- (id)initWithCoder:(NSCoder*)coder 
{
    if ((self = [super initWithCoder:coder]))
    {
      drawImage = [[UIImageView alloc] initWithImage:nil];
      drawImage.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
      [self addSubview:drawImage];
      self.backgroundColor = [UIColor whiteColor];
      mouseMoved = 0;
    }
    return self;
 }

 #pragma mark touch handling

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches)
{
     NSArray *array = touch.gestureRecognizers;
     for (UIGestureRecognizer *gesture in array)
     {
        if (gesture.enabled & [gesture isMemberOfClass:[UISwipeGestureRecognizer class]])
        {
            gesture.enabled = NO;
            self.theSwipeGesture = gesture;
        }
      }
   }

   mouseSwiped = NO;
   UITouch *touch = [touches anyObject];

   lastPoint = [touch locationInView:self];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
   mouseSwiped = YES;

   UITouch *touch = [touches anyObject];
   CGPoint currentPoint = [touch locationInView:self];

   UIGraphicsBeginImageContext(self.frame.size);
   [drawImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
   CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
   CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0);
   CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
   CGContextBeginPath(UIGraphicsGetCurrentContext());
   CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
   CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
   CGContextStrokePath(UIGraphicsGetCurrentContext());
   drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   lastPoint = currentPoint;

   mouseMoved++;

   if (mouseMoved == 10) {
    mouseMoved = 0;
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
   if(!mouseSwiped)
   {
      UIGraphicsBeginImageContext(self.frame.size);
      [drawImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
      CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
      CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0);
      CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
      CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
      CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
      CGContextStrokePath(UIGraphicsGetCurrentContext());
      CGContextFlush(UIGraphicsGetCurrentContext());
      drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
     }
    self.theSwipeGesture.enabled = YES;
    mouseSwiped = YES;
 }

#pragma mark Methods

- (void)erase
{
   mouseSwiped = NO;
   drawImage.image = nil;
}

- (void)setSignature:(NSData *)theLastData
{
    UIImage *image = [UIImage imageWithData:theLastData];
    if (image != nil) 
    {
      drawImage.image = [UIImage imageWithData:theLastData];
      mouseSwiped = YES;
    }
 }

 - (BOOL)isSignatureWrite
 {
   return mouseSwiped;
 }

 @end

接下来在 ViewController 中,我使用 UIView 创建了 UIImageView。

ViewController.h

#import <UIKit/UIKit.h>
#import "SignatureDrawView.h"

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet SignatureDrawView *drawSignView;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize drawSignView;

- (void)viewDidLoad 
{
  [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)actionSave:(id)sender 
{
   // code for save the signature
    UIGraphicsBeginImageContext(self.drawSignView.bounds.size); 
    [[self.drawSignView.layer presentationLayer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *postData = UIImageJPEGRepresentation(viewImage, 1.0);
    ....Then do your stuff to save this in DB or server
}

- (IBAction)actionCancel:(id)sender 
{
   //code for cancel the signature
   [self.drawSignView erase];
}

- (IBAction)actionClear:(id)sender 
{
    //code for clear the signature
    [self.drawSignView erase];
}
@end

注意:当您在 xib 第一个身份检查器中设置视图时(它是左侧 > 在实用程序中)。然后单击类的下拉框(它是 Custom Class).Select 或选择 SignatureDrawView.After 连接 从 xib 或 storyboard 到 ViewController.h 的视图

下面是输出截图

还有

【讨论】:

  • 欢迎穆朱-:)
  • 如果我的回答比第一个回答好并且对你有好处,请接受。
  • 谢谢你。 Obj-C 的东西越来越难找到了 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-22
相关资源
最近更新 更多