【iOS】使用UISlider实现播放器断点播放

实现播放器的断点播放有两个思路:

  • 自定义进度条(使用UIView绘制)
  • UISlider基础上实现断点播放

在这里简单介绍下用第2中思路是怎么实现的

实现思路:

扩展一个UISlider子类,在子类方法中重写开始触摸代理方法

1
-(void)touchesBegan:(NSSet <UITouch *> *)touches withEvent:(UIEvent *)event

代码实现:

1
2
3
4
5
6
7
8
9
10
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
CGRect trackRect = [self trackRectForBounds: [self bounds]];

float value = [self minimumValue] + ([[touches anyObject] locationInView: self].x - t.origin.x - 4.0) * (([self maximumValue]-[self minimumValue]) / (trackRect.size.width - 8.0));
[self setValue:value];
[super touchesBegan:touches withEvent:event];

double current = value * [AudioPlayer shareInstance].player.duration;
[[AudioPlayer shareInstance].player setCurrentTime:current];
}