2013-04-13 6 views
5

Chcę narysować przezroczysty obszar za pomocą pędzla, ale mój kod działa niezbyt dobrze. Myślę, że ktoś może mi w tym pomóc. Mój kod:Jak mogę wypełnić przezroczysty obszar obrazu, gdy mój palec przesunął się na

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

Jaki jest dokładnie Twój problem? Proszę wyjaśnić, na czym polega problem (y), ponieważ nie możemy zgadnąć, patrząc na kod. Poinformuj nas, co dzieje się źle po uruchomieniu aplikacji. – klcjr89

+1

Jest bardzo dobry film WWDC 2012 pokazujący, jak "malować palcem". – matt

+0

Zacząłem uczyć się Objective-c przy tworzeniu aplikacji do rysowania, Myśląc o obrazie z obszarem alfa, a ten obszar może być nieregularny. Chcę tylko narysować punkt w obszarze alfa, kiedy przesunąłem palec po obrazie. – bencore

Odpowiedz

0

Ważną rzeczą jest wiedzieć, że należy zrobić rzeczywisty rysunek w drawRect: swojej UIView. Dlatego metoda w kodzie renderLineFromPoint:toPoint: należy po prostu budując szereg linii i mówienie widok przerysować za każdym razem, coś takiego:

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

ta zakłada, że ​​masz klasę linii, która ma 2 CGPoint właściwości. Twój drawRect: może wyglądać mniej więcej tak:

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

Jeśli zrobisz to w ten sposób (bez OpenGL) nie ma potrzeby, aby odwrócić oś y.

Pozostaje tylko wdrożyć metodę isPointTransparent:. Z Twojego kodu trudno się dowiedzieć, jak to powinno działać.

Powiązane problemy