2013-10-24 11 views
8

オンラインでチュートリアルに従い、サブクラス化されたUIViewを描画しました。チュートリアルでは、背景が白いUIViewが表示されていましたが、これはスーパーのbgカラーを変更するだけで解決しました。問題は、タッチが終わると、背景が明確でないことです。何も思いつきません。塗りつぶしの色を[uicolor clearcolor]に設定しようとしました。失敗しました。ここで私が使用していたコードは次のとおりです。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

問題はあなたの 'drawBitmap'メソッドにありますので、' setStroke'行にコメントを付けてその違いを見てください。 - がんばろう –

答えて

16

ビューに次のように入れて - (ID)initWithFrame方法:

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

    self.backgroundColor = [UIColor clearColor]; 

} 

return self; 

ビューのサブクラスは=描画黒いを透明な背景を持っていませんその方法ベジエ、CGContext ...メソッドなどを使用します。

関連する問題