iOS中 UIProgressView 技术分享

 UIProgressView 继承自UIView,用来显示进度的,如音乐,视频的缓冲进度,文件的上传下载进度等.让用户知道当前操作完成了多少,离操作结束还有多远

AppDelegate.m

  ProgressViewController *progressVC = [[ProgressViewController alloc]init];
    self.window.rootViewController = progressVC;
    [progressVC release];

ProgressViewController.m

@interface ProgressViewController ()
@property (nonatomic,retain) UIProgressView *progressView;
@property (nonatomic,retain) UILabel *label;
@property (nonatomic,retain) NSTimer *timer;
@end

@implementation ProgressViewController
- (void)dealloc
{
    self.progressView = nil;
    self.label = nil;
    self.timer = nil;
    [super dealloc];
}
介绍相关   UIProgressView 的属性及用法:
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor brownColor];
    
     //UIProgressView 继承自UIView,用来显示进度的,如音乐,视频的缓冲进度,文件的上传下载进度等.让用户知道当前操作完成了多少,离操作结束还有多远
    
    //1.创建一个progress对象,有自己的初始化方法,创建的同时指定progress的样式,是个枚举,系统提供了2种样式
    self.progressView = [[UIProgressView alloc]initWithProgressViewStyle:(UIProgressViewStyleDefault)];
    
    //设置progress的frame,不用设置高度,设置的高度对进度条的高度没影响
    _progressView.frame = CGRectMake(20, 150, 280, 0);
    
    //2.配置属性
    //2.1 设置当前进度值,范围在0.0~1.0,不可以修改最大值和最小值,默认值是0.0
//    _progressView.progress = 0.5;
    
    //2.2 设置轨道(未走过进度)的颜色
    _progressView.trackTintColor = [UIColor whiteColor];
    
    //2.3 设置进度条(已走过进度)的颜色
    _progressView.progressTintColor = [UIColor redColor];
    
    //3.添加到self.view
    [self.view addSubview:self.progressView];
    
    //4.释放
    [self.progressView release];
    
    
    
    //创建一个定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(progressChanged) userInfo:nil repeats:YES];
    
    //调用label布局
    [self layoutLable];
}

布局label:

- (void)layoutLable{
    self.label = [[UILabel alloc]initWithFrame:(CGRectMake(120, 200, 80, 40))];
    _label.backgroundColor = [UIColor greenColor];
    _label.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:self.label];
}

实现定时器的方法:

- (void)progressChanged{
    if (_progressView.progress >= 1.0) {
        [self.timer invalidate];
    }
    _progressView.progress += 0.01;
    
    //找一个float类型的变量接受当前progress的数值
    float progress = self.progressView.progress * 100;
    //使用接受的数值初始化一个字符串,用于label的显示
    NSString *string = [NSString stringWithFormat:@"%.1f%%",progress];
    _label.text = string;
    self.label.textAlignment = NSTextAlignmentCenter;
}
最终效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值