ASDefaultPlaybackButton.m 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // ASDefaultPlaybackButton.m
  3. // AsyncDisplayKit
  4. //
  5. // Created by Erekle on 5/14/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 "ASDefaultPlaybackButton.h"
  13. @interface ASDefaultPlaybackButton()
  14. {
  15. ASDefaultPlaybackButtonType _buttonType;
  16. }
  17. @end
  18. @implementation ASDefaultPlaybackButton
  19. - (instancetype)init
  20. {
  21. if (!(self = [super init])) {
  22. return nil;
  23. }
  24. self.opaque = NO;
  25. return self;
  26. }
  27. - (void)setButtonType:(ASDefaultPlaybackButtonType)buttonType
  28. {
  29. ASDefaultPlaybackButtonType oldType = _buttonType;
  30. _buttonType = buttonType;
  31. if (oldType != _buttonType) {
  32. [self setNeedsDisplay];
  33. }
  34. }
  35. - (nullable id<NSObject>)drawParametersForAsyncLayer:(_ASDisplayLayer *)layer
  36. {
  37. return @{
  38. @"buttonType" : [NSNumber numberWithInt:_buttonType],
  39. @"color" : self.tintColor
  40. };
  41. }
  42. + (void)drawRect:(CGRect)bounds withParameters:(id<NSObject>)parameters isCancelled:(asdisplaynode_iscancelled_block_t)isCancelledBlock isRasterizing:(BOOL)isRasterizing
  43. {
  44. ASDefaultPlaybackButtonType buttonType = [parameters[@"buttonType"] intValue];
  45. UIColor *color = parameters[@"color"];
  46. CGContextRef context = UIGraphicsGetCurrentContext();
  47. CGContextSaveGState(context);
  48. UIBezierPath* bezierPath = [UIBezierPath bezierPath];
  49. if (buttonType == ASDefaultPlaybackButtonTypePlay) {
  50. [bezierPath moveToPoint: CGPointMake(0, 0)];
  51. [bezierPath addLineToPoint: CGPointMake(0, bounds.size.height)];
  52. [bezierPath addLineToPoint: CGPointMake(bounds.size.width, bounds.size.height/2)];
  53. [bezierPath addLineToPoint: CGPointMake(0, 0)];
  54. [bezierPath closePath];
  55. } else if (buttonType == ASDefaultPlaybackButtonTypePause) {
  56. CGFloat pauseSingleLineWidth = bounds.size.width / 3.0;
  57. [bezierPath moveToPoint: CGPointMake(0, bounds.size.height)];
  58. [bezierPath addLineToPoint: CGPointMake(pauseSingleLineWidth, bounds.size.height)];
  59. [bezierPath addLineToPoint: CGPointMake(pauseSingleLineWidth, 0)];
  60. [bezierPath addLineToPoint: CGPointMake(0, 0)];
  61. [bezierPath addLineToPoint: CGPointMake(0, bounds.size.height)];
  62. [bezierPath closePath];
  63. [bezierPath moveToPoint: CGPointMake(pauseSingleLineWidth * 2, 0)];
  64. [bezierPath addLineToPoint: CGPointMake(pauseSingleLineWidth * 2, bounds.size.height)];
  65. [bezierPath addLineToPoint: CGPointMake(bounds.size.width, bounds.size.height)];
  66. [bezierPath addLineToPoint: CGPointMake(bounds.size.width, 0)];
  67. [bezierPath addLineToPoint: CGPointMake(pauseSingleLineWidth * 2, 0)];
  68. [bezierPath closePath];
  69. }
  70. [color setFill];
  71. [bezierPath fill];
  72. CGContextRestoreGState(context);
  73. }
  74. @end