0

サブビューの図面をクリアするにはどうすればよいですか?以下の(void)clearLineに何を入れる必要がありますか?

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

    // Setup subview to draw a line

    CGRect subviewRect = CGRectMake(0, 0, 250, 300);
    Draw* _draw = [[Draw alloc] initWithFrame:subviewRect];
    _draw.exclusiveTouch = NO;
    _draw.backgroundColor = [UIColor clearColor];
    [self addSubview:_draw];
    }
    return self;
}

- (void) clearLine
    {
    // how can I clear the drawing in Draw
    }

以下はDraw.mファイルで、最初のタッチを開始点、2番目のタッチを終了点として直線を描画します。

@implementation Draw

- (void)drawRect:(CGRect)rect {

    CGPoint fromPoint;
    CGPoint toPoint;

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 2.0);
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
    CGColorRef color = CGColorCreate(colorspace, components);
    CGContextSetStrokeColorWithColor(context, color);
    CGContextSetLineCap(context, kCGLineCapRound);

    CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
    CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
    CGContextStrokePath(context);
    CGColorSpaceRelease(colorspace);
    CGColorRelease(color);
}    

// Touch event
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch* touchPoint = [touches anyObject];
    fromPoint = [touchPoint locationInView:self];
    toPoint = [touchPoint locationInView:self];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch* touch = [touches anyObject];
    toPoint = [touch locationInView:self];    
    [self setNeedsDisplay];
}

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

3 に答える 3

3

明確な線で、描画ビューを削除し、nilに設定する必要があります

Draw* _draw;

- (void) clearLine
{
  [_draw removeFromSuperview];
  _draw=nil;

}

再び描画の使用を有効にする

-(void) initDrawView
{
    CGRect subviewRect = CGRectMake(0, 0, 250, 300);
    if(_draw!=nil)
    {
     [_draw removeFromSuperview];
     _draw=nil;
     }
     _draw = [[Draw alloc] initWithFrame:subviewRect];
    _draw.exclusiveTouch = NO;
    _draw.backgroundColor = [UIColor clearColor];
    [self addSubview:_draw];

}
于 2013-02-11T09:59:45.137 に答える
1

ビューに背景色を描画し、

あなたが白い背景を持っている[UIColor whiteColor];
なら、あなたが黒い背景を持っているなら、[UIColor blackColor];

于 2013-02-11T09:59:55.067 に答える
1

DrawクラスでBool変数を作成します

そして、あなたがこのようにすべてをクリアしたいとき

- (void) clearLine
{
// Here you can clearn lines 
 [_draw CleanAllLines]
}

そして、Drawクラスでメソッドを作成します

-(void)CleanAllLines{
isNeedToClear = YES;
[self setNeedsLayout];
}

- (void)drawRect:(CGRect)rect {
if(isNeedToClear){
isNeedToClear = NO;
return;
}

CGPoint fromPoint;
CGPoint toPoint;

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context, 2.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextSetLineCap(context, kCGLineCapRound);

CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);

}

于 2013-02-11T10:03:10.740 に答える