2012-03-28 12 views
2

Hallo Ich arbeite an diesem Projekt, um Benutzer doodle auf einem UIView zu ermöglichen. Meine Vorgehensweise besteht darin, einen CGMutablePathRef-Pfad zu erstellen und während TouchMoved neue Zeilen hinzuzufügen.doodle auf UIView - iOS-Entwicklung

Die relevanten Codes sind unten aufgeführt, die in der Klasse von UIView sind.

static CGMutablePathRef path; //create it as static value. 
//I got this habit from java coding but actually not sure 
//if it's a good way to do it in objective-c. 


//draw the path 

-(void)drawRect:(CGRect)rect{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextBeginPath(context); 
    CGContextAddPath(context, path); 
    CGContextStrokePath(context); 
    CGPathRelease(path); 
} 

//touch began. create the path and add point. 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 

    path = CGPathCreateMutable(); 
    UITouch *touch = [touches anyObject]; 
    CGPoint point = [touch locationInView:self]; 
    CGPathMoveToPoint(path, NULL, point.x, point.y); 
    [self setNeedsDisplay];    
} 


//add points when touch moved 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 

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

    CGPathAddLineToPoint(path, NULL, point.x, point.y); 
    [self setNeedsDisplay]; 

} 

//last points when touch ends 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint point = [touch locationInView:self]; 
    CGPathAddLineToPoint(path, NULL, point.x, point.y); 
    [self setNeedsDisplay]; 
} 

Allerdings habe ich Fehler und ich habe sie nicht wirklich verstehen ... Ich denke, es ist etwas, mit UIGestureRecognizer, die ich von anderen doodle Codes sah. Ist es notwendig, UIGestureRecognizer hinzuzufügen? Ich habe nichts darüber gelernt, also versuchte ich es zu vermeiden. Aber wenn es ein Muss ist, werde ich es versuchen.

Ein anderer Ansatz, den ich denke, ist, alle Punktpositionen in einem veränderbaren Array zu speichern, weil ich diese Punktpositionswerte irgendwo speichern muss. Ich weiß jedoch nicht, ob Array geeignet ist, weil ich keine Möglichkeit gefunden habe, schwebende Werte im Array zu speichern. Es wird sehr hilfreich sein, wenn mir jemand dabei helfen kann. Vielen Dank!

+0

Sie müssen UIGestureRecognizers nicht verwenden. Wie zum Speichern von Floats zu NSMutableArray werfen Sie einen Blick auf [[NSNumber] (http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html) numberWithFloat: ]. Werfen Sie auch einen Blick auf diese Frage: http://stackoverflow.com/questions/7393058/how-doi-i-use-uibezierpath-to-draw-a-line-that-follows-a-touch-event –

+0

I ' Ich empfehle, eine Gestenerkennung zu verwenden. Es macht Ihren Code einfacher und einfacher zu erweitern. – Sven

+0

@rokjarc Ja die verknüpfte Frage ist sehr hilfreich. Noch eine kleine Frage: Die Klasse, die in dieser Antwort verwendet wird, ist UIImageView, also in den aufgerufenen Methoden [self.image drawInRect: CGRectMake (0, 0, self.frame.size.width, self.frame.size.height)]. Aber in meinem Fall ist es UIView, also welche Methode soll ich zeichnen? (Sorry, ich denke, es ist eine dumme Frage. Ich bin wirklich ein Newb.) Danke! – user1299082

Antwort

1

Danke an Leute, die mir Hilfe anboten. Nachdem ich durch eine Reihe von Codes und Ansätzen geschaut hatte, fand ich zum Glück den effektiven Weg, dieses Problem zu lösen!

Im Allgemeinen wird die Bildansicht in meiner SketchView beim Zeichnen dynamisch aktualisiert. Das heißt, jedes Mal, wenn ich mehr Pixel zeichne, wird dem neuen Pixel eine Linie hinzugefügt, die UIImageView geändert und dann als Hintergrundbild von UIView festgelegt.

SketchView.h

@property (assign, nonatomic) CGPoint CurrentPoint; 
@property (assign, nonatomic) CGPoint PreviousPoint; 
@property (assign, nonatomic) CGPoint InitialPoint; 

SketchView.m

@synthesize CurrentPoint; 
@synthesize PreviousPoint; 
@synthesize InitialPoint; 


//begin the touch. store the initial point because I want to connect it to the last 
//touch point 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint point = [touch locationInView:image]; 
    InitialPoint = point; 

    } 


//When touch is moving, draw the image dynamically 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 

UITouch *touch = [touches anyObject]; 
PreviousPoint = [touch previousLocationInView:image]; 
CurrentPoint = [touch locationInView:image]; 
UIGraphicsBeginImageContext(image.frame.size); 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 
[image.image drawInRect:CGRectMake(0, 0, image.frame.size.width, image.frame.size.height)]; 
CGContextSetLineCap(ctx, kCGLineCapRound); 
CGContextSetLineWidth(ctx, 5.0); 
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0); 
CGContextBeginPath(ctx); 
CGContextMoveToPoint(ctx, PreviousPoint.x, PreviousPoint.y); 
CGContextAddLineToPoint(ctx, CurrentPoint.x, CurrentPoint.y); 
CGContextStrokePath(ctx); 
image.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
} 



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

UITouch *touch = [touches anyObject]; 
PreviousPoint = [touch previousLocationInView:image]; 
CurrentPoint = [touch locationInView:image]; 
UIGraphicsBeginImageContext(image.frame.size); 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 
[image.image drawInRect:CGRectMake(0, 0, image.frame.size.width, image.frame.size.height)]; 
CGContextSetLineCap(ctx, kCGLineCapRound); 
CGContextSetLineWidth(ctx, 5.0); 
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0); 
CGContextBeginPath(ctx); 
CGContextMoveToPoint(ctx, PreviousPoint.x, PreviousPoint.y); 
CGContextAddLineToPoint(ctx, CurrentPoint.x, CurrentPoint.y); 

//I connected the last point to initial point to make a closed region 
CGContextMoveToPoint(ctx, CurrentPoint.x, CurrentPoint.y); 
CGContextAddLineToPoint(ctx, InitialPoint.x, InitialPoint.y); 
CGContextStrokePath(ctx); 
image.image = UIGraphicsGetImageFromCurrentImageContext(); 


UIGraphicsEndImageContext(); 
} 

Und es funktioniert!

p.s. Ich fand

PreviousPoint = [touch previousLocationInView:image]; 

sehr hilfreich, wenn es nicht viel in den Codes erwähnt wird ich fand kritzeln tun ... Ich hoffe, das hilft. :)