2012-05-04 44 views
1

我目前正在自己​​的項目上工作,我想實現一個自定義加載控件。 這幾乎可以在應用程序Amen中找到。只有當應用程序從服務器加載內容時,纔會顯示彩色條。ios欄,加載時改變顏色

任何提示將不勝感激。謝謝。

Here are some images to make it more clear

+0

重複您自己的已關閉的問題http://stackoverflow.com/questions/10426266/ios-colored-loading-bar – Abizern

+0

是的,我知道。我只是試圖更清楚這一次。我仍然需要我的問題 – alex

回答

1

「硬」的部分會寫一個UIView子類,將處理繪製的顏色。你會想覆蓋drawRect:方法,並找出如何知道進度(或將它只是「自動增量」?),並根據它來繪製/填充。然後,您可以簡單地在Interface Builder中添加一個UIView,更改視圖的Class類型,適當調整它的大小,然後離開!

「易」的部分是,當你想要的視圖是不可見的,你可以做許多事情之一:

  1. 通過改變其frame財產移至離屏視圖。 (這可以是「即時」或動畫)。
  2. 通過更改hidden屬性來設置視圖不可見。 (您也可以爲此設置動畫效果!)
  3. 使用[barView removeFromSuperview]完全擺脫該視圖。

更新/編輯

對於實際的圖紙,試試這個(快速,沒有做過測試,因此YMMV):

// ColorProgressBar.h 
#import <UIKit/UIKit.h> 
@interface ColorProgressBar : UIView { 
    float colorWidth; 
    float progressPercent; 
    NSMutableArray *colors; 
} 
@property (assign, nonatomic) float colorWidth; 
@property (assign, nonatomic) float progressPercent; 
@property (strong, nonatomic) NSMutableArray *colors; 
@end 

// ColorProgressBar.m 
#import "ColorProgressBar.h" 
@implementation ColorProgressBar 
@synthesize colors; 
@synthesize colorWidth; 
@synthesize progressPercent; 
- (id)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Make the color array to use (this is spelled out for clarity) 
     [self setColors:[NSMutableArray array]]; 
     [[self colors] addObject:[UIColor redColor]]; 
     [[self colors] addObject:[UIColor orangeColor]]; 
     [[self colors] addObject:[UIColor yellowColor]]; 
     [[self colors] addObject:[UIColor greenColor]]; 
     [[self colors] addObject:[UIColor blueColor]]; 
     [[self colors] addObject:[UIColor purpleColor]]; 
    } 
    return self; 
} 
- (void)drawRect:(CGRect)rect { 
    CGFloat left = 0; 
    CGRect drawBox = CGRectZero; 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextClearRect(ctx, rect); 
    int colorIndex = -1; 
    // Draw each color block from left to right, switching to the next color each time 
    do { 
     colorIndex = colorIndex + 1; 
     if (colorIndex >= [[self colors] count]) colorIndex = 0; 
     [(UIColor *)[[self colors] objectAtIndex:colorIndex] setFill]; 
     drawBox = CGRectMake(left, 0, [self colorWidth], rect.size.height); 
     CGContextFillRect(ctx, drawBox); 
    } while (left < rect.size.width); 
    // Figure out where the "faded/empty" part should start 
    left = [self progressPercent] * rect.size.width; 
    drawBox = CGRectMake(left, 0, rect.size.width - left, rect.size.height); 
    [[UIColor colorWithWhite:1.0 alpha:0.5] setFill]; 
    CGContextFillRect(ctx, drawBox); 
} 
@end 

有了這個代碼,您可以使用這個UIView子類,每次你想更新你的進度時,你只需設置你的progressPercent(這是一個浮點數,設計範圍從0.00到1.00 )並致電[myView setNeedsDisplay]。應該是這樣! ;-)

+0

的答案謝謝你的回答。我認爲這是一個很好的起點。根據您將滾動視圖拉到多少,直到它剛剛開始進入加載模式,然後完全充電爲止,該條會被填充(增量)。任何其他提示仍然歡迎 – alex

+0

如果你*真的*喜歡它,你也可以投票嗎? ;-) – mbm29414

+0

只要我有足夠的聲望點,我會投你的答案。再次感謝。 :) – alex