Je suis nouveau dans ios.And j'ai besoin de créer un textview ou une étiquette dans laquelle je peux signer.Comment dessiner Signature sur UIView
Vous aimez cette image.
Je suis nouveau dans ios.And j'ai besoin de créer un textview ou une étiquette dans laquelle je peux signer.Comment dessiner Signature sur UIView
Vous aimez cette image.
Vous pouvez dessiner sur la signature UIView
pour cette première subclass UIView
et votre sous-classe de UIView
devrait être quelque chose comme,
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];
}
Ensuite, vous pouvez import SignatureView.h
dans tout votre contrôleur de vue et peut instancier vue signature quelque chose comme,
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];
Et ce point de vue, vous pouvez dessiner votre signature!
Et vous pouvez appeler la méthode erase
à erase
la signature!
Ceci est ma solution.
D'abord, j'ai créé la classe SignatureDrawView. Dans la classe SignatureDrawView j'ai écrit la fonction pour dessiner la signature.
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
Suivante dans ViewController j'ai créé le UIImageView avec UIView.
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
REMARQUE: Lorsque vous définissez la vue inspecteur première identité xib (il reste > côté dans les services publics Ensuite, cliquez sur la liste déroulante de la classe (il fait partie de Custom Class) .Sélectionnez ou choisissez SignatureDrawView.Après au crochet la vue de xib ou storyboard à ViewController.h
Ci-dessous les captures d'écran de sortie
également
Merci votre code fonctionne aussi parfaitement. – Muju
Bienvenue Muju- :) – user3182143
Si ma réponse est meilleure que la première réponse et qu'il est bon pour vous s'il vous plaît l'accepter. – user3182143
https://www.raywenderlich.com/18840/how-to-make-a -simple-dessin-app-avec-uikit? – Larme