2014-09-26 3 views
1

내가 일부 코드 아래와 같이 가지고UIVew에 선을 그릴 때 왜 backgoundColor가 필요합니까?

- (void)drawRect:(CGRect)rect 
{ 
    [super drawRect:rect]; 

    [self drawWithBezierPath]; 
    //[self drawOnCurrentGraphicsContext]; 
} 

- (void)drawWithBezierPath 
{ 
    if (self.selectedButtons.count > 0) { 
     UIBezierPath *bezierPath = [UIBezierPath bezierPath]; 

     for (int i = 0; i < self.selectedButtons.count; i++) { 
      if (i == 0) { 
       UIButton *firstButton = [self.selectedButtons objectAtIndex:0]; 
       [bezierPath moveToPoint:firstButton.center]; 
      } else { 
       UIButton *button = [self.selectedButtons objectAtIndex:i]; 
       [bezierPath addLineToPoint:button.center]; 
       [bezierPath moveToPoint:button.center]; 
      } 
     } 

     [bezierPath addLineToPoint:self.currentPoint]; 

     [bezierPath setLineWidth:5.0f]; 
     [bezierPath setLineJoinStyle:kCGLineJoinRound]; 
     [[UIColor yellowColor] setStroke]; 

     [bezierPath stroke]; 
    } 
} 

는 I 선을 그리고 자 할 때의 손가락 움직임 :

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

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

- (void)onTouch:(UITouch *)touch 
{ 
    if (touch) { 
     CGPoint point = [touch locationInView:self]; 
     self.currentPoint = point; 

     UIButton *button = [self buttonContainsPoint:point]; 
     if (button && ![self.selectedButtons containsObject:button]) { 
      [self.selectedButtons addObject:button]; 
      button.selected = YES; 
     } 

     [self setNeedsDisplay]; 
    } 
} 

enter image description here 도시 상기 화상으로서

, I는 손가락을 이동할 때 그것은 내가 기대하지 않는 많은 라인을 그립니다.

나는 한번도 그려진 라인을 취소 할 수 CGContextClearRect과 같은 작업을 수행 할 수 있습니다 알고 있지만, 내가 찾은 중요한 건 내가 self.backgroundColor = [UIColor clearColor];를 작성하는 경우 CGContextClearRect하지 않고, 그 이전에 그려진 선이 자동으로 지워집니다 점이다.

enter image description here

그래서 명시 적으로 배경을 설정하지 않는 경우는 backgroundColor로는 nil 될 것이며, 아이폰 OS는 라인 이전에 그려 취소하지 않을 것이다, 또는 그것을하지만 나도 몰라 않습니다.

아무도 말해 줄 수 없습니까? 감사합니다 :-)

답변

1

내 생각에 배경색을 변경하면 도면이 지워질 것입니다. 따라서 원하는 색상을 사용할 수 있습니다. 배경색을 빨간색으로 설정하면 모든 것이 빨간색으로 표시되고 줄도 지워집니다.

클로스

+0

감사 @Claus, 당신의 추측은 너무 내 생각, 그래서 당신의 대답을 받아 들일 수 있지만, 난 그냥 당신에게 당신의 답변에 대한 업 투표를했다. –

관련 문제