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

1

6

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];
}
}
Uphroe answered 13/4, 2013 at 2:58 Comment(3)
What exactly is your problem? Please elaborate as to what the issue(s) you're experiencing are, as we cannot just guess by looking at your code. Tell us what's going wrong when you run your application.Anasarca
There's a very good WWDC 2012 video showing how to "paint with your finger".Propene
I've started to learn Objective-c with making drawing app,Thinking about an image with alpha area, and this area maybe irregular. I just want to drawn point on the alpha area when I moved my finger on the image.Uphroe
C
0

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.

Courage answered 17/4, 2013 at 18:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.