// // UIView+YMGradient.m // MSYOUPAI // // Created by YoMi on 2023/11/7. // #import "UIView+YMGradient.h" #import @implementation UIView (YMGradient) + (Class)layerClass { return [CAGradientLayer class]; } + (UIView *)ym_gradientViewWithColors:(NSArray *)colors locations:(NSArray *)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 *)colors locations:(NSArray *)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 *)ym_locations { return objc_getAssociatedObject(self, _cmd); } - (void)setYm_locations:(NSArray *)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