简体   繁体   中英

how can i fill the transparent area of the image when my finger moved on

I wanna draw the transparent area with brush, but my code work not very well.I think someone can help me here. My code :

// Handles the start of a touch

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGRect bounds = [self bounds];
    UITouch *touch = [[event touchesForView:self] anyObject];

    if (![image isPointTransparent:[touch locationInView:self]]
       || ![image isPointTransparent:[touch previousLocationInView:self]]) 
    {
        return;
    }

firstTouch = YES;

    // Convert touch point from UIView referential to OpenGL one (upside-down flip)

location = [touch locationInView:self];
location.y = bounds.size.height - location.y;
}

// Handles the continuation of a touch.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{  
    CGRect bounds = [self bounds];
    UITouch *touch = [[event touchesForView:self] anyObject];

    if (![image isPointTransparent:[touch locationInView:self]] 
        || ![image isPointTransparent:[touch previousLocationInView:self]]) 
    {
        return;
    }

// Convert touch point from UIView referential to OpenGL one (upside-down flip)
if (firstTouch) 
    {
    firstTouch = NO;
        previousLocation = [touch previousLocationInView:self];
    previousLocation.y = bounds.size.height - previousLocation.y;
} 
    else 
    {
    location = [touch locationInView:self];
    location.y = bounds.size.height - location.y;
    previousLocation = [touch previousLocationInView:self];
    previousLocation.y = bounds.size.height - previousLocation.y;
}

// Render the stroke
[self renderLineFromPoint:previousLocation toPoint:location];
}

// Handles the end of a touch event when the touch is a tap.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGRect bounds = [self bounds];
    UITouch *touch = [[event touchesForView:self] anyObject];

    if (![image isPointTransparent:[touch locationInView:self]] || ![image isPointTransparent:[touch previousLocationInView:self]]) 
    {  
        return;
    }

    if (firstTouch) 
    {
       firstTouch = NO;
       previousLocation = [touch previousLocationInView:self];
       previousLocation.y = bounds.size.height - previousLocation.y;
       [self renderLineFromPoint:previousLocation toPoint:location];
}
}

The important thing to know is that you should do your actual drawing in the drawRect: of your UIView . So the renderLineFromPoint:toPoint: method in your code should just be building up an array of lines and telling the view to redraw each time, something like this:

- (void)renderLineFromPoint:(CGPoint)from toPoint:(CGPoint)to
{
  [lines addObject:[Line lineFrom:from to:to]];
  [self setNeedsDisplay];
}

This assumes that you have a Line class which has 2 CGPoint properties. Your drawRect: may look something like this:

- (void)drawRect:(CGRect)rect
{
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetRGBStrokeColor(context, 0.0f, 0.0f, 0.0f, 1.0f);
  for (Line *line in lines) {
    CGContextMoveToPoint(context, line.from.x, line.from.y);
    CGContextAddLineToPoint(context, line.to.x, line.to.y);
    CGContextStrokePath(context);
  }
}

If you do it like this (without OpenGL) there is no need to flip the y-axis.

The only thing left is to implement the isPointTransparent: method. It's difficult to know from your code how this should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM