开发者

How to update a label of a custom view within a scroll view using a timer

Please help!

I define a custom UIControl with three labels, then on the viewDidLoad function of the main view controller, I repeated开发者_Python百科ly 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: function 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, of course I have the code for custom drawing part of the CustomIconControl, the initialization of the timer, viewDidLoad etc.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