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

实现播放器的断点播放有两个思路:
1.自定义进度条(使用UIView绘制)
2.在UISlider基础上实现断点播放

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

实现思路:

扩展一个UISlider子类,在子类方法中重写开始触摸代理方法
-(void)touchesBegan:(NSSet <UITouch *> *)touches withEvent:(UIEvent *)event

代码具体实现:

-(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];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容