2013-04-13 2 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: 메서드를 구현하는 것입니다. 이것이 어떻게 작동하는지 코드에서 알기가 어렵습니다.