美文网首页
拍摄分段动画

拍摄分段动画

作者: lifeLL | 来源:发表于2018-03-12 16:40 被阅读0次

    项目短视频拍摄需要像微信一样的圆环动画,且删除段落部分颜色改变,第二次点击则删除该段落;
    直接上方案:
    1.利用贝塞尔曲线和CAShapeLayer完成;
    2.在长按拍摄按钮时候,圆环进度不断前进,这个在代理回调过程中不断添加新值的图层;
    3.每拍完一段就一个不用颜色的间隔标识,这个也是画图层上去;
    4.删除每一段视频时候,分两段,首先先逆时针画个不同颜色的新图层标识这一段将要删除,然后真正删除时候就把这一段删除,但是在删除过程中其实是移除对应图层,但是在拍摄过程中图层是加了N多,所以这里在没拍完一段回调的时候记录下这个时候的图层总是放在数组里,作为待删除到某个图层的标识,这个比较key,基本上这样解决了就ok了,也走了些弯路,想把要真正删除的部分也用白色透明或者clear绘制出来,其实不行,就这样了,代码如下:

    添加图层函数

    -(void)addRingPathWithStartAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle color:(UIColor*)color clockwise:(BOOL)clockwise{
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.recordButton.frame.size.width/2, self.recordButton.frame.size.height/2) radius:37 startAngle:startAngle endAngle:endAngle clockwise:clockwise];
        CAShapeLayer *ringlayer = [CAShapeLayer layer];
        ringlayer.path = path.CGPath;
        ringlayer.lineWidth = 7.0;
        ringlayer.fillColor = [UIColor clearColor].CGColor;
        ringlayer.strokeColor = color.CGColor;
        self.ringlayer = ringlayer;
        //圆的起始位置,默认为0
    //    ringlayer.strokeStart = 0;
    //    //圆的结束位置,默认为1,如果值为0.75,则显示3/4的圆
    //    ringlayer.strokeEnd = 1;
        [self.recordButton.layer addSublayer:ringlayer];
    
    }
    
    @property(nonatomic,strong) NSMutableArray* pathArray;//拍摄生成图层数组
    @property(nonatomic,strong) NSMutableArray* delePathArray;//分段数组
    @property(nonatomic,assign) CGFloat totalDuration;//拍摄时长
    @property(nonatomic,strong) NSMutableArray* layerArray;//图层数
    #define DEGREES_DefaultStart(degrees)  ((M_PI * (degrees+270))/ 180)//默认270度为开始的位置
    
    // 正在录制的过程中
    - (void)shortVideoRecorder:(PLShortVideoRecorder *)recorder didRecordingToOutputFileAtURL:(NSURL *)fileURL fileDuration:(CGFloat)fileDuration totalDuration:(CGFloat)totalDuration {
    //    [_progressBar setLastProgressToWidth:fileDuration / self.shortVideoRecorder.maxDuration * _progressBar.frame.size.width];
       if (_delePathArray.count > 0) {
           [self addRingPathWithStartAngle: DEGREES_DefaultStart(_totalDuration / self.shortVideoRecorder.maxDuration * 360) endAngle:DEGREES_DefaultStart((fileDuration + _totalDuration) / self.shortVideoRecorder.maxDuration * 360) color:[UIColor orangeColor] clockwise:YES];
            [_pathArray addObject:[NSNumber numberWithFloat:fileDuration / self.shortVideoRecorder.maxDuration * 360 ]];
       }else{
           [self addRingPathWithStartAngle: DEGREES_DefaultStart(0) endAngle:DEGREES_DefaultStart(fileDuration / self.shortVideoRecorder.maxDuration * 360) color:[UIColor orangeColor] clockwise:YES];
           [_pathArray addObject:[NSNumber numberWithFloat:fileDuration / self.shortVideoRecorder.maxDuration * 360]];
       }
    
    
    
       self.endButton.enabled = (totalDuration >= self.shortVideoRecorder.minDuration);
       self.squareRecordButton.hidden = YES; // 录制过程中不允许切换分辨率(1:1 <--> 全屏)
       self.deleteButton.hidden = YES;
       self.endButton.hidden = YES;
       self.importMovieView.hidden = YES;
       self.musicButton.hidden = YES;
       self.filePathButton.hidden = YES;
    
       self.durationLabel.text = [NSString stringWithFormat:@"%.2fs", totalDuration];
    
    }
    
    // 完成一段视频的录制时
    - (void)shortVideoRecorder:(PLShortVideoRecorder *)recorder didFinishRecordingToOutputFileAtURL:(NSURL *)fileURL fileDuration:(CGFloat)fileDuration totalDuration:(CGFloat)totalDuration {
    
        [_progressBar stopShining];
        self.deleteButton.hidden = NO;
        self.endButton.hidden = NO;
    
        AVAsset *asset = [AVAsset assetWithURL:_URL];
        CGFloat duration = CMTimeGetSeconds(asset.duration);
        self.draftButton.hidden = (totalDuration +  duration) >= self.shortVideoRecorder.maxDuration;
        
        if (totalDuration >= self.shortVideoRecorder.maxDuration) {
    //        [self endButtonEvent:nil];
        }
        [self addRingPathWithStartAngle: DEGREES_DefaultStart(((totalDuration) / self.shortVideoRecorder.maxDuration * 360)) endAngle:DEGREES_DefaultStart((totalDuration) / self.shortVideoRecorder.maxDuration * 360 - 1) color:[UIColor greenColor] clockwise:NO];
    
         _totalDuration = totalDuration;
         [_delePathArray addObject:[NSNumber numberWithFloat:fileDuration / self.shortVideoRecorder.maxDuration * 360]];
        //拍摄一段视频完成有多少图层
         [_layerArray addObject:[NSNumber numberWithInt:_recordButton.layer.sublayers.count]];
    }
    
    // 删除了某一段视频
    - (void)shortVideoRecorder:(PLShortVideoRecorder *)recorder didDeleteFileAtURL:(NSURL *)fileURL fileDuration:(CGFloat)fileDuration totalDuration:(CGFloat)totalDuration {
        self.endButton.enabled = totalDuration >= self.shortVideoRecorder.minDuration;
    //    self.deleteButton.enabled = (totalDuration >= self.shortVideoRecorder.minDuration);
        if (totalDuration <= 0.0000001f) {
            self.squareRecordButton.hidden = NO;
            self.deleteButton.hidden = YES;
            self.endButton.hidden = YES;
            self.importMovieView.hidden = NO;
            self.musicButton.hidden = NO;
            self.filePathButton.hidden = NO;
            _desLabel.text = @"轻触拍照,长按摄像";
    
        }
        
        AVAsset *asset = [AVAsset assetWithURL:_URL];
        CGFloat duration = CMTimeGetSeconds(asset.duration);
        self.draftButton.hidden = (totalDuration +  duration) >= self.shortVideoRecorder.maxDuration;
    
        self.durationLabel.text = [NSString stringWithFormat:@"%.2fs", totalDuration];
    
    
        _totalDuration = totalDuration;
        [_delePathArray removeLastObject];
    }
    
    // 删除上一段视频
    - (void)deleteButtonEvent:(id)sender {
    
        if (_deleteButton.style == PLSDeleteButtonStyleNormal) {
    
             [self addRingPathWithStartAngle: DEGREES_DefaultStart(_totalDuration / self.shortVideoRecorder.maxDuration * 360) endAngle:DEGREES_DefaultStart(_totalDuration / self.shortVideoRecorder.maxDuration * 360 - [_delePathArray.lastObject floatValue]) color:[JJTool colorWithHexValue:0xfd6954] clockwise:NO];
    //        [_progressBar setLastProgressToStyle:PLSProgressBarProgressStyleDelete];
            _deleteButton.style = PLSDeleteButtonStyleDelete;
    
        } else if (_deleteButton.style == PLSDeleteButtonStyleDelete) {
            if (_layerArray.count>=1) {
                [_layerArray removeLastObject];
            }else{
                return;
            }
            [self.shortVideoRecorder deleteLastFile];
            if (_recordButton.layer.sublayers.count>1) {
                for (int i = _recordButton.layer.sublayers.count ; i > (_layerArray.count == 0 ? 1: [_layerArray.lastObject integerValue]); i--) {
                    [_recordButton.layer.sublayers.lastObject removeFromSuperlayer];
                }
            }
    //        [_progressBar deleteLastProgress];
            _deleteButton.style = PLSDeleteButtonStyleNormal;
    
        }
    }
    
    WeChat_1520843074.jpeg
    WeChat_1520843073.jpeg
    WeChat_1520843072.jpeg

    相关文章

      网友评论

          本文标题:拍摄分段动画

          本文链接:https://www.haomeiwen.com/subject/wjanfftx.html