UIButtonUILabel笔记篇

view 的渐变色 处理

2022-03-01  本文已影响0人  失忆的程序员
// 

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

/// 返回类型
typedef NS_ENUM(NSUInteger, TransitionColorType) {
    TransitionColorType_LeftToRight    = 0, // 左右
    TransitionColorType_oblique        = 1  // 斜
};

@interface UIView (TransitonColor)
/// 添加渐变色
- (void)addTransitionColor:(UIColor *)startColor endColor:(UIColor *)endColor;
/// 头脚渐变
- (void)addTransitionColorTopToBottom:(UIColor *)startColor endColor:(UIColor *)endColor;
/// 左右渐变
- (void)addTransitionColorLeftToRight:(UIColor *)startColor endColor:(UIColor *)endColor;
///
- (void)addTransitionColorType:(TransitionColorType)type startColor:(UIColor *)startColor endColor:(UIColor *)endColor;
/// 
- (void)addTransitionColor:(UIColor *)startColor
                  endColor:(UIColor *)endColor
                startPoint:(CGPoint)startPoint
                  endPoint:(CGPoint)endPoint;

@end

NS_ASSUME_NONNULL_END


// 

#import "UIView+TransitonColor.h"

@implementation UIView (TransitonColor)
/// 左右渐变
- (void)addTransitionColorLeftToRight:(UIColor *)startColor endColor:(UIColor *)endColor {
    [self addTransitionColor:startColor endColor:endColor startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0)];
}
/// 头脚渐变
- (void)addTransitionColorTopToBottom:(UIColor *)startColor endColor:(UIColor *)endColor {
    [self addTransitionColor:startColor endColor:endColor startPoint:CGPointMake(0, 1) endPoint:CGPointMake(0, 1)];
}

/// 斜渐变
- (void)addTransitionColor:(UIColor *)startColor endColor:(UIColor *)endColor {
    [self addTransitionColor:startColor endColor:endColor startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 1)];
}

/// 
- (void)addTransitionColorType:(TransitionColorType)type startColor:(UIColor *)startColor endColor:(UIColor *)endColor
{
    if (type == TransitionColorType_LeftToRight)
    {// 左右
        [self addTransitionColorLeftToRight:startColor endColor:endColor];
    }
    else if (type == TransitionColorType_oblique)
    {// 斜
        [self addTransitionColor:startColor endColor:endColor];
    }
}

- (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

参考

二哈.gif
上一篇下一篇

猜你喜欢

热点阅读