12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- //
- // UIView+Gradient.m
- // AZCategory
- //
- // Created by Alfred Zhang on 2017/6/29.
- // Copyright © 2017年 Alfred Zhang. All rights reserved.
- //
- #import "UIView+Gradient.h"
- #import <objc/runtime.h>
- @implementation UIView (Gradient)
- + (Class)layerClass {
- return [CAGradientLayer class];
- }
- + (UIView *)gradientViewWithColors:(NSArray<UIColor *> *)colors locations:(NSArray<NSNumber *> *)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<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.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<NSNumber *> *)locations {
- return objc_getAssociatedObject(self, _cmd);
- }
- - (void)setLocations:(NSArray<NSNumber *> *)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
|