标签:
一个分类, 粘贴到项目中即可, 调用方法在下边, 注意这里没头文件的方法声明, 需要自己复制过去
#import "UIView+Extension.h" @implementation UIView (Extension) static CAGradientLayer *gradientLayer_; - (void) insertTransparentGradientFromColor:(UIColor *)fromColor toColor:(UIColor *)toColor fromLocation:(CGFloat)fromLocation toLocation:(CGFloat)toLocation startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint { CAGradientLayer *layer = [CAGradientLayer layer]; gradientLayer_ = layer; layer.colors = @[(__bridge id)fromColor.CGColor, (__bridge id)toColor.CGColor]; layer.locations = @[@(fromLocation) ,@(toLocation)]; layer.frame = self.bounds; layer.startPoint = startPoint; layer.endPoint = endPoint; [self.layer addSublayer:layer]; } @end
调用如下
/** * view 背景颜色的渐变 * * @param fromColor 从什么颜色 * @param toColor 渐变到的颜色 * @param fromLocation 从什么位置渐变 * @param toLocation 渐变到什么位置 * @param startPoint 显示渐变开始的位置 * @param endPoint 显示渐变结束的位置 * */ [self.tmpView insertTransparentGradientFromColor:[UIColor redColor] toColor:[UIColor clearColor] fromLocation:0.5 toLocation:1 startPoint:CGPointMake(0, 0) endPoint:CGPointMake(0, 1)];
标签:
原文地址:http://www.cnblogs.com/MrTao/p/5473441.html