2012-04-19 2 views
0

enter image description here 다음 코드를 사용하여 아이폰에 페인트 브러시를 만듭니다.아이폰에 페인트 브러시를 그리는 동안 라인에서 점들을 제거하는 방법?

@interface Canvas : UIImageView { 
    CGPoint location; 
} 

@property CGPoint location; 
.m file 
@synthesize location; 

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

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

    UIGraphicsBeginImageContext(self.frame.size); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    //CGContextSetBlendMode(ctx, kCGBlendModeOverlay); 


    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
    CGContextSetLineCap(ctx, kCGLineCapRound); 
    CGContextSetBlendMode(ctx, kCGBlendModeNormal); 
    CGContextSetLineWidth(ctx, 5.0); 
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0); 
    //CGContextSetBlendMode(ctx, kCGBlendModeOverlay); 
    CGContextBeginPath(ctx); 
    CGContextMoveToPoint(ctx, location.x, location.y); 
    CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y); 
    CGContextStrokePath(ctx); 
    self.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    location = currentLocation; 
} 

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

    UIGraphicsBeginImageContext(self.frame.size); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    // CGContextSetBlendMode(ctx, kCGBlendModeOverlay); 

    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
    CGContextSetBlendMode(ctx, kCGBlendModeNormal); 
    CGContextSetLineCap(ctx, kCGLineCapRound); 
    CGContextSetLineWidth(ctx, 5.0); 
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0); 
    CGContextBeginPath(ctx); 
    CGContextMoveToPoint(ctx, location.x, location.y); 
    CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y); 
    CGContextStrokePath(ctx); 
    self.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    location = currentLocation;  
} 

그것은 작동하지만 drawn.i 그 점을 제거하려면 부드러운 직선 line.How 내가 이것을 달성 할 수 원하는되고있는 줄에 일정한 거리 후 일부 도트가 그리는 동안이야?

답변

0

동일한 이미지를 반복적으로 사용하면서 이전 이미지에 여러 번 사용하고 있습니까? 나는 매 터치마다 신선한 컨텍스트를 사용하려고합니다. 그런 다음 기존 이미지에이를 곱합니다. 아마 이상한 표정에 대한 이유는 무엇입니까?

시작과 끝 점이 다른 경우에도 검사를 추가 할 수 있습니다.

그리기위한 추가 방법을 만들어야합니다. TouchesMoved 및 TouchesEnded에 중복 코드가 있습니다.

+0

정상으로 모드를 변경하더라도 동일한 이미지가 반복적으로 사용되는 것은 아닙니다. 또한 처음으로 – Bhoomi

+0

의 결과 스크린 샷을 보여줍니다. 아마도 도움이됩니다. – calimarkus

+0

그래서 투명 색상으로 그릴 경우에만 문제가 나타 납니까? 단색의 파란색 선이 괜찮은 것 같습니까? 당신은 터치의 모든 위치를 저장하고 모든 변경 사항에 대해 현재의 전체 라인을 새로 그릴 수 있습니다. – calimarkus