UIProgressView与NSTimer的一些使用方法

查看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];
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • The Antarctic is home to just 2 of the world's 17 types o...
    XY圆圆阅读 701评论 0 0
  • 我只身赤足淌过河水 衣衫褴褛翻过丘岭 一路听风嗅雨,牵云扯雾 ...
    冷月关山阅读 456评论 8 15
  • 和老帅聊天。老帅感慨:三十年前,城里人不用辛劳,穿华衣吃饱饭,那么滋润与清闲,好生羡慕!于是,读书学习再吃苦,终是...
    那时花开1阅读 265评论 0 3
  • 一个风和日丽的星期六,狐狸请老狼吃饭。狐狸做的菜是一大锅美味的烧鸡。老狼知道了,馋的口水都流了下来,他想独霸这盆烧...
    甜甜起司猫阅读 946评论 3 1
  • 我发给儿子的简书,儿子还非要当我面看,真是开心极了。我一天天的记录,只是为了看见周遭生活的美好,家人的美好...
    夜雨轩1991阅读 176评论 0 3

友情链接更多精彩内容