6

ブラシで透明な領域を描きたいのですが、コードがうまく機能しません。誰かが私を助けてくれると思います。私のコード:

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

1 に答える 1

0

知っておくべき重要なことは、実際の描画は の で行う必要があるということdrawRect:ですUIView。したがって、renderLineFromPoint:toPoint:コード内のメソッドは、次のように、行の配列を構築し、毎回再描画するようにビューに指示するだけである必要があります。

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

これは、2 つのプロパティを持つ Line クラスがあることを前提としてい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:あとはメソッドを実装するだけです。これがどのように機能するかをコードから知ることは困難です。

于 2013-04-17T18:51:18.427 に答える