在開發中滑動跳轉頁面時,標題需要顏色漸變效果,此時需要重寫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混合使用