当我的手指移动时,如何填充图像的透明区域

时间:2013-04-13 02:58:03

标签: iphone ios objective-c uiimageview brushes

我想用画笔绘制透明区域,但我的代码工作得不是很好。我想有人可以帮助我。我的代码:

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

1 个答案:

答案 0 :(得分:0)

要知道的重要一点是,您应该在drawRect:的{​​{1}}内进行实际绘图。因此,代码中的UIView方法应该只是构建一个行数组并告诉视图每次重绘,如下所示:

renderLineFromPoint:toPoint:

这假设您有一个具有2个- (void)renderLineFromPoint:(CGPoint)from toPoint:(CGPoint)to { [lines addObject:[Line lineFrom:from to:to]]; [self setNeedsDisplay]; } 属性的Line类。您的CGPoint可能如下所示:

drawRect:

如果这样做(没有OpenGL),则无需翻转y轴。

唯一剩下的就是实现- (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); } } 方法。从你的代码中很难知道它应该如何工作。