2013-02-11 185 views
0

如何清除子視圖中的圖形?我應該在我的(無效)clearLine下面放什麼?清除drawRect中的圖形

- (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文件,將通過獲得第一觸摸爲出發點和第二觸摸爲終點畫一條直線。

@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]; 
} 

回答

3

在明確的界限,你應該刪除繪製視圖,並將其設置爲零

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]; 

} 
+0

它的工作原理!謝謝 – askingtoomuch 2013-02-11 12:46:25

+0

@boogiedoll我的榮幸:) – BhushanVU 2013-02-11 12:59:33

1

畫上你的觀點的背景顏色,

如果你有白色背景,那麼[UIColor whiteColor];
,如果你有黑色的背景,然後[UIColor blackColor];

+0

我實際上是在圖像上繪製 – askingtoomuch 2013-02-11 12:36:40

1

請在繪圖類中的布爾變量

而且當你要清除所有這樣

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

並在繪圖類使該方法

-(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); 

}

+0

這條線消失了但我不能再畫 – askingtoomuch 2013-02-11 12:35:37

+0

@boogiedoll現在我已經更新了答案,你可以找到正確的! – SachinVsSachin 2013-02-11 13:05:31