2013-04-23 40 views
-1

我很新的核心圖形。我需要畫一個完整的圈子。我該如何解決它?我目前有一個蒙面的圈子。如何繪製一個完整的圓形圖像

我提到this example

enter image description here

//Use the draw rect to draw the Background, the Circle and the Handle 
-(void)drawRect:(CGRect)rect{ 

    [super drawRect:rect]; 

    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

/** Draw the Background **/ 

    //Create the path 
    CGContextAddArc(ctx, self.frame.size.width/2, self.frame.size.height/2, radius, 0, M_PI *2, 0); 

// CGContextFillEllipseInRect(ctx, CGRectMake(self.frame.size.height/2, self.frame.size.width/2, 100, 100)); 
// CGContextSetRGBFillColor(ctx, 0.5, 0.5, 0.5, 1.0); 


    //Set the stroke color to black 
    [[UIColor greenColor]setStroke]; 

    //Define line width and cap 
    CGContextSetLineWidth(ctx, TB_BACKGROUND_WIDTH); 
    CGContextSetLineCap(ctx, kCGLineCapButt); 

    CGContextDrawPath(ctx, kCGPathStroke); 

    UIGraphicsBeginImageContext(CGSizeMake(TB_SLIDER_SIZE,TB_SLIDER_SIZE)); 
    CGContextRef imageCtx = UIGraphicsGetCurrentContext(); 

    CGContextAddArc(imageCtx, self.frame.size.width/2 , self.frame.size.height/2, radius, 0, ToRad(self.self.startAngle), 0); 

    [[UIColor redColor]set]; 

    CGContextSetLineWidth(imageCtx, TB_LINE_WIDTH); 
    CGContextDrawPath(imageCtx, kCGPathStroke); 

    //save the context content into the image mask 
    CGImageRef mask = CGBitmapContextCreateImage(UIGraphicsGetCurrentContext()); 
    UIGraphicsEndImageContext(); 
    CGContextSaveGState(ctx); 
    CGContextClipToMask(ctx, self.bounds, mask); 
    CGImageRelease(mask); 

    //list of components 
    CGFloat components[8] = { 
     0.0, 0.0, 1.0, 1.0,  // Start color - Blue 
     1.0, 1.0, 1.0, 1.0 }; // End color - Violet 

    CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB(); 
    CGGradientRef gradient = CGGradientCreateWithColorComponents(baseSpace, components, NULL, 2); 
    CGColorSpaceRelease(baseSpace), baseSpace = NULL; 

    //Gradient direction 
    CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect)); 
    CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect)); 

    //Draw the gradient 
    CGContextDrawLinearGradient(ctx, gradient, startPoint, endPoint, 0); 
    CGGradientRelease(gradient), gradient = NULL; 

    CGContextRestoreGState(ctx); 

/** Draw the handle **/ 
    [self drawTheHandle:ctx]; 

} 
+1

你是什麼意思一整圈?你想創建一個充滿某種顏色的簡單圓圈嗎? – Amit 2013-04-23 10:07:36

+0

嗨@amit3117,我想要一個完全填充的圓圈,而不是整個中間的黑色。 – Desmond 2013-04-23 11:54:51

回答

0

使backgroup寬度和線寬度140和它的工作原理!

#define TB_BACKGROUND_WIDTH 140 //The width of the dark background 
#define TB_LINE_WIDTH 140   //The width of the active area (the gradient) and the width of the handle 
1
CGContextDrawPath(imageCtx, kCGPathFill); 
+0

謝謝你能告訴我如何使內部的小圓? – Desmond 2013-04-24 04:16:50

+0

我認爲你得到圓的原因是因爲你將線寬設置爲TB_BACKGROUND_WIDTH。如果你想讓洞更小,那麼將線寬設置得更大。這將使外部半徑更大,所以你可能想減少CGContextAddArc半徑。 – geowar 2013-04-24 17:57:30

+0

謝謝@geowar,它的工作原理.... – Desmond 2013-04-25 06:00:04