2013-02-11 187 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