2012-06-30 27 views
0

我写了这个类,绘制一个圆一个动画进度(它借鉴基础上的浮动进步的扇形)Objecive-C画出一环跟一圆形

@implementation MXMProgressView 

@synthesize progress; 

- (id)initWithDefaultSize { 
    int circleOffset = 45.0f; 
    self = [super initWithFrame:CGRectMake(0.0f, 
              0.0f, 
              135.0f + circleOffset, 
              135.0f + circleOffset)]; 
    self.backgroundColor = [UIColor clearColor]; 
    return self; 
} 

- (void)drawRect:(CGRect)rect { 

    CGRect allRect = self.bounds; 
    CGRect circleRect = CGRectMake(allRect.origin.x + 2, allRect.origin.y + 2, allRect.size.width - 4, 
            allRect.size.height - 4); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // background image 
    //UIImage *image = [UIImage imageNamed:@"loader_disc_hover.png"]; 
    //[image drawInRect:circleRect]; 

    // Orange: E27006 
    CGContextSetRGBFillColor(context, 
          ((CGFloat)0xE2/(CGFloat)0xFF), 
          ((CGFloat)0x70/(CGFloat)0xFF), 
          ((CGFloat)0x06/(CGFloat)0xFF), 
          0.01f); // fill 

    //CGContextSetLineWidth(context, 2.0); 
    CGContextFillEllipseInRect(context, circleRect); 
    //CGContextStrokeEllipseInRect(context, circleRect); 

    // Draw progress 
    float x = (allRect.size.width/2); 
    float y = (allRect.size.height/2); 

    // Orange: E27006 
    CGContextSetRGBFillColor(context, 
          ((CGFloat)0xE2/(CGFloat)0xFF), 
          ((CGFloat)0x70/(CGFloat)0xFF), 
          ((CGFloat)0x06/(CGFloat)0xFF), 
          1.0f); // progress 

    CGContextMoveToPoint(context, x, y); 
    CGContextAddArc(context, x, y, (allRect.size.width - 4)/2, -M_PI_2, (self.progress * 2 * M_PI) - M_PI_2, 0); 
    CGContextClosePath(context); 
    CGContextFillPath(context); 
} 

@end 

现在我想做的事我使用相同的进度动画绘制环形图形,而不是填充整个圆,因此圆形扇形再次不是从圆的中心开始。

我试着用CGContextAddEllipseInRectCGContextEOFillPath(context);

没有成功。

+0

太过本地化。尝试概括你的问题。更多,以便它可以帮助未来的游客。 –

+1

您是否知道在笔画路径之前可以更改线宽? – NSResponder

回答

1

我想你会需要构建一个更复杂的路径是这样的:

// Move to start point of outer arc (which might not be required) 
CGContextMoveToPoint(context, x+outerRadius*cos(startAngle), y+outerRadius*sin(startAngle)); 
// Add outer arc to path (counterclockwise) 
CGContextAddArc(context, x, y, outerRadius, startAngle, endAngle, 0); 
// move *inward* to start point of inner arc 
CGContextMoveToPoint(context, x+innerRadius*cos(endAngle), y+innerRadius*sin(endAngle)); 
// Add inner arc to path (clockwise) 
CGContextAddArc(context, x, y, innerRadius, endAngle, StartAngle, 1); 
// Close the path from end of inner arc to start of outer arc 
CGContextClosePath(context); 

注:我没有尝试过上面的代码自己

+0

我也喜欢@NSResponder的想法,只是用粗宽的线条抚摸单个弧线,而不是填充复杂的路径。 –

0

廉价和肮脏的解决方案:

  • 通过要绘制的环的粗细绘制一个比原始圆小的实心圆。
  • 在原来的圆圈顶部绘制此圆圈,您将看到的所有动画都是圆环。
+0

使用剪切路径 – hypercrypt

+0

我说这是一个快速和肮脏的解决方案。你需要做的事情就是看看效果是什么样子的,然后在需要的时候以更有效的方式重写它,然后再进行调整。 – Abizern