UILabel文字颜色渐变和闪烁效果
2021-06-09 本文已影响0人
ymhlbj
1获取渐变色,获取渐变的方法有好几种,之所以选择用颜色生成图片,再从图片获取颜色,是因为我的内容不是固定的,如果用一张固定的渐变色获取颜色是不可取。还有一个方法用layer,也不太好,最终选择了这种
+(UIImage *)getImageWithColors:(NSArray *)colors
withBounds:(CGRect)bounds{
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:colors.count];
for (UIColor *color in colors) {
[mutableArray addObject:(__bridge id)color.CGColor];
}
UIGraphicsBeginImageContextWithOptions(bounds.size, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
//绘制渐变层
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradientRef = CGGradientCreateWithColors(colorSpaceRef,
(__bridge CFArrayRef)[mutableArray copy],
NULL);
CGPoint startPoint = CGPointZero;
CGPoint endPoint = CGPointMake(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
CGContextDrawLinearGradient(context, gradientRef, startPoint, endPoint, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
//取到渐变图片
UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
//释放资源
CGColorSpaceRelease(colorSpaceRef);
CGGradientRelease(gradientRef);
UIGraphicsEndImageContext();
return gradientImage;
}
2闪烁效果,就是在加一个label,然后用一个layer,进行动画。
![](https://img.haomeiwen.com/i2312938/6110d3cdabce39d6.png)
![](https://img.haomeiwen.com/i2312938/40069b7bed05fdf8.png)
![](https://img.haomeiwen.com/i2312938/cd127cc2737f8c64.png)
![](https://img.haomeiwen.com/i2312938/c377a73dfc600389.png)
![](https://img.haomeiwen.com/i2312938/4597f774bb4690e1.png)
![](https://img.haomeiwen.com/i2312938/d8290f80478e18e2.png)
![](https://img.haomeiwen.com/i2312938/e471b9b475d3a6b5.png)