Iphone 在drawRect中清除图形

Iphone 在drawRect中清除图形,iphone,Iphone,如何清除子视图中的图形?我应该在下面的(无效)空白行中添加什么 - (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] ini

如何清除子视图中的图形?我应该在下面的(无效)空白行中添加什么

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

在“清除线”中,应删除“绘制视图”,并将其设置为零

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

}

在“清除线”中,应删除“绘制视图”,并将其设置为零

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

}

在视图上绘制背景色

就像你有白色背景,然后
[UIColor-whiteColor]

如果您有黑色背景,则
[UIColor blackColor]

在视图上绘制背景色

就像你有白色背景,然后
[UIColor-whiteColor]

如果您有黑色背景,则
[UIColor blackColor]

在Draw类中生成布尔变量

当你想把这些都清理干净的时候

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

}在Draw类中创建布尔变量

当你想把这些都清理干净的时候

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

}

线消失了,但我画不出来again@boogiedoll现在我已经更新了答案,你可以找到正确的!线不见了,但我画不出来again@boogiedoll现在我已经更新了答案,你可以找到正确的!我在一张照片上画画我在一张照片上画画image@boogiedoll我的荣幸:)@boogiedoll我的荣幸:)