2016-05-23 1 views
2

Je travaille sur la signature numérique sur uiview. Je le crée normalement par ce code, mais je ne suis pas en mesure de supprimer bezier chemin sur le bouton click.i suis le partage de mon code S'il vous plaît regarder mon code.Comment vais-je supprimer UIBezierPath de l'uiview sur le clic du bouton.

#import "Signature.h" 

@implementation Signature { 
UIBezierPath *path; 
UIImage *incrementalImage; 
CGPoint pts[5]; // we now need to keep track of the four points of a Bezier segment and the first control point of the next segment 
uint ctr; 
    } 

    - (id)initWithCoder:(NSCoder *)aDecoder 
    { 
if (self = [super initWithCoder:aDecoder]) 
{ 
    [self setMultipleTouchEnabled:NO]; 
    [self setBackgroundColor:[UIColor greenColor]]; 
    path = [UIBezierPath bezierPath]; 
    [path setLineWidth:2.0]; 
} 
return self; 

    } 

    - (id)initWithFrame:(CGRect)frame 
{ 
self = [super initWithFrame:frame]; 
if (self) { 
    [self setMultipleTouchEnabled:NO]; 
    path = [UIBezierPath bezierPath]; 
    [path setLineWidth:2.0]; 
} 
return self; 
    } 

- (void)drawRect:(CGRect)rect 
    { 
[incrementalImage drawInRect:rect]; 
[path stroke]; 
} 

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
ctr = 0; 
UITouch *touch = [touches anyObject]; 
pts[0] = [touch locationInView:self]; 
    } 

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
     { 
UITouch *touch = [touches anyObject]; 
CGPoint p = [touch locationInView:self]; 
ctr++; 
pts[ctr] = p; 
if (ctr == 4) 
{ 
    pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment 

    [path moveToPoint:pts[0]]; 
    [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2] 

    [self setNeedsDisplay]; 
    // replace points and get ready to handle the next segment 
    pts[0] = pts[3]; 
    pts[1] = pts[4]; 
    ctr = 1; 
} 
     } 

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
[self drawBitmap]; 
[self setNeedsDisplay]; 
[path removeAllPoints]; 
ctr = 0; 
    } 

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

    - (void)drawBitmap 
     { 
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0); 

if (!incrementalImage) // first time; paint background white 
{ 
    UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; 
    [[UIColor greenColor] setFill]; 
    [rectpath fill]; 
} 
[incrementalImage drawAtPoint:CGPointZero]; 
[[UIColor blackColor] setStroke]; 
[path stroke]; 
incrementalImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
    } 

    - (void)erase { 
NSLog(@"Erase Testing..."); 
// self->path = nil; //Set current path nil 
//path = [UIBezierPath bezierPath]; 
// [self setNeedsDisplay]; 
    } 

I appeler la méthode d'effacement sur un clic déboutonner d'une autre classe, mais je ne suis pas en mesure de supprimer uibezeripath.

+0

'incrementalImage' est l'image résultante? et vous définissez sur n'importe quelle imageview? – Lion

+0

non monsieur je viens d'ajouter un uiview et rien d'autre. J'ajoute uiview dans le contrôleur de vue et donne une signature de classe –

+0

Quel est le problème exact? Je n'obtiens pas ce dont vous avez besoin – Lion

Répondre

1

Vérifiez comment je me sers de ce dans l'un de mon projet si elle peut aider,

- (void)drawRect:(CGRect)rect { 

    [_path stroke]; 

} 


- (id)initWithFrame:(CGRect)frame{ 

self = [super initWithFrame: frame]; 

if (self) { 
    // set Multiple touches Enabled to false for allow only single touch. 
    [self setMultipleTouchEnabled: NO]; 
    _path = [UIBezierPath bezierPath]; 
    // set Line width. 
    [_path setLineWidth:2.0]; 

} 
return self; 
} 

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

isMouseswipped = NO; //for touches eneded to make dot 
ctr = 0; 
UITouch *myTouch = [touches anyObject]; 
pts[0] = [myTouch locationInView: self]; 


} 

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

isMouseswipped = YES; //for touches eneded to make dot 
CustomSignatureViewController *csvc = [[CustomSignatureViewController alloc]init]; 

UITouch *touch = [touches anyObject]; 
CGPoint p = [touch locationInView: self]; 
ctr++; 
pts[ctr] = p; 

if (ctr == 4) { 

    pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); 
    [_path moveToPoint: pts[0]]; 
    [_path addCurveToPoint: pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; 
    [self setNeedsDisplay]; 
    pts[0] = pts[3]; 
    pts[1] = pts[4]; 
    ctr = 1; 

    csvc.status = 1; 
    self.status = 1; 
    } 


} 


- (void)erase { 

_path = nil; 

_path = [UIBezierPath bezierPath]; 
[_path setLineWidth:2.0]; 
[self setNeedsDisplay]; 

} 

C'est classe personnalisée qui renvoie UIView à signature fraw. Il s'agit d'une sous-classe de UIView.

Donc, quand la signature est requise, j'importe cette classe et l'instancie et je reçois l'objet que je peux dessiner. puis capturer cette vue en tant qu'image en utilisant le contexte de début de graphiques!

Mise à jour:

En viewcontroller je fais appel méthode comme,

-(void)clear : (id)sender{ 

    [signView erase]; 

    signView.status = 0; 

} 

signView est la classe de signature (comme mentionné ci-dessus) l'objet de '. et erase est une méthode publique déclarée dans le fichier .h de la classe de signature. (Note: la classe de signature est la sous-classe de UIView si intance de celui-ci retourne vue onject si signView est objet UIView)

Espérons que cela aidera :)

+0

ok monsieur j'ai essayé de l'implémenter juste attendre un min plz –

+0

monsieur je dois poser une question d'où vous appelez la méthode d'effacement? –

+0

J'appelle la méthode d'effacement d'une autre classe qui est mon contrôleur de vue Dans cette classe je fais l'instance de cette classe qui a mentionné dans la réponse er et j'ai une autre méthode dans la classe viewcontroller pour capturer l'image à partir de cette vue après avoir dessiné la signature – Lion

0

utiliser ce code, il vous aidera à

- (void)resetPath { 
    path = nil; 
    path = [UIBezierPath bezierPath]; 
    [self setNeedsDisplay]; 
} 

créer cette méthode dans le fichier .h de votre subclass. De votre classe UIViewController vous appelez cette méthode à chaque fois que vous en avez besoin

- (IBAction)youraction:(id)sender 
{ 
    [self.subclassedView resetPath]; 
} 
+0

son ne fonctionne pas bro –

+0

essayez ce rootView.layer.sublayers = nil; –

+0

Merci pour votre réponse monsieur –

0

Essayez d'utiliser cette option pour supprimer le UIBezierPath,

[path removeAllPoints]; 

Si vous utilisez CAShapeLayer *rectLayer = [[CAShapeLayer alloc] init]; aswell ensuite appeler cette ligne aussi,

[rectLayer removeFromSuperlayer];