gpt4 book ai didi

加载时改变颜色的ios栏

转载 作者:行者123 更新时间:2023-11-29 13:36:32 25 4
gpt4 key购买 nike

我目前正在做我自己的一个项目,我想实现一个自定义加载控件。这几乎是您可以在应用程序 Amen 中找到的内容。只有当应用程序从服务器加载内容时才会显示彩色条。

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

Here are some images to make it more clear

最佳答案

“困难”部分是编写一个 UIView 子类来处理绘制颜色。您需要覆盖 drawRect: 方法并弄清楚如何了解进度(或者它只是“自动递增”?)并基于此绘制/填充。然后,您可以简单地在 Interface Builder 中添加一个 UIView,更改 View 的类类型,适当调整大小,然后开始!

“简单”的部分是,当您希望 View 不可见时,您可以执行以下操作之一:

  1. 通过更改其 frame 属性将 View 移出屏幕。 (这可以是“即时的”或动画的。)
  2. 通过更改其hidden 属性将 View 设置为不可见。 (你也可以制作动画!)
  3. 使用 [barView removeFromSuperview] 完全摆脱 View 。

更新/编辑

对于实际绘图,试试这个(快速完成且未测试,所以 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 的 float )并调用 [myView setNeedsDisplay ]。应该是这样! ;-)

关于加载时改变颜色的ios栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10452207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com