NSProgress进度条重画问题

iOS 码拜 9年前 (2015-05-09) 678次浏览 0个评论
 

NSProgress进度条只有两种风格:    UIProgressViewStyleDefault,     // normal progress bar
                                  UIProgressViewStyleBar,         // for use in a toolbar
我现在想使用UIProgressViewStyleDefault风格,但是它的进度条的颜色是蓝色的,我想把它改成绿色的!因为progress没有color的属性,我就想到重写- (void)drawRect:(CGRect)rect;函数。但是不知道怎么将进度条颜色更改。
各位大虾给点主意吧!

2分
用这个initWithFrame初始化试试
18分
- (void)awakeFromNib
{
 [self setBackgroundColor:[UIColor clearColor]];
 }
 // allow non-clear background color in Interface Builder, but do not draw it
 - (BOOL)getClearsContextBeforeDrawing
 {
  return NO;
 };
 // draw left and right edge of progress bar with filled sections between
 - (void)drawRect:(CGRect)rect{
  const int numParts = 10;
  UIImage *pb_left = [UIImage imageNamed:@"pb_left.png"];
  // "[" UIImage *pb_right = [UIImage imageNamed:@"pb_right.png"];
  // "]" UIImage *pb_on = [UIImage imageNamed:@"pb_on.png"];
  // "X" UIImage *pb_off = [UIImage imageNamed:@"pb_off.png"];
  // " " CGPoint p = {0,0}; 
  [pb_left drawAtPoint:p]; 
  p.x = pb_left.size.width; 
  int q = (int)(self.progress * numParts); 
  for (int i=0; i<numParts; i++) 
  {
    if (i == q)  // partial on/off section. works with semi-transparent images too  
    {
       float w = truncf(pb_on.size.width * fmodf(self.progress * numParts, 1.0f));   
       [pb_on drawInRect:CGRectMake(p.x, p.y, w, pb_on.size.height)];   
       [pb_off drawInRect:CGRectMake(p.x + w, p.y, pb_on.size.width - w, pb_on.size.height)];
    }
    else if (i < q)
    {
       [pb_on drawAtPoint:p];
    }
    else // (i > q)
    {
       [pb_off drawAtPoint:p];
    }
    p.x += pb_on.size.width;
  }
  [pb_right drawAtPoint:p];
}

http://darkfader.blogspot.com/2009/06/uiprogressview-custom-draw-method.html

非常感谢!

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明NSProgress进度条重画问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!