NSTimer+UIProgressView

#import "ViewController.h"

@interface ViewController (){
    NSTimer *_timer;
    UIProgressView *_progressView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // 初始化 Pramas:触发间隔时间 触发对象 触发方法 传递参数 是否重复
    _timer  = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(proAction) userInfo:nil repeats:YES];
    // 开始执行 若重复则重复 否则只开始一次
    [_timer fire];
    // 加入RunLoop重复 否则只走一次
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
    // 初始化进度条
    _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
    _progressView.frame = CGRectMake(30, 30, 300, 50);
    _progressView.progressTintColor = [UIColor redColor];
    [self.view addSubview:_progressView];
}

- (void)proAction{
    NSLog(@"走了一次");
    _progressView.progress = _progressView.progress + 0.1;
    // 进度条走完 销毁定时器
    if (_progressView.progress == 1.0) {
        [_timer invalidate];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"downLoad complete" message:@"ss" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertView show];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
NSTimer+UIProgressView_第1张图片
33.gif

你可能感兴趣的:(NSTimer+UIProgressView)