VLCBufferingBar.m 2.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
/*****************************************************************************
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2015 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Tobias Conradi <videolan # tobias-conradi.de>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

#import "VLCBufferingBar.h"
@interface VLCBufferingBar()
@property (nonatomic) CAShapeLayer *borderLayer;
@property (nonatomic) CAShapeLayer *fillLayer;
@property (nonatomic) CAShapeLayer *fillMaskLayer;

@end

@implementation VLCBufferingBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        _borderColor = [UIColor lightGrayColor];
        _bufferColor = [UIColor lightGrayColor];

        _borderLayer = [CAShapeLayer layer];
        _borderLayer.lineWidth = 2.0;
        _borderLayer.fillColor = [UIColor clearColor].CGColor;
        _borderLayer.strokeColor = _borderColor.CGColor;

        [self.layer addSublayer:_borderLayer];

        _fillLayer = [CAShapeLayer layer];
        _fillLayer.fillColor = _bufferColor.CGColor;

        _fillMaskLayer = [CAShapeLayer layer];
        _fillMaskLayer.fillColor = [UIColor blackColor].CGColor;
        _fillLayer.mask = _fillMaskLayer;

        [self.layer addSublayer:_fillLayer];

    }
    return self;
}

- (void)setBufferColor:(UIColor *)bufferColor {
    _bufferColor = bufferColor;
    self.fillLayer.fillColor = bufferColor.CGColor;
}
- (void)setBorderColor:(UIColor *)borderColor {
    _borderColor = borderColor;
    self.borderLayer.strokeColor = borderColor.CGColor;
}

- (void)setBufferStartFraction:(CGFloat)bufferStartFraction {
    _bufferStartFraction = bufferStartFraction;
    [self setNeedsLayout];
}
- (void)setBufferEndFraction:(CGFloat)bufferEndFraction {
    _bufferEndFraction = bufferEndFraction;
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    CGFloat inset = self.borderLayer.lineWidth/2.0;
    CGRect borderRect = CGRectInset(bounds, inset, inset);
    CGFloat cornerRadius = CGRectGetMidY(bounds); // bounds is correct (flatter)
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:borderRect cornerRadius:cornerRadius];
    self.borderLayer.path = path.CGPath;

    CGRect bufferRect = CGRectMake(CGRectGetWidth(bounds)*self.bufferStartFraction,
                                   0,
                                   CGRectGetWidth(bounds)*(self.bufferEndFraction-self.bufferStartFraction),
                                   CGRectGetHeight(bounds));
    UIBezierPath *bufferPath = [UIBezierPath bezierPathWithRect:bufferRect];
    self.fillLayer.path = bufferPath.CGPath;
    self.borderLayer.frame = bounds;
    self.fillLayer.frame = bounds;
    self.fillMaskLayer.frame = bounds;
    self.fillMaskLayer.path = path.CGPath;
}
@end