一、创建椭圆path
绘制水平渐变渐变色
#import <UIKit/UIKit.h>
@interface UIImage (Tools)
+ (UIImage *)lineGradientImageWithSize:(CGSize)size colors:(NSArray *)colors;
@end
#import "UIImage+Tools.h"
@implementation UIImage (Tools)
+ (UIImage *)lineGradientImageWithSize:(CGSize)size colors:(NSArray *)colors
{
//创建CGContextRef
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
//创建CGMutablePathRef
CGMutablePathRef path = [self pathWithSize:size radisu:size.height/2];
// 画图
[self drawLinearGradient:context path:path colors:colors];
//注意释放CGMutablePathRef
CGPathRelease(path);
//从Context中获取图像,并显示在界面上
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
/**
创建圆角的path
@param size
@param radius
@return path
*/
+ (CGMutablePathRef)pathWithSize:(CGSize)size radisu:(CGFloat)radius
{
//创建CGMutablePathRef
CGMutablePathRef path = CGPathCreateMutable();
//绘制Path
CGFloat width = size.width;
CGFloat height = size.height;
CGPathMoveToPoint(path, NULL, radius, height);
CGPathAddArc(path, NULL, radius, radius, radius, M_PI_2, -M_PI_2, 0);
CGPathAddLineToPoint(path, NULL, width-radius, 0);
CGPathAddArc(path, NULL, width-radius, radius, radius, -M_PI_2, M_PI_2, 0);
CGPathAddLineToPoint(path, NULL, width-radius, height);
CGPathCloseSubpath(path);
return path;
}
/**
绘制水平方向线性渐变图片
@param context
@param path
@param colors
*/
+ (void)drawLinearGradient:(CGContextRef)context path:(CGPathRef)path colors:(NSArray *)colors
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = { 0.0, 1.0 };
NSMutableArray *colorList = [NSMutableArray array];
for(UIColor *color in colors){
if ([color isKindOfClass:[UIColor class]]) {
[colorList addObject:(__bridge id)color.CGColor];
}else{
[colorList addObject:color];
}
}
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colorList, locations);
CGRect pathRect = CGPathGetBoundingBox(path);
//具体方向可根据需求修改
CGPoint startPoint = CGPointMake(CGRectGetMinX(pathRect), CGRectGetMinY(pathRect));
CGPoint endPoint = CGPointMake(CGRectGetMaxX(pathRect), CGRectGetMinY(pathRect));
CGContextSaveGState(context);
CGContextAddPath(context, path);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
@end
创建一个渐变背景,有阴影的按钮
#import <UIKit/UIKit.h>
@interface GradientBgButton : UIButton
@end
#import "GradientBgButton.h"
#import "UIImage+Tools.h"
@interface GradientBgButton()
@property (nonatomic,assign) CGSize oldSize;
@end
@implementation GradientBgButton
- (void)layoutSubviews
{
[super layoutSubviews];
if(!CGSizeEqualToSize(self.bounds.size, self.oldSize)){
self.oldSize = self.bounds.size;
NSArray *normalColors = @[[UIColor redColor],[UIColor greenColor]];
[self setBackgroundImage:[UIImage lineGradientImageWithSize:self.bounds.size colors:normalColors] forState:UIControlStateNormal];
NSArray *hightColors = @[[UIColor yellowColor],[UIColor purpleColor]];
[self setBackgroundImage:[UIImage lineGradientImageWithSize:self.bounds.size colors:hightColors] forState:UIControlStateHighlighted];
self.layer.shadowColor = [UIColor blueColor].CGColor;
self.layer.shadowOffset = CGSizeMake(0,-1);
self.layer.shadowOpacity = 0.5;
self.layer.shadowRadius = 5;
// 单边阴影 顶边
CGFloat height = self.bounds.size.height;
CGFloat width = self.bounds.size.width;
CGFloat radius = height/2;
CGRect shadowRect = CGRectMake(radius, height, width-radius*2, self.layer.shadowRadius);
UIBezierPath *path = [UIBezierPath bezierPathWithRect:shadowRect];
self.layer.shadowPath = path.CGPath;
}
}
@end

网友评论