1. oc
版本
UIView+LayerShadowGradient.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIView (LayerShadowGradient)
/**
* 设置阴影、圆角
* 需要在设置view的frame之后调用此方法
*/
- (void)naLayerShadowColor:(UIColor *)showColor
bgColor:(UIColor *)bgColor;
/**
* 设置阴影、圆角
* 需要在设置view的frame之后调用此方法
*/
- (void)naLayerShadowColor:(UIColor *)showColor
radius:(CGFloat)radius
bgColor:(UIColor *)bgColor;
/**
设置阴影、渐变色、圆角
需要在设置view的frame之后调用此方法
* showColor:阴影色
*/
- (void)naLayerShadowColor:(UIColor *)showColor
gradientCGColor:(NSArray *)gradientCGColors;
/**
设置阴影、渐变色、圆角
需要在设置view的frame之后调用此方法
* showColor:阴影色
* gradientCGColors:渐变色数组
* startPoint:渐变色起始位置
* endPoint:渐变色结束位置
*/
- (void)naLayerShadowColor:(UIColor *)showColor
gradientCGColor:(NSArray *)gradientCGColors
startPoint:(CGPoint)startPoint
endPoint:(CGPoint)endPoint;
@end
NS_ASSUME_NONNULL_END
UIView+LayerShadowGradient.m
#import "UIView+LayerShadowGradient.h"
@implementation UIView (LayerShadowGradient)
/**
* 设置阴影、圆角
* 需要在设置view的frame之后调用此方法
*/
- (void)naLayerShadowColor:(UIColor *)showColor bgColor:(UIColor *)bgColor {
[self naLayerShadowColor:showColor radius:0.5*CGRectGetWidth(self.bounds) bgColor:bgColor];
}
/**
* 设置阴影、圆角
* 需要在设置view的frame之后调用此方法
*/
- (void)naLayerShadowColor:(UIColor *)showColor radius:(CGFloat)radius bgColor:(UIColor *)bgColor {
///圆角
self.layer.cornerRadius = radius;
///阴影
self.layer.shadowColor = showColor.CGColor;
self.layer.shadowOffset = CGSizeMake(0, 0);
self.layer.shadowOpacity = .5;
self.layer.shadowRadius = 10;//阴影半径
self.layer.shouldRasterize = YES;
self.layer.backgroundColor = bgColor.CGColor;//backgroundColor 后面设置的覆盖前面的颜色
//self.backgroundColor = [UIColor yellowColor];//layer.backgroundColor 后面设置的覆盖前面的颜色
}
/**
* 设置阴影、渐变色、圆角
* 需要在设置view的frame之后调用此方法
*/
- (void)naLayerShadowColor:(UIColor *)showColor gradientCGColor:(NSArray *)gradientCGColors startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint {
///阴影
self.layer.shadowColor = showColor.CGColor;
self.layer.shadowOffset = CGSizeMake(0, 0);
self.layer.shadowOpacity = .5;
self.layer.shadowRadius = 10;
self.layer.shouldRasterize = YES;
//self.layer.backgroundColor = [UIColor blueColor].CGColor;//backgroundColor 后面设置的覆盖前面的颜色
//self.backgroundColor = [UIColor yellowColor];//layer.backgroundColor 后面设置的覆盖前面的颜色
///渐变色,圆角
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.colors = gradientCGColors;
gradientLayer.locations = @[@0.0, @1.0];
gradientLayer.startPoint = startPoint;
gradientLayer.endPoint = endPoint;
gradientLayer.frame = self.bounds;
gradientLayer.cornerRadius = 0.5*CGRectGetWidth(self.bounds);
[self.layer addSublayer:gradientLayer];
}
/**
* 设置阴影、渐变色、圆角
* 需要在设置view的frame之后调用此方法
*/
- (void)naLayerShadowColor:(UIColor *)showColor gradientCGColor:(NSArray *)gradientCGColors {
[self naLayerShadowColor:showColor gradientCGColor:gradientCGColors startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1.0, 0)];
}
@end
- (void)shadowTest1 {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(220, 100, 100, 100);
// button.layer.backgroundColor = [UIColor redColor].CGColor;//backgroundColor 后面设置的覆盖前面的颜色
// button.backgroundColor = [UIColor yellowColor];//layer.backgroundColor 后面设置的覆盖前面的颜色
[button naLayerShadowColor:[UIColor blueColor] bgColor:[UIColor yellowColor]];
[self.view addSubview:button];
}
- (void)shadowTest2 {
UIButton *_checkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_checkBtn setTitle:@"ojbk" forState:UIControlStateNormal];
[_checkBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
NSArray *gdColors = @[(__bridge id)[UIColor colorWithRed:0.26 green:0.75 blue:0.98 alpha:1.00].CGColor, (__bridge id)[UIColor colorWithRed:0.18 green:0.48 blue:0.95 alpha:1.00].CGColor];
_checkBtn.frame = CGRectMake(50, 400, 200, 200);
[_checkBtn naLayerShadowColor:[UIColor redColor] gradientCGColor:gdColors];
[self.view addSubview:_checkBtn];
}

