123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- //
- // UIView+YMGradient.m
- // MSYOUPAI
- //
- // Created by YoMi on 2023/11/7.
- //
- #import "UIView+YMGradient.h"
- #import <objc/runtime.h>
- @implementation UIView (YMGradient)
- + (Class)layerClass {
- return [CAGradientLayer class];
- }
- + (UIView *)ym_gradientViewWithColors:(NSArray<UIColor *> *)colors locations:(NSArray<NSNumber *> *)locations startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint {
- UIView *view = [[self alloc] init];
- [view ym_setGradientBackgroundWithColors:colors locations:locations startPoint:startPoint endPoint:endPoint];
- return view;
- }
- - (void)ym_setGradientBackgroundWithColors:(NSArray<UIColor *> *)colors locations:(NSArray<NSNumber *> *)locations startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint {
- NSMutableArray *colorsM = [NSMutableArray array];
- for (UIColor *color in colors) {
- [colorsM addObject:(__bridge id)color.CGColor];
- }
- self.ym_colors = [colorsM copy];
- self.ym_locations = locations;
- self.ym_startPoint = startPoint;
- self.ym_endPoint = endPoint;
- }
- #pragma mark- Getter&Setter
- - (NSArray *)ym_colors {
- return objc_getAssociatedObject(self, _cmd);
- }
- - (void)setYm_colors:(NSArray *)colors {
- objc_setAssociatedObject(self, @selector(ym_colors), colors, OBJC_ASSOCIATION_COPY_NONATOMIC);
- if ([self.layer isKindOfClass:[CAGradientLayer class]]) {
- [((CAGradientLayer *)self.layer) setColors:self.ym_colors];
- }
- }
- - (NSArray<NSNumber *> *)ym_locations {
- return objc_getAssociatedObject(self, _cmd);
- }
- - (void)setYm_locations:(NSArray<NSNumber *> *)locations {
- objc_setAssociatedObject(self, @selector(ym_locations), locations, OBJC_ASSOCIATION_COPY_NONATOMIC);
- if ([self.layer isKindOfClass:[CAGradientLayer class]]) {
- [((CAGradientLayer *)self.layer) setLocations:self.ym_locations];
- }
- }
- - (CGPoint)ym_startPoint {
- return [objc_getAssociatedObject(self, _cmd) CGPointValue];
- }
- - (void)setYm_startPoint:(CGPoint)startPoint {
- objc_setAssociatedObject(self, @selector(ym_startPoint), [NSValue valueWithCGPoint:startPoint], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- if ([self.layer isKindOfClass:[CAGradientLayer class]]) {
- [((CAGradientLayer *)self.layer) setStartPoint:self.ym_startPoint];
- }
- }
- - (CGPoint)ym_endPoint {
- return [objc_getAssociatedObject(self, _cmd) CGPointValue];
- }
- - (void)setYm_endPoint:(CGPoint)endPoint {
- objc_setAssociatedObject(self, @selector(ym_endPoint), [NSValue valueWithCGPoint:endPoint], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- if ([self.layer isKindOfClass:[CAGradientLayer class]]) {
- [((CAGradientLayer *)self.layer) setEndPoint:self.ym_endPoint];
- }
- }
- @end
- @implementation UILabel (Gradient)
- + (Class)layerClass {
- return [CAGradientLayer class];
- }
- @end
|