VLCBufferingBar.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*****************************************************************************
  2. * VLC for iOS
  3. *****************************************************************************
  4. * Copyright (c) 2015 VideoLAN. All rights reserved.
  5. * $Id$
  6. *
  7. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. #import "VLCBufferingBar.h"
  12. @interface VLCBufferingBar()
  13. @property (nonatomic) CAShapeLayer *borderLayer;
  14. @property (nonatomic) CAShapeLayer *fillLayer;
  15. @property (nonatomic) CAShapeLayer *fillMaskLayer;
  16. @end
  17. @implementation VLCBufferingBar
  18. - (instancetype)initWithFrame:(CGRect)frame
  19. {
  20. self = [super initWithFrame:frame];
  21. if (self) {
  22. _borderColor = [UIColor lightGrayColor];
  23. _bufferColor = [UIColor lightGrayColor];
  24. _borderLayer = [CAShapeLayer layer];
  25. _borderLayer.lineWidth = 2.0;
  26. _borderLayer.fillColor = [UIColor clearColor].CGColor;
  27. _borderLayer.strokeColor = _borderColor.CGColor;
  28. [self.layer addSublayer:_borderLayer];
  29. _fillLayer = [CAShapeLayer layer];
  30. _fillLayer.fillColor = _bufferColor.CGColor;
  31. _fillMaskLayer = [CAShapeLayer layer];
  32. _fillMaskLayer.fillColor = [UIColor blackColor].CGColor;
  33. _fillLayer.mask = _fillMaskLayer;
  34. [self.layer addSublayer:_fillLayer];
  35. }
  36. return self;
  37. }
  38. - (void)setBufferColor:(UIColor *)bufferColor {
  39. _bufferColor = bufferColor;
  40. self.fillLayer.fillColor = bufferColor.CGColor;
  41. }
  42. - (void)setBorderColor:(UIColor *)borderColor {
  43. _borderColor = borderColor;
  44. self.borderLayer.strokeColor = borderColor.CGColor;
  45. }
  46. - (void)setBufferStartFraction:(CGFloat)bufferStartFraction {
  47. _bufferStartFraction = bufferStartFraction;
  48. [self setNeedsLayout];
  49. }
  50. - (void)setBufferEndFraction:(CGFloat)bufferEndFraction {
  51. _bufferEndFraction = bufferEndFraction;
  52. [self setNeedsLayout];
  53. }
  54. - (void)layoutSubviews {
  55. [super layoutSubviews];
  56. CGRect bounds = self.bounds;
  57. CGFloat inset = self.borderLayer.lineWidth/2.0;
  58. CGRect borderRect = CGRectInset(bounds, inset, inset);
  59. CGFloat cornerRadius = CGRectGetMidY(bounds); // bounds is correct (flatter)
  60. UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:borderRect cornerRadius:cornerRadius];
  61. self.borderLayer.path = path.CGPath;
  62. CGRect bufferRect = CGRectMake(CGRectGetWidth(bounds)*self.bufferStartFraction,
  63. 0,
  64. CGRectGetWidth(bounds)*(self.bufferEndFraction-self.bufferStartFraction),
  65. CGRectGetHeight(bounds));
  66. UIBezierPath *bufferPath = [UIBezierPath bezierPathWithRect:bufferRect];
  67. self.fillLayer.path = bufferPath.CGPath;
  68. self.borderLayer.frame = bounds;
  69. self.fillLayer.frame = bounds;
  70. self.fillMaskLayer.frame = bounds;
  71. self.fillMaskLayer.path = path.CGPath;
  72. }
  73. @end