酷店

UIView+Gradient.m 2.7KB

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