self.navigationController.navigationBar.sb_progress = 0.8;
GitHub地址
類似于QQ加載網(wǎng)頁(yè)時(shí)候在UINavigationBar上的進(jìn)度條
要實(shí)現(xiàn)一行代碼實(shí)現(xiàn)漸變進(jìn)度條需要以下操作
//1.導(dǎo)入U(xiǎn)INavigationBar+Item
//2.在UINavigationController 中實(shí)現(xiàn)下面兩個(gè)方法
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item
{
navigationBar.sb_gradientProgress.hidden = YES;
return YES;
}
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
navigationBar.sb_gradientProgress.hidden = YES;
return YES;
}
//3.設(shè)置進(jìn)度
self.navigationController.navigationBar.sb_progress = 0.8;
SBGradientProgress 是一個(gè)帶有漸變動(dòng)畫的View,主要思路是給gradientlayer設(shè)置漸變顏色倡蝙,做key為“colors”的動(dòng)畫
- (void)startAnimation
{
//設(shè)置初始漸變值
[self gradientColors];
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"colors"];
anim.values = [self animValues];
[anim setDuration:1];
[anim setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
anim.repeatCount = MAXFLOAT;
[self.layer addAnimation:anim forKey:nil];
}
/**
純色進(jìn)度條
*/
- (void)setPureColor:(UIColor *)pureColor
{
_pureColor = pureColor;
if (pureColor == nil) {
[self startAnimation];
} else {
CAGradientLayer *layer = (id)[self layer];
layer.colors = @[(id)pureColor.CGColor,(id)pureColor.CGColor];
[self.layer removeAllAnimations];
}
}
/**
乾坤大挪移(最后一個(gè)放到前面)
*/
- (NSArray *)shiftColors:(NSArray *)colors {
NSMutableArray *mutable = [colors mutableCopy];
id last = [mutable lastObject];
[mutable removeLastObject];
[mutable insertObject:last atIndex:0];
return [NSArray arrayWithArray:mutable];
}
/**
動(dòng)畫的values <NSArray *<color>>
*/
- (NSArray *)animValues
{
CAGradientLayer *layer = (id)[self layer];
NSMutableArray *values = [NSMutableArray array];
NSArray *colors = nil;
for (NSInteger deg = 0; deg <= 360; deg += 5) {
if (values.count) {
colors = values[deg / 5 - 1];
} else {
colors = layer.colors;
}
[values addObject:[self shiftColors:colors]];
}
return values;
}
<img src="http://upload-images.jianshu.io/upload_images/1899979-42a50711400da6a6.gif?imageMogr2/auto-orient/strip" width = "300" height = "500" alt="好像有點(diǎn)大" align=center />