我有一个进度条,我使用重复计时器进行动画制作。它工作正常,除了动画不像使用核心动画那样流畅。如果任何人有任何建议如何使用核心动画来实现它,我将不胜感激!对于简单的东西,我没有使用核心动画的问题,但动画渐变等,已经难倒我。如何动画渐变进度条
我的代码是在这里 - 进度条的
-(id)initWithWidth:(float)theWidth radius:(float)theRadius progress:(float)theProgress
{
self = [super initWithFrame:CGRectMake(0, 0, theWidth, theRadius * 2)];
if (self) {
_progressBarWidth = theWidth;
_radius = theRadius;
_progress = (float)theProgress;
self.backgroundColor = [UIColor clearColor];
}
return self;
}
-(void)setProgress:(float)theProgress
{
_progress = theProgress;
[self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect
{
// gradient for blue background (pale blue -> dark blue)
float colours[] = {0.0, 135.0/255.0, 237.0/255.0, 1.0, 1.0/255.0, 24.0/255.0, 140.0/255.0, 1.0};
CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB();
size_t num_locations = 2;
CGFloat locations[2] = {0.0, 1.0};
CGGradientRef gradient = CGGradientCreateWithColorComponents(baseSpace, colours, locations, num_locations);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextMoveToPoint(context, _radius, 0.0);
CGContextAddArcToPoint(context, _progressBarWidth, 0.0, _progressBarWidth, _radius, _radius);
CGContextAddArcToPoint(context, _progressBarWidth, 2.0 * _radius, _progressBarWidth - _radius, 2.0 * _radius, _radius);
CGContextAddArcToPoint(context, 0.0, 2.0 * _radius, 0.0, _radius, _radius);
CGContextAddArcToPoint(context, 0.0, 0.0, _radius, 0.0, _radius);
CGContextClosePath(context);
CGContextClip(context);
CGPoint startPoint = CGPointMake(_progressBarWidth/2.0, 0.0);
CGPoint endPoint = CGPointMake(_progressBarWidth/2.0, 2.0 * _radius);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGGradientRelease(gradient);
CGColorSpaceRelease(baseSpace);
CGContextRestoreGState(context);
// gradient for progressed element (pale yellow -> dark yellow)
float colours2[] = {252.0/255.0, 224.0/255.0, 0.0, 1.0, 246.0/255.0, 191.0/255.0, 0.0, 1.0};
baseSpace = CGColorSpaceCreateDeviceRGB();
gradient = CGGradientCreateWithColorComponents(baseSpace, colours2, locations, num_locations);
context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
// this defines an overall clipping path for the yellow bar (it's got a square end which can protrude when it's near 100%)
CGContextMoveToPoint(context, _radius, kDefaultLineWidth);
CGContextAddArcToPoint(context, _progressBarWidth - kDefaultLineWidth, kDefaultLineWidth, _progressBarWidth - kDefaultLineWidth, _radius, _radius - kDefaultLineWidth);
CGContextAddArcToPoint(context, _progressBarWidth - kDefaultLineWidth, 2.0 * _radius - kDefaultLineWidth, _progressBarWidth - _radius, 2.0 * _radius - kDefaultLineWidth, _radius - kDefaultLineWidth);
CGContextAddArcToPoint(context, kDefaultLineWidth, 2.0 * _radius - kDefaultLineWidth, kDefaultLineWidth, _radius, _radius - kDefaultLineWidth);
CGContextAddArcToPoint(context, kDefaultLineWidth, kDefaultLineWidth, _radius, kDefaultLineWidth, _radius - kDefaultLineWidth);
CGContextClosePath(context);
CGContextClip(context);
// now draw the yellow bar
float progressWidth = _progress * _progressBarWidth;
CGContextMoveToPoint(context, progressWidth, kDefaultLineWidth);
CGContextAddArcToPoint(context, kDefaultLineWidth, kDefaultLineWidth, kDefaultLineWidth, _radius, _radius - kDefaultLineWidth);
CGContextAddArcToPoint(context, kDefaultLineWidth, 2.0 * _radius - kDefaultLineWidth, _radius, 2.0 * _radius - kDefaultLineWidth, _radius - kDefaultLineWidth);
CGContextAddLineToPoint(context, progressWidth, 2.0 * _radius - kDefaultLineWidth);
CGContextClosePath(context);
CGContextClip(context);
startPoint = CGPointMake(progressWidth/2.0, kDefaultLineWidth);
endPoint = CGPointMake(progressWidth/2.0, 2.0 * _radius - kDefaultLineWidth);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGGradientRelease(gradient);
CGColorSpaceRelease(baseSpace);
CGContextRestoreGState(context);
// draw the scale
CGContextSetLineWidth(context, 0.25f);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
float div = _progressBarWidth/10;
for (float i = 1; i < 10; i++) {
CGContextMoveToPoint(context, div * i, 0.25);
CGContextAddLineToPoint(context, div * i, _radius * 2.0 + 1.75);
CGContextDrawPath(context, kCGPathStroke);
}
CGContextSetStrokeColorWithColor(context, [UIColor darkGrayColor].CGColor);
for (float i = 1; i < 10; i++) {
CGContextMoveToPoint(context, div * i - 0.5, 0.25);
CGContextAddLineToPoint(context, div * i - 0.5, _radius * 2.0 + 1.75);
CGContextDrawPath(context, kCGPathStroke);
}
}
图像在0%& 75%:
感谢您的帮助。
你看过[CAGradientLayer](https://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CAGradientLayer_class/Reference/Reference.html)吗?这是为动画梯度制作的 – 2012-07-23 12:00:14