运行效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/24560b8dc070553062d02eade1712f43.png)
代码
#import "ViewController.h"
@interface ViewController ()
@property(strong,nonatomic) UIProgressView *progressView;
@property(nonatomic, strong) NSTimer *timer;
@end
@implementation ViewController
- (void)viewDidLoad {
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat proWidth = 200;
CGFloat proheight = 2;
CGFloat proTopView = 283;
self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake((screen.size.width - proWidth) / 2, proTopView, proWidth, proheight)];
[self.view addSubview: _progressView];
UIButton *downloadBtn = [UIButton buttonWithType:UIButtonTypeSystem];
[downloadBtn setTitle:@"Download" forState:UIControlStateNormal];
CGFloat downloadBtnWidth = 69;
CGFloat downloadBtnHeight = 30;
CGFloat downloadBtnTopView = 384;
downloadBtn.frame = CGRectMake((screen.size.width - downloadBtnWidth) / 2, downloadBtnTopView, downloadBtnWidth, downloadBtnHeight);
[downloadBtn addTarget:self action:@selector(downloadProgress:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:downloadBtn];
}
- (void) downloadProgress:(id)sender{
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(download) userInfo:nil repeats:TRUE];
}
-(void) download{
self.progressView.progress = self.progressView.progress + 0.1;
if(self.progressView.progress == 1.0) {
[self.timer invalidate];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"download completed!" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:okAction];
[self presentViewController:alertController animated:TRUE completion:nil];
}
}
@end