// // UIView+Gradient.m // AZCategory // // Created by Alfred Zhang on 2017/6/29. // Copyright © 2017年 Alfred Zhang. All rights reserved. // #import "UIView+Gradient.h" #import @implementation UIView (Gradient) + (Class)layerClass { return [CAGradientLayer class]; } + (UIView *)gradientViewWithColors:(NSArray *)colors locations:(NSArray *)locations startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint { UIView *view = [[self alloc] init]; [view setGradientBackgroundWithColors:colors locations:locations startPoint:startPoint endPoint:endPoint]; return view; } - (void)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.colors = [colorsM copy]; self.locations = locations; self.startPoint = startPoint; self.endPoint = endPoint; } #pragma mark- Getter&Setter - (NSArray *)colors { return objc_getAssociatedObject(self, _cmd); } - (void)setColors:(NSArray *)colors { objc_setAssociatedObject(self, @selector(colors), colors, OBJC_ASSOCIATION_COPY_NONATOMIC); if ([self.layer isKindOfClass:[CAGradientLayer class]]) { [((CAGradientLayer *)self.layer) setColors:self.colors]; } } - (NSArray *)locations { return objc_getAssociatedObject(self, _cmd); } - (void)setLocations:(NSArray *)locations { objc_setAssociatedObject(self, @selector(locations), locations, OBJC_ASSOCIATION_COPY_NONATOMIC); if ([self.layer isKindOfClass:[CAGradientLayer class]]) { [((CAGradientLayer *)self.layer) setLocations:self.locations]; } } - (CGPoint)startPoint { return [objc_getAssociatedObject(self, _cmd) CGPointValue]; } - (void)setStartPoint:(CGPoint)startPoint { objc_setAssociatedObject(self, @selector(startPoint), [NSValue valueWithCGPoint:startPoint], OBJC_ASSOCIATION_RETAIN_NONATOMIC); if ([self.layer isKindOfClass:[CAGradientLayer class]]) { [((CAGradientLayer *)self.layer) setStartPoint:self.startPoint]; } } - (CGPoint)endPoint { return [objc_getAssociatedObject(self, _cmd) CGPointValue]; } - (void)setEndPoint:(CGPoint)endPoint { objc_setAssociatedObject(self, @selector(endPoint), [NSValue valueWithCGPoint:endPoint], OBJC_ASSOCIATION_RETAIN_NONATOMIC); if ([self.layer isKindOfClass:[CAGradientLayer class]]) { [((CAGradientLayer *)self.layer) setEndPoint:self.endPoint]; } } @end @implementation UILabel (Gradient) + (Class)layerClass { return [CAGradientLayer class]; } @end