#import <UIKit/UIKit.h>
@interface UIView (TransitionColor)
//添加渐变色
- (void)addTransitionColor:(UIColor *)startColor endColor:(UIColor *)endColor;
- (void)addTransitionColorLeftToRight:(UIColor *)startColor endColor:(UIColor *)endColor;
- (void)addTransitionColor:(UIColor *)startColor
endColor:(UIColor *)endColor
startPoint:(CGPoint)startPoint
endPoint:(CGPoint)endPoint;
@end
#import "UIView+TransitionColor.h"
@implementation UIView (TransitionColor)
//左右渐变
- (void)addTransitionColorLeftToRight:(UIColor *)startColor endColor:(UIColor *)endColor {
[self addTransitionColor:startColor endColor:endColor startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0)];
}
//斜渐变
- (void)addTransitionColor:(UIColor *)startColor endColor:(UIColor *)endColor {
[self addTransitionColor:startColor endColor:endColor startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 1)];
}
- (void)addTransitionColor:(UIColor *)startColor
endColor:(UIColor *)endColor
startPoint:(CGPoint)startPoint
endPoint:(CGPoint)endPoint {
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.colors = @[(__bridge id)startColor.CGColor, (__bridge id)endColor.CGColor];
gradientLayer.locations = @[@0, @1];
gradientLayer.startPoint = startPoint;
gradientLayer.endPoint = endPoint;
gradientLayer.frame = self.bounds;
[self.layer insertSublayer:gradientLayer atIndex:0];
}
@end
使用
[self addTransitionColorLeftToRight:[UIColor greenColor] endColor:[UIColor redColor]];