效果
2. swift
版本
import UIKit
extension UIView {
//
// MARK: - 设置圆角
//
func naLayerRadius(_ radius: CGFloat = 0) {
///圆角
self.layer.cornerRadius = (radius==0) ? 0.5*self.bounds.width : radius
}
//
// MARK: - 设置阴影、圆角 需要在设置view的frame之后调用此方法
//
func naLayerShadowColor(showColor: UIColor,
bgColor: UIColor,
radius: CGFloat = 0) {
///圆角
self.layer.cornerRadius = (radius==0) ? 0.5*self.bounds.width : radius
///阴影
self.layer.shadowColor = showColor.cgColor
self.layer.shadowOffset = CGSize.zero
self.layer.shadowOpacity = 0.5
self.layer.shadowRadius = 10//阴影半径
self.layer.shouldRasterize = true
self.layer.backgroundColor = bgColor.cgColor//backgroundColor 后面设置的覆盖前面的颜色
//self.backgroundColor = [UIColor yellowColor]//layer.backgroundColor 后面设置的覆盖前面的颜色
}
//
// MARK: - 设置阴影、渐变色、圆角 需要在设置view的frame之后调用此方法
//
func naLayerShadowColor(showColor: UIColor,
gradientCGColors:[Any],
radius: CGFloat = 0.0,
startPoint:CGPoint = CGPoint.zero,
endPoint:CGPoint = CGPoint(x: 1.0, y: 0)) {
///阴影
self.layer.shadowColor = showColor.cgColor
self.layer.shadowOffset = CGSize.zero
self.layer.shadowOpacity = 0.5
self.layer.shadowRadius = 10.0
self.layer.shouldRasterize = true
//self.layer.backgroundColor = [UIColor blueColor].CGColor//backgroundColor 后面设置的覆盖前面的颜色
//self.backgroundColor = [UIColor yellowColor]//layer.backgroundColor 后面设置的覆盖前面的颜色
///渐变色,圆角
let gradientLayer = CAGradientLayer()
gradientLayer.colors = gradientCGColors
// gradientLayer.locations = [@0.0, @1.0]
gradientLayer.startPoint = startPoint
gradientLayer.endPoint = endPoint
gradientLayer.frame = self.bounds
gradientLayer.cornerRadius = (radius==0) ? 0.5*self.bounds.width : radius
self.layer.addSublayer(gradientLayer)
}
}
func testDemo() {
let colors2 = [
colors[0],
colors[1]
]
colorButton.naLayerShadowColor(showColor: .yellow, gradientCGColors: colors2)
}

A1279B8394F88F19928E14A5107814BA.png
网友评论