iOS UIProgressView 的progress属性 在循环中赋值无效 解决方案

来源:互联网 发布:淘宝skype充值 编辑:程序博客网 时间:2024/06/06 01:48

iOS UIProgressView 的progress属性 在循环中赋值无效 解决方案

今天做了一个关于用UIProgressView 做音乐播放进度条的问题,本想用循环直接给progress属性赋值,结果各种显示不出来,于是有了以下两种猜测:
1.循环太快,进度条根本来不及更新显示;
2.progress的更新要等到循环结束才执行。

最初的代码为:

-(void)musicAction

{


    [self.musicPlayerTooplay];

    

    //最初想法通过循环该表progress属性,实践证明想多了

    while ([self.musicPlayerTooisPlaying]) {

        CGFloat len =_musicPlayerToo.currentTime /_musicPlayerToo.duration;

        [self.progressViewsetProgress:len animated:YES];

    }

}

后来经过不断的调试明白,真的是第二种猜测是正确的,然而不能因为循环解决不了这个问题就选择放弃,后来我灵机一动,咱不是有个计时器可以达到循环类似的效果么,所以开始动手,果断想到就干啊!

机智的我稍稍一改动,马上就可以用了,完美解决该类问题!

-(void)musicAction

{


    [self.musicPlayerTooplay];

    //实例化一个计时器

    self.timer = [NSTimerscheduledTimerWithTimeInterval:0.1ftarget:selfselector:@selector(updateProgress)userInfo:nilrepeats:YES];

    //设置计时器的优先级

    NSRunLoop *runLoop = [NSRunLoopcurrentRunLoop];

    [runLoop addTimer:self.timerforMode:NSRunLoopCommonModes];

}

-(void)updateProgress{

    CGFloat len =_musicPlayerToo.currentTime /_musicPlayerToo.duration;

    [self.progressViewsetProgress:len animated:YES];

    if (![_musicPlayerTooisPlaying]) {

//当音乐播放完毕时移除计时器

        [self.timerinvalidate];

        self.timer =nil;

    }

}


各位程序猿兄弟姐妹们,小弟刚开始写个人博客,写作水平有限望见谅啊,但是写的都是自己所思所想的东西,望多多转发,求关注啊,有兴趣的加好友一起研究啊。


2 0