2013-04-13 50 views
5

我想用画笔绘制透明区域,但我的代码工作不太好。我认为有人可以帮助我。我的代码:当我的手指移动时,如何填充图像的透明区域

// 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

你的问题到底是什么?请详细说明您遇到的问题,因为我们不能通过查看代码来进行猜测。告诉我们您运行应用程序时出了什么问题。 – klcjr89

+1

有一个很好的WWDC 2012视频展示了如何“用手指画”。 – matt

+0

我已经开始学习Objective-c与绘制应用程序,思考与alpha区域的图像,并且这个区域可能不规则。当我将手指放在图像上时,我只想在alpha区域绘制点。 – bencore

回答

0

重要的是要知道的是,你应该做你的实际绘制在UIViewdrawRect:。因此,在你的代码renderLineFromPoint:toPoint:方法应该只建立线的阵列,并告诉视图每次重绘,这样的:

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

这假定您已经有2个CGPoint特性的线路类。你可以drawRect:看起来是这样的:

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

如果你不喜欢这样(没有的OpenGL)没有必要翻转Y轴。

剩下的唯一事情就是实施isPointTransparent:方法。从代码中很难知道这应该如何工作。

相关问题