清除drawRect中的绘图

时间:2013-02-11 09:51:37

标签: iphone

如何清除子视图中的图形?我应该在下面的(无效)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文件,它将第一次触摸作为起点,第二次触摸作为结束点绘制直线。

@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 个答案:

答案 0 :(得分:3)

在清晰的行中,您应该删除Draw视图并将其设置为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];

}

答案 1 :(得分:1)

在视图上绘制背景颜色

如果你有白色背景,那么就像[UIColor whiteColor];一样 如果你有黑色背景,那么[UIColor blackColor];

答案 2 :(得分: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);

}

相关问题