2010-07-06 5 views
0

터치 제스처에서 직사각형이나 삼각형을 그릴 때 문제가 있습니다. 다음은 코드입니다.아이폰에 그림 그리기

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInView:self]; 

    // move the cannon to the touched location 

    x = location.x; 

    [self moveCannon]; 

    [self setNeedsDisplay]; 

} 

-(void) moveCannon 
{ 
    // draw the cannot as a triangle for right now 


    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPathMoveToPoint(path, NULL, 20.0f, 100.0f); 
    CGPathAddLineToPoint(path, NULL, 50.0f, 100.0f); 
    CGPathAddLineToPoint(path, NULL, 50.0f, 20.0f); 
    CGPathAddLineToPoint(path, NULL, 50.0f, 100.0f); 
    CGPathCloseSubpath(path); 

    CGContextSetFillColorWithColor(self.context, [UIColor redColor].CGColor); 
    CGContextAddPath(self.context, path); 
    CGContextFillPath(self.context); 

} 

무엇이 누락 되었습니까?

답변

2

두 가지 가능한 문제점이 있습니다. 우선, 이벤트를 처리 할 때가 아니라, 뷰의 drawRect 메소드로 대포를 드로잉 (Drawing)해야한다. 현재 코드가 경로를 칠한 다음 다시 표시 이벤트를 게시하면 대포가 지워질 수 있습니다.

다른 가능한 문제는 위치를 저장하더라도 실제로 사용하지 않는 것입니다. 그래서 대포가 나타나지만 움직이지 않는 경우 그 이유가 있습니다.

희망이 도움이됩니다.

+0

내 맞춤법을 제대로 호출하지 않아서 문제가 발견되었습니다. – azamsharp