2011-01-12 5 views
2

헤이 목적 C의 메신저 초보자는 터치 온 라인 그리기 방법?

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

UITouch *touch = [touches anyObject]; 
if ([touch view] == self.view) { 

    CGPoint location = [touch locationInView:self.view]; 
    loc1 = location; 
    CGContextMoveToPoint(context, location.x, location.y); 
    NSLog(@"x:%d y:%d At Touch Begain", loc1.x, loc1.y); 
} 
} 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 

{ 
    UITouch *touch = [touches anyObject]; 

     if ([touch view] == self.view) 
    { 
    CGPoint location = [touch locationInView:self.view]; 
    CGContextMoveToPoint(context, loc1.x, loc1.y); 
    NSLog(@"x:%d y:%d At Touch Move", loc1.x, loc1.y); 
    CGContextAddLineToPoint(context, location.x, location.y); 
    NSLog(@"x:%d y:%d", location.x, location.y); 
} 

} 

내가있는 viewDidLoad 방법에 텍스를 선언 나에게 내가 코드를 다음 있지만 작동하지 만든다

를 ..... 도와 또한 터치 이벤트에 선언하려고하지만하십시오 작업 ...

내 응용 프로그램 로그 파일보기와 같은 ...

X : 0 Y : 터치 이동 목 1월 13일 11시 20분 5초 .local의 끌어서 놓기에서 1079934976 [536 ]

CGContextAddLineToPoint : 20 : 11 2011-01-13 잘못된 상황이 0x0 05.149 끌어서 놓기 [536 : 207] X : 0 Y : 1079902208 목 일월 13 11시 20분 5초 .local의 끌어서 놓기 [536]

CGContextSetRGBStrokeColor 잘못된 상황이 0x0 목 일월 13 11시 20분 5초 .local의 끌어서 놓기 [536]

CGContextDrawPath 잘못된 상황이 0x0 목 일월 13 11시 20분 5초 .local의 끌어서 놓기 [536]

CGContextMoveToPoint : 유효하지 않은 컨텍스트 0x0 2011-01-13 11 : 20 : 0 5.199 끌어서 놓기 [536 : 207] X : 0 Y :

CGContextAddLineToPoint : 터치 이동 목 일월 13 11시 20분 5초 .local의 끌어서 놓기 [536] 위치에 1,079,934,976 잘못된 상황이 0x0 2011-01-13 11:20 : 05.200 끌어서 놓기 [536 : 207] X : 0 Y : 1079885824

답변

13

일반적으로 터치 감지 방식에서는 직접 그리지 않습니다. 일반적으로 새로운 점/선을 저장하고 drawRect:에 모두 그릴 수 있습니다. 인스턴스 변수 pathsNSMutableArray이고 속성이 currentPath이고 UIBezierPathUIView의 사용자 정의 하위 클래스가 있다고 가정 해 보겠습니다. 다음과 같이 터치 감지 및 drawRect 메서드를 구현할 수 있습니다.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    self.currentPath = [UIBezierPath bezierPath]; 
    currentPath.lineWidth = 3.0; 
    [currentPath moveToPoint:[touch locationInView:self]]; 
    [paths addObject:self.currentPath]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    [self.currentPath addLineToPoint:[touch locationInView:self]]; 
    [self setNeedsDisplay]; 
} 

- (void)drawRect:(CGRect)rect { 
    [[UIColor redColor] set]; 
    for (UIBezierPath *path in paths) { 
    [path stroke]; 
    } 
} 

많은 부분이 단순화되어 있습니다. 많은 선을 그릴 경우 성능이 저하되고 비트 맵 이미지에 그림을 캐시하고 싶지만 시작해야합니다.

0

당신은 AddLineToPoint 호출 후 CGContextStrokePath를 호출 할 필요가있다. 획 경로 색상을 먼저 정의해야합니다.

CGContextSetRGBStrokeColor(context,0,0,1,1); 
CGContextStrokePath(context); 
관련 문제