黑马程序员技术交流社区
标题:
音乐播放器类进度条(优化版)
[打印本页]
作者:
叶子哥
时间:
2016-7-11 11:09
标题:
音乐播放器类进度条(优化版)
话不多说,先看GIF预览:
musicProgress.gif
(2.73 MB, 下载次数: 1)
下载附件
2016-7-11 11:06 上传
这个Demo中实现主要用到那些方面呢? 下面为你一一道来:
1.Quartz-2D
这里主要利用Quartz-2D的重绘功能,进度条的实时刷新.
- (void)drawRect:(CGRect)rect {
CGPoint center = CGPointMake(rect.size.width*0.5, rect.size.height*0.5);
CGFloat radius = 100.0f;
CGFloat startAngle = - M_PI_2;
CGFloat endAngle = 2 * M_PI * self.valueChange - M_PI_2;
//大圆
UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:2 * M_PI clockwise:YES];
[[UIColor darkGrayColor] set];
[path1 fill];
//内圆
UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:center radius:radius - 5 startAngle:0 endAngle:2 * M_PI clockwise:YES];
[[UIColor whiteColor] set];
[path2 fill];
//显示的进度 圆形
UIBezierPath *path3 = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
[path3 addLineToPoint:center];
[[UIColor redColor] set];
[path3 fill];
//剪切图片,并加载
[path2 addClip];
[[UIImage imageNamed:@"1"] drawInRect:rect];
//图片转动
[self rotateImage];
}
复制代码
2.自定义UISlider
重写方法和自定义代理方法,实现监听滑块拖动时,用户停止拖拽的事件,并进行判断,调用代理方法.
@protocol YYSliderDelegate <NSObject>
@optional
- (void)sliderWhenEndTracking:(YYSlider *)slider;
@end
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
if ([self.delegate respondsToSelector:@selector(sliderWhenEndTracking:)]) {
[self.delegate sliderWhenEndTracking:self];
}
}
复制代码
3.设置定时器
这里的定时器主要用来调用改变进度的方法,如果用在真实的播放音乐上的话,可以根据不同的歌曲时间长度来相应设置.
//开启定时器方法
- (void)startTimer{
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.04 target:self selector:@selector(progressViewValueChange) userInfo:nil repeats:YES];
self.timer = timer;
[[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
}
复制代码
//结束定时器方法
- (void)endTimer{
[self.timer invalidate];
self.timer = nil;
}
复制代码
4.设置按钮的不同状态
根据不同的点击时间,按钮的状态会发生相应的变化.这里使用一个属性来记录点击时间,并封装设置按钮颜色和文字的方法.
- (void)setClickBtnWithTitle:(NSString *)title withColor:(UIColor *)color{
[self.beginBtn setBackgroundColor:color];
[self.beginBtn setTitle:title forState:UIControlStateNormal];
}
复制代码
结束语:
本Demo的实现思路很简单,可以
活学活用
.需要源代码的
请戳这里
:
https://github.com/yaomars/MusicProgress
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2