tags:

views:

112

answers:

1

Hi everyone,

Please help!

I define a custom UIControl with three labels, then on the viewDidLoad function of the main view controller, I repeatedly add multiple instances of that UIControl into the scroll view.

I need one of the label within the UIControl to be updated with new value each second. I have a question, how to clear the previously drawn custom view? Or what is the method for updating that label with best drawing performance?

- (void) updateMinuteLabel{
  CustomIconControl *control = nil;
  for (control in scrollView.subviews){
    if ([view isKindOfClass:[CustomIconControl class]] && control.tag >0){
      CustomIconControl *item = (CustomIconControl *) control;
      item.intMinute += 1;
      [item setNeedsDisplay];
    }
  }
}

In the drawRect: funtion of the CustomIconControl, I use the

[minuteString drawInRect: minuteRect withFont: [UIFont systemFontOfSize:10];

With this code, it continues to draw without clearing out the previously drawn controls.

Can someone help ? Please tell me if you need more information, offcourse I have the code for custom drawing part of the CustomIconControl, the initialization of the timer, viewDidLoad etc...

+1  A: 

You say your UIControl has three labels, do you mean you are adding three UILabel's as subviews of the UIControl? If so, you can just the text' property of the label to the value and it will automatically redraw itself - no need to [minuteString drawInRet...

Otherwise, if you're drawing the three "labels" in drawRect:, calling setNeedsDisplay: after intMinute is updated should cause drawRect: to be called, thus drawing the updated text.

Also, I recommend your CustomIconControl be responsible for calling setNeedsDisplay on itself when intMinute is updated.

Andrew

Andrew
hi Andrew, I drawed three labels with drawInRect methods. My temporary solution is to redraw the whole rect of my custom view (CustomIconControl) with the color of the background color. Then redraw the string using the drawInRect, but it seems a lot of work, I just need to update the string I have just drawn with drawInRect: The setNeedsDisplay of the CustomIconControl will redraw the whole rect, in which I don't want to, because besides the minute label, other two labels and an image background do not change.
sfa
I see. In drawRect, call this first:`CGContextClearRect(UIGraphicsGetCurrentContext(), rect);`This will clear the rect so subsequent drawing won't stack up.
Andrew
oohhh thank Andrew, it works ;)
sfa