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;
}
}
各位程序猿兄弟姐妹们,小弟刚开始写个人博客,写作水平有限望见谅啊,但是写的都是自己所思所想的东西,望多多转发,求关注啊,有兴趣的加好友一起研究啊。
相关文章推荐
- 本地推送UILocalNotification(整理)
- UI/UIWindow/UIView
- 通过UIEdgeInsetsMake来制作可伸缩的Button(图片拉伸)
- zzulioj 1801: xue姐的小动物 (排序)水
- UEFI启动模式安装ubuntu指南
- iOS点击UITableView的cell,弹出UIAlertController有时会出现延迟
- iOS 设置UITextField的placeholder属性的颜色
- Building librtmp for iOS
- jsp Request获取url信息的各种方法比较
- POJ-2299 Ultra_QuickSort 线段树+逆序对数
- POJ-2299 Ultra_QuickSort 线段树+逆序对数
- 如何给UIViewController瘦身
- UICollectionView可移动item
- USACO [2.1] Sorting a Three-Valued Sequence
- 大猫品Android[一][打入MessageQueue内部]
- ui-router中resolve使用说明
- weiFenLuo.winFormsUI.Docking.dll学习
- iOS 获取 UITabViewController 和 UINavigationController 的图标位置
- 提升UITableView性能-复杂页面的优化
- zzulioj 1818: squee_spoon and his Cube VI 【dp】