ios ProgressView进度条示例 demo

运行效果

ios ProgressView进度条示例 demo_第1张图片

代码
#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

你可能感兴趣的:(IOS)