我有一个进度 View ,我正在尝试为正在进行的更改设置动画。你打算怎样做呢?下面是绘制进度的代码。
提前致谢
- (void)drawRect:(CGRect)rect {
// Drawing code.
CGContextRef context = UIGraphicsGetCurrentContext();
// draw the background
CGContextSaveGState(context);
UIBezierPath *outerPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:0];
[outerPath addClip];
CGPoint backgroundStartPoint = CGPointMake(0.0f, 0.0f);
CGPoint backgroundEndPoint = CGPointMake(0.0f, CGRectGetHeight(rect));
CGContextDrawLinearGradient(context, _backgroundGradient, backgroundStartPoint, backgroundEndPoint, 0);
CGContextRestoreGState(context);
// draw the progress
CGContextSaveGState(context);
UIBezierPath *innerPath = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(rect, 1.0f, 1.0f) cornerRadius: 0];
[innerPath addClip];
[_glossTintColor setFill];
CGRect progressRect = CGRectMake(0, 0, CGRectGetWidth(rect)*_progress, CGRectGetHeight(rect));
CGContextFillRect(context, progressRect);
CGContextRestoreGState(context);
}
最佳答案
每当进度发生变化时,使用[view setNeedsDisplay];触发重绘
请参阅UIView reference在setNeedsDisplay
上。
例如,假设您创建的自定义 View 有一个名为 progress
的属性,实例变量名为 _progress
。我们还假设属性由某个事件/计时器/附加线程更新。
- (void)setProgress:(float)progress
{
_progress = progress;
[self setNeedsDisplay];
}
这会覆盖原始 setter 并添加上述功能。
关于iphone - 动画 CGContextFillRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10961037/