#pragma mark - createLayer
//加上这句 set方法
@property (nonatomic, assign) CGFloat progress;
CAShapeLayer *_progressLayer;
UIBezierPath *_progressPath;
CALayer * _maskLayer;
CAGradientLayer *_gradlayer;
//该方法是在自定义view中实现
- (void)createLayerWithView:(UIView *)view
{
// _progressLayer = [CAShapeLayer layer];
// _progressLayer.fillColor = nil;
// _progressLayer.lineCap = kCALineCapRound;
// _progressLayer.frame = view.bounds;
// _progressLayer.lineWidth = 10;
// _progressLayer.strokeColor = [UIColor orangeColor].CGColor;
// [view.layer addSublayer:_progressLayer];
//
// _progressPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(view.bounds.size.width/2, view.bounds.size.height/2) radius:(view.bounds.size.width - 1)/2+1 startAngle:M_PI endAngle:3*M_PI clockwise:YES];
// _progressLayer.path = _progressPath.CGPath;
// _progressLayer.strokeEnd = 1;
//
// CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
// pathAnimation.duration = 3.0;
// pathAnimation.repeatCount = MAXFLOAT;
// pathAnimation.fromValue = @(0);
// pathAnimation.toValue = @(1);
// pathAnimation.removedOnCompletion = NO;
// pathAnimation.fillMode = kCAFillModeForwards;
// pathAnimation.autoreverses = NO; // yes 会回到起点
// CABasicAnimation *pathAnimation2 = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
// pathAnimation2.duration = 7.0;
// pathAnimation2.repeatCount = MAXFLOAT;
// pathAnimation2.fromValue = @(0.3);
// pathAnimation2.toValue = @(1);
// pathAnimation2.removedOnCompletion = NO;
// pathAnimation2.fillMode = kCAFillModeForwards;
// pathAnimation2.autoreverses = NO; // yes 会回到起点
// pathAnimation2.beginTime = 3.0;
// CAAnimationGroup *group = [CAAnimationGroup animation];
// group.duration = 10;
// group.removedOnCompletion = NO;
// group.fillMode = kCAFillModeForwards;
// group.repeatCount = MAXFLOAT;
// group.autoreverses = NO;
// group.animations = @[pathAnimation];
//
// [_progressLayer addAnimation:group forKey:@"circle"];
// 彩带条
_gradlayer = [CAGradientLayer layer];
_gradlayer.frame = CGRectMake(0, 0, view.frame.size.width, 10);
[_gradlayer setStartPoint:CGPointMake(0.0, 0.5)];
[_gradlayer setEndPoint:CGPointMake(1.0, 0.5)];
NSMutableArray *colorAry = [NSMutableArray array];
for (NSInteger hue = 0; hue <= 360; hue += 5)
{
UIColor *color = [UIColor colorWithHue:1.0*hue/360.0 saturation:1.0 brightness:1.0 alpha:1.0];
[colorAry addObject:(id)[color CGColor]];
}
[_gradlayer setColors:[NSArray arrayWithArray:colorAry]];
[view.layer addSublayer:_gradlayer];
_maskLayer = [CALayer layer];
[_maskLayer setFrame:CGRectMake(0, 0, view.frame.size.width, 10)];
[_maskLayer setBackgroundColor:[[UIColor blackColor] CGColor]];
[_gradlayer setMask:_maskLayer];
//Start the animation
[self performAnimation];
[self performSelector:@selector(gradualProgress) withObject:nil afterDelay:1];
}
- (void)gradualProgress
{
self.progress += 0.1;
if (self.progress >= 1.0)
{
self.progress = 1;
// return;
}
[self performSelector:@selector(gradualProgress) withObject:nil afterDelay:0.3];
}
- (void)layoutSubviews
{
CGRect maskRect = [_maskLayer frame];
maskRect.size.width = CGRectGetWidth([self bounds]) * self.progress;
[_maskLayer setFrame:maskRect];
}
- (void)setProgress:(CGFloat)value
{
if (_progress != value)
{
_progress = MIN(1.0, fabs(value));
[self setNeedsLayout];
}
}
- (void)performAnimation
{
NSMutableArray * colorArray = [[_gradlayer colors] mutableCopy];
UIColor * lastColor = [colorArray lastObject];
[colorArray removeLastObject];
[colorArray insertObject:lastColor atIndex:0];
NSArray * shiftedColors = [NSArray arrayWithArray:colorArray];
[_gradlayer setColors:shiftedColors];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"colors"];
[animation setToValue:shiftedColors];
[animation setDuration:0.08];
[animation setFillMode:kCAFillModeForwards];
[animation setDelegate:self];
[_gradlayer addAnimation:animation forKey:@"animateGradient"];
}
- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag
{
[self performAnimation];
}
//使用方法
#warning CALayer test
[self createLayerWithView:showMoneyBackView];
彩带条实现
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一丶原理 先看下效果: 无渐变: 由2个圆环,一个背景灰色圆环+一个进度圆环; 1.圆环 CAShapeLayer...
- 地址: 实现过程简介: 界面组成:底部是TableView(占据控制器的整个View)顶部的一个View以及其内部...