美文网首页
iOS开发中的小技巧3:UILabel文字自左向右颜色渐变

iOS开发中的小技巧3:UILabel文字自左向右颜色渐变

作者: 莫离_焱 | 来源:发表于2017-03-18 13:39 被阅读297次

在开发中滑动跳转页面时,标题需要颜色渐变效果,此时需要重写UILabel写一个继承于UILabel的类;

在.h中添加

@property(nonatomic, strong) UIColor *fillColor;//渐变色

@property(nonatomic, assign) CGFloat progress;//进度

在.m中添加

// 滑动进度

- (void)setProgress:(CGFloat)progress {

_progress = progress;

[self setNeedsDisplay];

}

- (void)drawRect:(CGRect)rect {

[super drawRect:rect];

[_fillColor set];

CGRect newRect = rect;

newRect.size.width = rect.size.width * self.progress;

UIRectFillUsingBlendMode(newRect, kCGBlendModeSourceIn);

}

使用时依旧是使用textColor:

label.textColor = [UIColor cyanColor];//原颜色

label.progress = (line.frame.origin.x+80-SCREEN_W/2-51)/ 80;//进度

label.textColor = [UIColor redColor];//变换后的颜色

一般配合scrollView、UIView的animateWithDuration、button混合使用

相关文章

网友评论

      本文标题:iOS开发中的小技巧3:UILabel文字自左向右颜色渐变

      本文链接:https://www.haomeiwen.com/subject/hjxtgttx.html