2013-10-24 16 views
8

Tôi đã làm theo hướng dẫn trực tuyến để vẽ trong UIView được phân lớp. Các hướng dẫn cho thấy một UIView với một nền trắng, tôi cố định điều này bằng cách thay đổi màu sắc của siêu bg. Vấn đề là, khi chạm vào cuối, nền không rõ ràng. Tôi không có ý kiến. Tôi chỉ đơn giản là cố gắng thiết lập màu tô thành [uicolor clearcolor]; không thành công. Đây là mã tôi đang sử dụng:Nền đen trong 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

vì sự cố nằm trong phương thức 'drawBitmap' của bạn, hãy nhận xét dòng' setStroke' và thấy sự khác biệt. - Chúc may mắn –

Trả lời

16

Đặt sau theo quan điểm của bạn - (id) phương pháp initWithFrame:

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

    self.backgroundColor = [UIColor clearColor]; 

} 

return self; 

Bằng cách đó xem lớp con của bạn sẽ có nền trong suốt = không có màu đen khi vẽ sử dụng các phương thức bezier, CGContext… hoặc bất kỳ thứ gì khác.

Các vấn đề liên quan