美文网首页
UIProgressView与NSTimer的一些使用方法

UIProgressView与NSTimer的一些使用方法

作者: Flandreko | 来源:发表于2018-06-03 18:44 被阅读0次

查看UIProgressView的定义
知道UIProgressView有2种Style,说实话感觉没有区别

typedef NS_ENUM(NSInteger, UIProgressViewStyle) {
    UIProgressViewStyleDefault,     // normal progress bar
    UIProgressViewStyleBar __TVOS_PROHIBITED,     // for use in a toolbar
};

UIProgressView的属性

@property(nonatomic) UIProgressViewStyle progressViewStyle; // default is UIProgressViewStyleDefault
@property(nonatomic) float progress;                        // 0.0 .. 1.0, default is 0.0. values outside are pinned.
@property(nonatomic, strong, nullable) UIColor* progressTintColor;//进度条颜色
@property(nonatomic, strong, nullable) UIColor* trackTintColor;//进度条背景色
//设置进度条图片
@property(nonatomic, strong, nullable) UIImage* progressImage;
@property(nonatomic, strong, nullable) UIImage* trackImage;

progress这个值只能取0~1之间
设置进度条启动位置的方法

- (void)setProgress:(float)progress animated:(BOOL)animated
//progressView.progress=0.0;
//[progressView setProgress:0.0 animated:YES];
//上面两者作用在测试的时候一一样的...

举个例子:

-(void)setUI{
     UIProgressView *progressView=[[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
    
    progressView.progressTintColor=[UIColor redColor];
    progressView.trackTintColor=[UIColor blackColor];
    
    //两种方法效果是一样的,设置进度条启动位置
    progressView.progress=0.0;
    //[progressView setProgress:0.1 animated:YES];
    [self.view addSubview:progressView];
    [progressView makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(0);
        make.leading.equalTo(20);
        make.trailing.equalTo(-20);
        //这竟然可以设置progressView的高度
        make.height.equalTo(30);
    }];
    self.progressView = progressView;
    
    
    [NSTimer scheduledTimerWithTimeInterval:0.1
                                     target:self
                                   selector:@selector(progressChanged:)
                                   userInfo:nil
                                    repeats:YES];
}

-(void)progressChanged:(NSTimer *)timer
{
    self.progressView.progress += 0.005;
    NSLog(@"%f",self.progressView.progress);
    //progree最大值为1
    if (self.progressView.progress >= 1) {
        //移除定时器
        [timer invalidate];
    }
}

创建NSTimer的两种方法:

//第一种
timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(startLocation) userInfo:nil repeats:YES];
//将定时器加入主循环中
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

//第二种
timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(startLocation) userInfo:nil repeats:YES];

启动、停止、取消定时器

//启动
[timer setFireDate:[NSDate distantPast]];
//停止
[timer setFireDate:[NSDate distantFuture]];
//取消
[timer invalidate];

相关文章

网友评论

      本文标题:UIProgressView与NSTimer的一些使用方法

      本文链接:https://www.haomeiwen.com/subject/jbkcsftx.html