標籤:
#import <UIKit/UIKit.h>
@interface JianBianView : UIView
//為了增加一個表示進度條的進行,可們可以使用mask屬性來屏蔽一部分
@property (nonatomic, strong) CALayer *maskLayer;
@property (nonatomic, assign) CGFloat progress;
//動畫方法
-(void)performAnimation;
-(void)setProgress:(CGFloat)value;
@end
#import "JianBianView.h"
@implementation JianBianView
@synthesize maskLayer,progress;
+(Class)layerClass
{
//設定預設是 CAGradientLayer
return [CAGradientLayer class];
}
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
maskLayer = [CALayer layer];
[maskLayer setFrame:CGRectMake(0, 0, 0, frame.size.height)];
[maskLayer setBackgroundColor:[UIColor blackColor].CGColor];
CAGradientLayer *layer = (id)[self layer];
[layer setStartPoint:CGPointMake(0.0, 0.5)];
[layer setEndPoint:CGPointMake(1.0, 0.5)];
NSMutableArray *colors = [[NSMutableArray alloc] init];
for (NSInteger hue = 0; hue < 360; hue += 5)
{
UIColor *color;
//hue 色調 saturation 飽和度 brightness 亮度
color = [UIColor colorWithHue:1.0*hue/360.0 saturation:1.0 brightness:1.0 alpha:1.0];
[colors addObject:(id)[color CGColor]];
}
[layer setColors:[NSArray arrayWithArray:colors]];
[layer setMask:maskLayer];
}
return self;
}
//建立一個寬度為0的mask覆蓋整個View,mask的顏色不重要,當我們progress屬性更新的時候我們會增加它的寬度 然後在initWithFrame:裡面添加:
/*
maskLayer = [CALayer layer];
[maskLayer setFrame:CGRectMake(0, 0, 0, frame.size.height)];
[maskLayer setBackgroundColor:[UIColor blackColor].CGColor];
*/
//所以重寫setProgress:
-(void)setProgress:(CGFloat)value
{
if (progress != value)
{
progress = MIN(1.0, fabs(value));
[self setNeedsLayout];
}
}
-(void)layoutSubviews
{
CGRect maskRect = [maskLayer frame];
maskRect.size.width = CGRectGetWidth([self bounds]) * progress;
[maskLayer setFrame:maskRect];
}
-(void)performAnimation
{
// Move the last color in the array to the front
// shifting all the other colors.
CAGradientLayer *layer = (id)[self layer];
NSMutableArray *mutable = [[layer colors] mutableCopy];
id lastColor = [mutable lastObject];
[mutable removeLastObject];
[mutable insertObject:lastColor atIndex:0];
NSArray *shiftColors = [NSArray arrayWithArray:mutable];
[layer setColors:shiftColors];
CABasicAnimation *animation;
animation = [CABasicAnimation animationWithKeyPath:@"Colors"];
[animation setToValue:shiftColors];
[animation setDuration:0.08];
[animation setRemovedOnCompletion:YES];
[animation setFillMode:kCAFillModeForwards];
[animation setDelegate:self];
[layer addAnimation:animation forKey:@""];
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
[self performAnimation];
}
@end
視圖控制器 調用
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = @"漸層測試";
self.view.backgroundColor = [UIColor whiteColor];
// [self jianBianMethord];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeMethod) userInfo:nil repeats:YES];
[self addJianBianView];
}
-(void)timeMethod
{
NSLog(@"進入");
progress += 0.1;
[self.jianBianView setProgress:progress];
}
//-----------添加漸層view
-(void)addJianBianView
{
if (self.jianBianView == nil)
{
self.jianBianView = [[JianBianView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 10)];
[self.view addSubview:self.jianBianView];
[self.jianBianView performAnimation];
}
}
iOS 漸層進度條