2012-09-10 2 views
0

여기 내 문제가 있습니다. 나는 drawRect에서 성공적으로 완료된 설치시 뷰 안쪽에 999 개의 원을 그릴 계획입니다. 그런 다음 사용자가 터치 할 때 각 원의 색상을 편집하려고합니다 (보기 컨트롤러에서 탭 제스처를 사용하여 처리 중입니다). 문제는 내가 [self.pegView setNeedsDisplay]를 호출 할 때이다. drawRect가 호출되어 drawRect에서 if의 else 부분으로 이동하지만 다른 모든 비드는 제거되고 999 구슬 대신 1 개의 비드 만 보입니다.drawRect에서 특정 rect를 다시 그리는 방법

아래 코드는 제 코드입니다.

- (void)drawRect:(CGRect)rect 
{ 

    if(!self.isEditingHappening) // For the initial setup 
    { 
     CGRect beadRect = CGRectMake(BEAD_ORIGIN_X, BEAD_ORIGIN_Y, BEAD_VIEW_SIZE, BEAD_VIEW_SIZE); 
     self.ctx = UIGraphicsGetCurrentContext(); 

     for(int i = 0 ; i < 999 ; i++) 
     { 
      // To set up the bead view for each column 
      if (i !=0 && i % 37 !=0) 
      { 
       beadRect.origin = CGPointMake((beadRect.origin.x + BEAD_VIEW_SIZE), beadRect.origin.y); 
      } 

      // To set up bead view for each row 
      if (i !=0 && i %37 ==0) 
      { 
       beadRect.origin.y += BEAD_VIEW_SIZE; 
       beadRect.origin = CGPointMake(BEAD_ORIGIN_X, beadRect.origin.y); 
      } 

      BeadPattern *pattern = [self.beadPatternsArray objectAtIndex:(i/37)]; 

      int beadType=[[pattern.eachRowPattern objectAtIndex:i%37] intValue]; 
      BeadColor *color=[self.beadColorsArray objectAtIndex:beadType]; 


      CGPoint center; 
      center.x = beadRect.origin.x + beadRect.size.width/2.0; 
      center.y = beadRect.origin.y + beadRect.size.height/2.0; 

      CGRect newInnerRect; 
      newInnerRect.size.width = beadRect.size.width * 0.6; 
      newInnerRect.size.height = beadRect.size.height * 0.6; 
      newInnerRect.origin.x = center.x - newInnerRect.size.width/2; 
      newInnerRect.origin.y = center.y - newInnerRect.size.height/2; 



      [[UIColor whiteColor] set]; 
      UIRectFill(beadRect); 

      // Adds the outer circle 
      CGContextAddEllipseInRect(self.ctx, beadRect); 

      // Fill the outer circle with any color 
      CGContextSetRGBFillColor(self.ctx, color.redValue, color.greenValue, color.blueValue, 1); 

      CGContextFillEllipseInRect(self.ctx, beadRect); 

      //Add inner circle 
      CGContextAddEllipseInRect(self.ctx, newInnerRect); 


      //Fill inner circle with white color 
      CGContextSetRGBFillColor(self.ctx, 1, 1, 1, 0.4); 
      CGContextFillEllipseInRect (self.ctx, newInnerRect); 

     } 
    } 
    else // When editing is happening 
    { 
     NSLog(@"The redrawing rect is %@", NSStringFromCGRect(rect)); 
     CGContextSaveGState(self.ctx); 
     CGRect beadRect; 
     beadRect.size = CGSizeMake(BEAD_VIEW_SIZE, BEAD_VIEW_SIZE); 
     beadRect.origin = CGPointMake(self.columnToBeUpdated * BEAD_VIEW_SIZE, self.rowToBeUpdated * BEAD_VIEW_SIZE); 

     CGPoint center; 
     center.x = beadRect.origin.x + beadRect.size.width/2.0; 
     center.y = beadRect.origin.y + beadRect.size.height/2.0; 

     CGRect newInnerRect; 
     newInnerRect.size.width = beadRect.size.width * 0.6; 
     newInnerRect.size.height = beadRect.size.height * 0.6; 
     newInnerRect.origin.x = center.x - newInnerRect.size.width/2; 
     newInnerRect.origin.y = center.y - newInnerRect.size.height/2; 

     CGContextRestoreGState(self.ctx); 

     [[UIColor whiteColor] set]; 
     UIRectFill(beadRect); 

     // Adds the outer circle 
     CGContextAddEllipseInRect(self.ctx, beadRect); 

     // Fill the outer circle with any color 
     CGContextSetRGBFillColor(self.ctx, self.colorToBeShownWhileEdited.redValue, self.colorToBeShownWhileEdited.greenValue, self.colorToBeShownWhileEdited.blueValue, 1); 

     CGContextFillEllipseInRect(self.ctx, beadRect); 

     //Add inner circle 
     CGContextAddEllipseInRect(self.ctx, newInnerRect); 

     //Fill inner circle with white color 
     CGContextSetRGBFillColor(self.ctx, 1, 1, 1, 0.4); 
     CGContextFillEllipseInRect (self.ctx, newInnerRect); 
    } 
    } 

내가하려는 의도는 999 구슬 모두를 유지하면서 사용자가 터치 한 비드의 색만 편집하는 것입니다. [setNeedsDisplayInRect]도 사용하려고 시도하지만 rect가 올바르게 전달되지 않습니다. 제발 도와주세요.

답변

0

정확하게 기억하면 캔버스에있는 다른 항목이 지워 지므로 매번 초기화 설정을해야합니다.

또는 각 비드에 대해 개별적으로 볼 수있는 별도의 뷰를 만들 수 있습니다. 각 구슬은 자체 논리를 포함 할 수 있습니다.

+0

각 구슬에 대해 단일보기를 사용했지만 너무 많은 오버 헤드가 있었으며 스크롤보기 내부에서보기를 확대 할 때 너무 많이 뒤집 혔습니다 (모든 확대/축소에 대해 999 개의보기를 호출해야하므로). 그래서 나는 999 개의 모든 구슬을 볼 수있는 길을 걷게되었습니다. – jeevangs

관련 문제