ARTLinearGradient.m 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import <React/ARTLinearGradient.h>
  8. #import <React/RCTLog.h>
  9. #import "RCTConvert+ART.h"
  10. @implementation ARTLinearGradient
  11. {
  12. CGGradientRef _gradient;
  13. CGPoint _startPoint;
  14. CGPoint _endPoint;
  15. }
  16. - (instancetype)initWithArray:(NSArray<NSNumber *> *)array
  17. {
  18. if ((self = [super initWithArray:array])) {
  19. if (array.count < 5) {
  20. RCTLogError(@"-[%@ %@] expects 5 elements, received %@",
  21. self.class, NSStringFromSelector(_cmd), array);
  22. return nil;
  23. }
  24. _startPoint = [RCTConvert CGPoint:array offset:1];
  25. _endPoint = [RCTConvert CGPoint:array offset:3];
  26. _gradient = CGGradientRetain([RCTConvert CGGradient:array offset:5]);
  27. }
  28. return self;
  29. }
  30. - (void)dealloc
  31. {
  32. CGGradientRelease(_gradient);
  33. }
  34. - (void)paint:(CGContextRef)context
  35. {
  36. CGGradientDrawingOptions extendOptions =
  37. kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation;
  38. CGContextDrawLinearGradient(context, _gradient, _startPoint, _endPoint, extendOptions);
  39. }
  40. @end