-
封裝的工具類代碼如下
封裝的工具類代碼如下
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIView (JKLayout)
@property CGPoint jk_startPoint;
@property CGPoint jk_endPoint;
@property(nullable, copy) NSArray *jk_colors;
@property(nullable, copy) NSArray<NSNumber *> *jk_locations;
#pragma mark 3舌缤、給繼承于view的類添加 四周 陰影
/**
給繼承于view的類添加 四周 陰影
@param shadowRadius 陰影半徑
@param theColor 陰影的顏色
@param size 陰影的偏移度:CGSizeMake(X[正的右偏移,負(fù)的左偏移], Y[正的下偏移,負(fù)的上偏移]);
@param opacity 陰影的透明度
*/
- (void)jk_addShadowToViewShadowRadius:(CGFloat)shadowRadius withColor:(UIColor *)theColor withShadowOffset:(CGSize)size withShadowOpacity:(float)opacity;
#pragma mark 4歼培、給繼承于view的類添加 單邊 陰影
/**
給繼承于view的類添加 單邊 陰影
@param shadowRadius 陰影半徑
@param theColor 陰影的顏色
@param size 陰影的偏移度:CGSizeMake(X[正的右偏移,負(fù)的左偏移], Y[正的下偏移,負(fù)的上偏移]);
@param opacity 陰影的透明度
*/
- (void)jk_addShadowSingleToViewShadowRadius:(CGFloat)shadowRadius withColor:(UIColor *)theColor withShadowOffset:(CGSize)size withShadowOpacity:(float)opacity;
#pragma mark 5脐嫂、繪制(類方法)漸變色返回一個(gè)自身對象
/**
繪制漸變色返回一個(gè)自身對象
@param colors 顏色數(shù)組计寇,最前面的是第一個(gè)顏色艳吠,后面的依次排列
@param locations 起始位置
@param startPoint 開始的點(diǎn)
@param endPoint 結(jié)束的點(diǎn)
@return 返回的漸變色之后的view
*/
+ (UIView *_Nullable)jk_gradientViewWithColors:(NSArray<UIColor *> *_Nullable)colors locations:(NSArray<NSNumber *> *_Nullable)locations startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint;
#pragma mark 6队丝、繪制(對象方法)漸變色
/**
繪制漸變色
@param colors 顏色數(shù)組,最前面的是第一個(gè)顏色呢堰,后面的依次排列
@param locations 起始位置
@param startPoint 開始的點(diǎn)
@param endPoint 結(jié)束的點(diǎn)
*/
- (void)jk_setGradientBackgroundWithColors:(NSArray<UIColor *> *_Nullable)colors locations:(NSArray<NSNumber *> *_Nullable)locations startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint;
@end
NS_ASSUME_NONNULL_END
#import "UIView+JKLayout.h"
#import <objc/runtime.h>
@implementation UIView (JKLayout)
/**-----------**/
- (void)jk_addShadowToViewShadowRadius:(CGFloat)shadowRadius withColor:(UIColor *)theColor withShadowOffset:(CGSize)size withShadowOpacity:(float)opacity{
// 陰影顏色
self.layer.shadowColor = theColor.CGColor;
// 陰影的偏移 CGSizeMake(X[正的右偏移,負(fù)的左偏移], Y[正的下偏移,負(fù)的上偏移]);
self.layer.shadowOffset = size;
// 陰影透明度,默認(rèn)0,不透明度(不透明度只要大于1就說明是有陰影的)
self.layer.shadowOpacity = opacity;
// 陰影半徑凡泣,默認(rèn)3
self.layer.shadowRadius = shadowRadius;
}
/**-----------**/
- (void)jk_addShadowSingleToViewShadowRadius:(CGFloat)shadowRadius withColor:(UIColor *)theColor withShadowOffset:(CGSize)size withShadowOpacity:(float)opacity{
self.layer.shadowColor = theColor.CGColor;
self.layer.shadowOffset = size;
self.layer.shadowOpacity = opacity;
self.layer.shadowRadius = shadowRadius;
// 單邊陰影 頂邊
float shadowPathWidth = self.layer.shadowRadius;
CGRect shadowRect = CGRectMake(0, 0-shadowPathWidth/2.0, self.bounds.size.width, shadowPathWidth);
UIBezierPath *path = [UIBezierPath bezierPathWithRect:shadowRect];
self.layer.shadowPath = path.CGPath;
}
/**-----------**/
+ (Class)layerClass {
return [CAGradientLayer class];
}
+ (UIView *)jk_gradientViewWithColors:(NSArray<UIColor *> *)colors locations:(NSArray<NSNumber *> *)locations startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint {
UIView *view = [[self alloc] init];
[view jk_setGradientBackgroundWithColors:colors locations:locations startPoint:startPoint endPoint:endPoint];
return view;
}
- (void)jk_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.jk_colors = [colorsM copy];
self.jk_locations = locations;
self.jk_startPoint = startPoint;
self.jk_endPoint = endPoint;
}
- (NSArray *)jk_colors {
return objc_getAssociatedObject(self, _cmd);
}
- (void)setJk_colors:(NSArray *)colors {
objc_setAssociatedObject(self, @selector(jk_colors), colors, OBJC_ASSOCIATION_COPY_NONATOMIC);
if ([self.layer isKindOfClass:[CAGradientLayer class]]) {
[((CAGradientLayer *)self.layer) setColors:self.jk_colors];
}
}
- (NSArray<NSNumber *> *)jk_locations {
return objc_getAssociatedObject(self, _cmd);
}
- (void)setJk_locations:(NSArray<NSNumber *> *)locations {
objc_setAssociatedObject(self, @selector(jk_locations), locations, OBJC_ASSOCIATION_COPY_NONATOMIC);
if ([self.layer isKindOfClass:[CAGradientLayer class]]) {
[((CAGradientLayer *)self.layer) setLocations:self.jk_locations];
}
}
- (CGPoint)jk_startPoint {
return [objc_getAssociatedObject(self, _cmd) CGPointValue];
}
-(void)setJk_startPoint:(CGPoint)startPoint{
objc_setAssociatedObject(self, @selector(jk_startPoint), [NSValue valueWithCGPoint:startPoint], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
if ([self.layer isKindOfClass:[CAGradientLayer class]]) {
[((CAGradientLayer *)self.layer) setStartPoint:self.jk_startPoint];
}
}
- (CGPoint)jk_endPoint {
return [objc_getAssociatedObject(self, _cmd) CGPointValue];
}
- (void)setJk_endPoint:(CGPoint)endPoint {
objc_setAssociatedObject(self, @selector(jk_endPoint), [NSValue valueWithCGPoint:endPoint], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
if ([self.layer isKindOfClass:[CAGradientLayer class]]) {
[((CAGradientLayer *)self.layer) setEndPoint:self.jk_endPoint];
}
}
/**-----------**/
@end
- 調(diào)用
#import "ViewController.h"
#import "UIView+JKLayout.h"
#define JKRGBCOLOR(r,g,b,p) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:p]
#define JK_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define JK_SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(50, 200,JK_SCREEN_WIDTH-100 , 100)];
view.layer.cornerRadius = 50;
[view jk_setGradientBackgroundWithColors:@[JKRGBCOLOR(255,219,0,1),JKRGBCOLOR(255,185,17,1)] locations:nil startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0)];
[view jk_addShadowToViewShadowRadius:4 withColor:JKRGBCOLOR(255,185,17,1) withShadowOffset:CGSizeMake(0, 2) withShadowOpacity:0.48];
[self.view addSubview:view];
}
@end
-
效果如下枉疼,可根據(jù)自己的需要實(shí)現(xiàn)
效果圖