ASDefaultPlayButton.m 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // ASDefaultPlayButton.m
  3. // AsyncDisplayKit
  4. //
  5. // Created by Luke Parham on 1/27/16.
  6. //
  7. // Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  8. // This source code is licensed under the BSD-style license found in the
  9. // LICENSE file in the root directory of this source tree. An additional grant
  10. // of patent rights can be found in the PATENTS file in the same directory.
  11. //
  12. #import "ASDefaultPlayButton.h"
  13. #import "_ASDisplayLayer.h"
  14. @implementation ASDefaultPlayButton
  15. - (instancetype)init
  16. {
  17. if (!(self = [super init])) {
  18. return nil;
  19. }
  20. self.opaque = NO;
  21. return self;
  22. }
  23. + (void)drawRect:(CGRect)bounds withParameters:(id<NSObject>)parameters isCancelled:(asdisplaynode_iscancelled_block_t)isCancelledBlock isRasterizing:(BOOL)isRasterizing
  24. {
  25. CGFloat originX = bounds.size.width/4;
  26. CGRect buttonBounds = CGRectMake(originX, bounds.size.height/4, bounds.size.width/2, bounds.size.height/2);
  27. CGFloat widthHeight = buttonBounds.size.width;
  28. //When the video isn't a square, the lower bound should be used to figure out the circle size
  29. if (bounds.size.width < bounds.size.height) {
  30. widthHeight = bounds.size.width/2;
  31. originX = (bounds.size.width - widthHeight)/2;
  32. buttonBounds = CGRectMake(originX, (bounds.size.height - widthHeight)/2, widthHeight, widthHeight);
  33. }
  34. if (bounds.size.width > bounds.size.height) {
  35. widthHeight = bounds.size.height/2;
  36. originX = (bounds.size.width - widthHeight)/2;
  37. buttonBounds = CGRectMake(originX, (bounds.size.height - widthHeight)/2, widthHeight, widthHeight);
  38. }
  39. CGContextRef context = UIGraphicsGetCurrentContext();
  40. // Circle Drawing
  41. UIBezierPath *ovalPath = [UIBezierPath bezierPathWithOvalInRect: buttonBounds];
  42. [[UIColor colorWithWhite:0.0 alpha:0.5] setFill];
  43. [ovalPath fill];
  44. // Triangle Drawing
  45. CGContextSaveGState(context);
  46. UIBezierPath *trianglePath = [UIBezierPath bezierPath];
  47. [trianglePath moveToPoint:CGPointMake(originX + widthHeight/3, bounds.size.height/4 + (bounds.size.height/2)/4)];
  48. [trianglePath addLineToPoint:CGPointMake(originX + widthHeight/3, bounds.size.height - bounds.size.height/4 - (bounds.size.height/2)/4)];
  49. [trianglePath addLineToPoint:CGPointMake(bounds.size.width - originX - widthHeight/4, bounds.size.height/2)];
  50. [trianglePath closePath];
  51. [[UIColor colorWithWhite:0.9 alpha:0.9] setFill];
  52. [trianglePath fill];
  53. CGContextRestoreGState(context);
  54. }
  55. @end