大体思路:1.主要用到了网络的下载请求,和2D绘图以及AVPlayerViewController 2.我这里的四个按钮是用故事板上拖的,然后连接一下方法。分别是
#pragma mark - 开始下载 - (IBAction)starloadAction:(id)sender { //1.url NSURL *url = [NSURL URLWithString:self.urlStr]; //2.创建会话 NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]]; //3.创建任务 task = [session downloadTaskWithURL:url]; //绘制图层 [self createDrawView]; //执行任务 [task resume]; } #pragma mark - 暂停下载 - (IBAction)pauseloadAction:(id)sender { //1.暂停 [task cancelByProducingResumeData:^(NSData * _Nullable resumeData) { //保存到全局变量中,方便继续下载 saveData = resumeData; //把以前的任务丢弃 task = nil; }]; } #pragma mark - 继续下载 - (IBAction)continueloadAction:(id)sender { task = [session downloadTaskWithResumeData:saveData]; //(恢复) [task resume]; }3.我用代理方法,监听下载的进度,和是否完成下载,使下载和绘图同步,方法是
#pragma mark - 代理方法,下载完成后 - (void) URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { //移动文件,不然程序结束,文件就会销毁,因为是在临时文件中,所以要转移 NSFileManager *manager = [NSFileManager defaultManager]; //创建新的地址 NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/download.mp4"]; NSURL *url = [NSURL fileURLWithPath:path]; //保存地址,方便播放用 self.saveUrl = url; //移动 [manager moveItemAtURL:location toURL:url error:nil]; //使画圆进度和下载进度同步 [draw setMyBlock:^float{ return 7.0; }]; //下载完成后,使用block通知画图层,绘制完成图形,我这里中间是打勾的图形 [draw setYesBlock:^BOOL{ return YES; }]; //重绘 [draw setNeedsDisplay]; //创建播放按钮 [self performSelector:@selector(createPlayButton) withObject:nil afterDelay:0.5]; } #pragma mark - 代理方法,实时掌握下载进度 - (void) URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { /* totalBytesWritten:已经下载传输的字节 totalBytesExpectedToWrite:文件的总大小 */ progress += (totalBytesWritten/(float) totalBytesExpectedToWrite)/10.0; //实时更新进度 if (progress < 6) { //避免怪圈现象 __block float this = progress; [draw setMyBlock:^float() { return this; }]; [draw setNeedsDisplay]; }4.播放视图的方法,创建播放按钮的就不放了
#pragma mark - 播放视频 - (void) buttonAction:(UIButton *) btn { //播放视图 AVPlayerViewController *avPlayer = [[AVPlayerViewController alloc] init]; //添加播放地址 avPlayer.player = [AVPlayer playerWithURL:self.saveUrl]; //手动播放 [avPlayer.player play]; //推送 [self presentViewController:avPlayer animated:YES completion:nil]; }5.然后就是绘图视图,一个画圆的方法,和三个点组成的勾勾图形
- (void) drawRect:(CGRect)rect { //获取上下文 CGContextRef context = UIGraphicsGetCurrentContext(); //画圆 CGContextAddArc(context, (414-80)/2, 150, 80, 0,self.endY, 0); //设置格式 [[UIColor whiteColor] setStroke]; CGContextSetLineWidth(context, 3); //绘制 CGContextDrawPath(context, kCGPathStroke); if (self.isy) { [self drawOk]; } } - (void) drawOk { //获取上下文 CGContextRef context = UIGraphicsGetCurrentContext(); //创建路径 CGMutablePathRef path = CGPathCreateMutable(); //创建第一个点 CGPathMoveToPoint(path, NULL, (414-80)/2-35, 140); //两个终点 CGPathAddLineToPoint(path, NULL, (414-60)/2-25, 170); CGPathAddLineToPoint(path, NULL, (414+30)/2, 130); //设置格式 [[UIColor greenColor] setStroke]; CGContextSetLineWidth(context, 5); CGContextSetLineJoin(context, kCGLineJoinRound); //添加路径 CGContextAddPath(context, path); //绘制 CGContextDrawPath(context, kCGPathStroke); }6.我用到了两个block方法,实时获取下载进度和监听下载是否完成,如果完成了则绘制打勾的图形,超简单
//获取进度的block方法 - (void) setMyBlock:(EndyBlock)endy { _block = endy; self.endY = _block(); } //判断下载是否完成 - (void) setYesBlock:(isYesBlock)isy { self.isy = isy(); }ps:做的比较糙,╮(╯﹏╰)╭,好久没敲代码,最近比较烦比较烦………懵逼的状态…..