我希望画一条弯曲的线,直到它完成一个完整的旋转并连接成为一个完整的圆,只是圆的轮廓,不填充。这必须在几秒钟内进行动画处理。
有人可以指点我方向吗?我已经问过一个类似的问题similar question,但我表达得不正确,所以每个人都很难理解我的意思,因此它在问题的海洋中丢失了。
非常感谢任何帮助
[编辑]
我目前正在子类化UIView并覆盖drawRect。我找到了绘制填充圆的代码,但我只需要描边。
有人可以指点我方向吗?我已经问过一个类似的问题similar question,但我表达得不正确,所以每个人都很难理解我的意思,因此它在问题的海洋中丢失了。
非常感谢任何帮助
[编辑]
我目前正在子类化UIView并覆盖drawRect。我找到了绘制填充圆的代码,但我只需要描边。
- (void)drawRect:(CGRect)rect {
// Drawing code
CGRect allRect = self.bounds;
CGRect circleRect = CGRectInset(allRect, 2.0f, 2.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
// Draw background
CGContextSetRGBStrokeColor(context, self.strokeValueRed, self.strokeValueGreen, self.strokeValueBlue, self.strokeValueAlpha); // white
CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 0.1f); // translucent white
CGContextSetLineWidth(context, self.lineWidth);
CGContextFillEllipseInRect(context, circleRect);
CGContextStrokeEllipseInRect(context, circleRect);
// Draw progress
CGPoint center = CGPointMake(allRect.size.width / 2, allRect.size.height / 2);
CGFloat radius = (allRect.size.width - 4) / 2;
CGFloat startAngle = - ((float)M_PI / 2); // 90 degrees
CGFloat endAngle = (self.progress * 2 * (float)M_PI) + startAngle;
CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f); // white
CGContextMoveToPoint(context, center.x, center.y);
CGContextAddArc(context, center.x, center.y, radius, startAngle, endAngle, 0);
CGContextClosePath(context);
CGContextFillPath(context);
}
[编辑 #2]
我修改了代码,移除了所有的填充引用,但现在它没有画出任何东西 :( 有什么想法吗?
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGRect allRect = self.bounds;
CGContextRef context = UIGraphicsGetCurrentContext();
// Draw background
CGContextSetRGBStrokeColor(context, self.strokeValueRed, self.strokeValueGreen, self.strokeValueBlue, self.strokeValueAlpha); // white
CGContextSetLineWidth(context, 5);
// Draw progress
CGPoint center = CGPointMake(allRect.size.width / 2, allRect.size.height / 2);
CGFloat radius = (allRect.size.width - 4) / 2;
CGFloat startAngle = - ((float)M_PI / 2); // 90 degrees
CGFloat endAngle = (self.progress * 2 * (float)M_PI) + startAngle;
CGContextAddArc(context, center.x, center.y, radius, startAngle, endAngle, 0);
CGContextStrokePath(context);
}
[编辑 #3] 解决方案
我感觉自己像个傻瓜!问题在于描边颜色值没有被初始化,这意味着线条被绘制出来,但是显然看不到它!
removedOnCompletion
应该等于YES
。strokeEnd
属性默认已经是1.0,一旦完成动画就没有必要将其保留在屏幕上,否则会浪费电池/性能。这是最佳解决方案! - Drmorgan