2013-04-13 7 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を学び始めました。アルファ領域を持つ画像について考えると、この領域は不規則なものかもしれません。私はちょうど私がイメージに私の指を動かすと、アルファエリア上のポイントを描画したい。 – 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:メソッドを実装することだけです。これがどのように機能すべきかをコードから知ることは難しいです。

関連する問題