UIView+YMGradient.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // UIView+YMGradient.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2023/11/7.
  6. //
  7. #import "UIView+YMGradient.h"
  8. #import <objc/runtime.h>
  9. @implementation UIView (YMGradient)
  10. + (Class)layerClass {
  11. return [CAGradientLayer class];
  12. }
  13. + (UIView *)ym_gradientViewWithColors:(NSArray<UIColor *> *)colors locations:(NSArray<NSNumber *> *)locations startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint {
  14. UIView *view = [[self alloc] init];
  15. [view ym_setGradientBackgroundWithColors:colors locations:locations startPoint:startPoint endPoint:endPoint];
  16. return view;
  17. }
  18. - (void)ym_setGradientBackgroundWithColors:(NSArray<UIColor *> *)colors locations:(NSArray<NSNumber *> *)locations startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint {
  19. NSMutableArray *colorsM = [NSMutableArray array];
  20. for (UIColor *color in colors) {
  21. [colorsM addObject:(__bridge id)color.CGColor];
  22. }
  23. self.ym_colors = [colorsM copy];
  24. self.ym_locations = locations;
  25. self.ym_startPoint = startPoint;
  26. self.ym_endPoint = endPoint;
  27. }
  28. #pragma mark- Getter&Setter
  29. - (NSArray *)ym_colors {
  30. return objc_getAssociatedObject(self, _cmd);
  31. }
  32. - (void)setYm_colors:(NSArray *)colors {
  33. objc_setAssociatedObject(self, @selector(ym_colors), colors, OBJC_ASSOCIATION_COPY_NONATOMIC);
  34. if ([self.layer isKindOfClass:[CAGradientLayer class]]) {
  35. [((CAGradientLayer *)self.layer) setColors:self.ym_colors];
  36. }
  37. }
  38. - (NSArray<NSNumber *> *)ym_locations {
  39. return objc_getAssociatedObject(self, _cmd);
  40. }
  41. - (void)setYm_locations:(NSArray<NSNumber *> *)locations {
  42. objc_setAssociatedObject(self, @selector(ym_locations), locations, OBJC_ASSOCIATION_COPY_NONATOMIC);
  43. if ([self.layer isKindOfClass:[CAGradientLayer class]]) {
  44. [((CAGradientLayer *)self.layer) setLocations:self.ym_locations];
  45. }
  46. }
  47. - (CGPoint)ym_startPoint {
  48. return [objc_getAssociatedObject(self, _cmd) CGPointValue];
  49. }
  50. - (void)setYm_startPoint:(CGPoint)startPoint {
  51. objc_setAssociatedObject(self, @selector(ym_startPoint), [NSValue valueWithCGPoint:startPoint], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  52. if ([self.layer isKindOfClass:[CAGradientLayer class]]) {
  53. [((CAGradientLayer *)self.layer) setStartPoint:self.ym_startPoint];
  54. }
  55. }
  56. - (CGPoint)ym_endPoint {
  57. return [objc_getAssociatedObject(self, _cmd) CGPointValue];
  58. }
  59. - (void)setYm_endPoint:(CGPoint)endPoint {
  60. objc_setAssociatedObject(self, @selector(ym_endPoint), [NSValue valueWithCGPoint:endPoint], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  61. if ([self.layer isKindOfClass:[CAGradientLayer class]]) {
  62. [((CAGradientLayer *)self.layer) setEndPoint:self.ym_endPoint];
  63. }
  64. }
  65. @end
  66. @implementation UILabel (Gradient)
  67. + (Class)layerClass {
  68. return [CAGradientLayer class];
  69. }
  70. @end