您的位置:首页 > 产品设计 > UI/UE

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

2015-12-09 18:48 585 查看

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

今天做了一个关于用UIProgressView 做音乐播放进度条的问题,本想用循环直接给progress属性赋值,结果各种显示不出来,于是有了以下两种猜测:

1.循环太快,进度条根本来不及更新显示;

2.progress的更新要等到循环结束才执行。

最初的代码为:

-(void)musicAction
{

[self.musicPlayerToo
play];

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

while ([self.musicPlayerToo
isPlaying]) {

CGFloat len =
_musicPlayerToo.currentTime /
_musicPlayerToo.duration;
[self.progressView
setProgress:len animated:YES];
}
}

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

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

-(void)musicAction

{

[self.musicPlayerToo
play];

//实例化一个计时器

self.timer = [NSTimer
scheduledTimerWithTimeInterval:0.1f
target:self
selector:@selector(updateProgress)
userInfo:nil
repeats:YES];

//设置计时器的优先级

NSRunLoop *runLoop = [NSRunLoop
currentRunLoop];

[runLoop addTimer:self.timer
forMode:NSRunLoopCommonModes];

}

-(void)updateProgress{

CGFloat len =
_musicPlayerToo.currentTime /
_musicPlayerToo.duration;

[self.progressView
setProgress:len animated:YES];

if (![_musicPlayerToo
isPlaying]) {

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

[self.timer
invalidate];

self.timer =
nil;

}

}

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: