1

我目前正在开发自己的项目,我想实现自定义加载控件。这几乎是您可以在应用程序 Amen 中找到的内容。彩色条仅在应用程序从服务器加载内容时出现。

任何提示将不胜感激。谢谢。

这里有一些图像,以使其更清晰

4

1 回答 1

1

“硬”部分将编写一个UIView处理绘制颜色的子类。您将要覆盖该drawRect:方法并弄清楚如何知道进度(或者它只是“自动增量”?)并在此基础上进行绘制/填充。然后,您可以简单地UIView在 Interface Builder 中添加一个,更改视图的 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]. 应该是这样!;-)

于 2012-05-04T16:19:57.417 回答