2013-10-24 18 views
8

Ich folgte einem Tutorial online, um in eine Unterklasse UIView zu zeichnen. Das Tutorial zeigte eine UIView mit einem weißen Hintergrund, ich habe das behoben, indem ich einfach die bg-Farbe des Super geändert habe. Das Problem ist, wenn die Berührungen enden, bleibt der Hintergrund nicht klar. Ich habe keine Ahnung. Ich habe einfach versucht, die Füllfarbe auf [uicolor clearcolor] zu setzen; vergeblich. Hier ist der Code ich verwende:Schwarzer Hintergrund in UIView?

@implementation CachedLIView 
{ 
    UIBezierPath *path; 
    UIImage *incrementalImage; // (1) 
} 

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

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


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint p = [touch locationInView:self]; 
    [path moveToPoint:p]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint p = [touch locationInView:self]; 
    [path addLineToPoint:p]; 
    [self setNeedsDisplay]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event // (2) 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint p = [touch locationInView:self]; 
    [path addLineToPoint:p]; 
    [self drawBitmap]; // (3) 
    [self setNeedsDisplay]; 
    [path removeAllPoints]; //(4) 
} 

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

- (void)drawBitmap // (3) 
{ 
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0); 
    [[UIColor blackColor] setStroke]; 
    if (!incrementalImage) // first draw; paint background white by ... 
    { 
     UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object 
     [[UIColor clearColor] setFill]; 
     [rectpath fill]; // filling it with white 
    } 
    [incrementalImage drawAtPoint:CGPointZero]; 
    [path stroke]; 
    incrementalImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
} 
+0

als das Problem ist in Ihrer 'drawBitmap' Methode Kommentar 'setStroke' Zeile und sehen Sie den Unterschied. - Viel Glück –

Antwort

16

gebe folgende Ihrer Ansicht nach - (id) initwithframe-Methode:

self = [super initWithFrame:frame]; 
if (self) { 

    self.backgroundColor = [UIColor clearColor]; 

} 

return self; 

Auf diese Weise Ihre Ansicht Unterklasse einen transparenten Hintergrund haben = keine schwarz beim Zeichnen mit Bezier, CGContext ... Methoden oder irgendetwas anderem.

Verwandte Themen