2013-08-16 77 views
1

我在視圖上繪製註釋。下面是我用來畫星的方法。drawRect - Draw Star

-(void)drawRect:(CGRect)rect { 

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetLineWidth(context, 2.0); 
CGFloat xCenter = rect.size.width/2; 
CGFloat yCenter = rect.size.height/2; 

float width; 
if(rect.size.width > rect.size.height) { 
    width = rect.size.height; 
} else { 
    width = rect.size.width; 
} 

double r = width/2.0; 
float flip = -1.0; 

double theta = 2.0 * M_PI * (2.0/5.0); // 144 degrees 

CGContextMoveToPoint(context, xCenter, r*flip+yCenter); 

for (NSUInteger k=1; k<5; k++) 
{ 
    float x = r * sin(k * theta); 
    float y = r * cos(k * theta); 
    CGContextAddLineToPoint(context, x+xCenter, y*flip+yCenter); 
} 

CGContextSetStrokeColorWithColor(context, self.color.CGColor); 

CGContextClosePath(context); 
CGContextStrokePath(context); 

} 

我想只有明星的邊界刪除其他內部線是否有任何方法來實現這一目標? (除了使用moveToPoint和addLine方法)

+0

由於你知道明星的界限在哪裏,我能想到的只是清除它內部的路徑。 – geekchic

+1

例如,你會計算其餘5個內點,並使用10個點來製作星形 - 多邊形... – holex

+0

星星在中心包含一個倒五角形。我需要明白它的意思,你的意思是? –

回答

3

使用此代碼:

-(void)drawRect:(CGRect)rect 
{ 
    int aSize = 100.0; 
    float color[4] = { 0.0, 0.0, 1.0, 1.0 }; // Blue 
    CGColorRef aColor = CGColorCreate(CGColorSpaceCreateDeviceRGB(), color); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(context, aSize); 
    CGFloat xCenter = 100.0; 
    CGFloat yCenter = 100.0; 

    float w = 100.0; 
    double r = w/2.0; 
    float flip = -1.0; 

    CGContextSetFillColorWithColor(context, aColor); 


    CGContextSetStrokeColorWithColor(context, aColor); 

    double theta = 2.0 * M_PI * (2.0/5.0); // 144 degrees 

    CGContextMoveToPoint(context, xCenter, r*flip+yCenter); 

    for (NSUInteger k=1; k<5; k++) 
    { 
     float x = r * sin(k * theta); 
     float y = r * cos(k * theta); 
     CGContextAddLineToPoint(context, x+xCenter, y*flip+yCenter); 
    } 

    CGContextClosePath(context); 
    CGContextFillPath(context); 
    } 

這將創建1藍星

0

您可以通過使用此代碼繪製星:

UIBezierPath* starPath = UIBezierPath.bezierPath; 
[starPath moveToPoint: CGPointMake(90, 31)]; 
[starPath addLineToPoint: CGPointMake(98.11, 42.06)]; 
[starPath addLineToPoint: CGPointMake(111.87, 45.86)]; 
[starPath addLineToPoint: CGPointMake(103.12, 56.49)]; 
[starPath addLineToPoint: CGPointMake(103.52, 69.89)]; 
[starPath addLineToPoint: CGPointMake(90, 65.4)]; 
[starPath addLineToPoint: CGPointMake(76.48, 69.89)]; 
[starPath addLineToPoint: CGPointMake(76.88, 56.49)]; 
[starPath addLineToPoint: CGPointMake(68.13, 45.86)]; 
[starPath addLineToPoint: CGPointMake(81.89, 42.06)]; 
[starPath closePath]; 
[UIColor.grayColor setFill]; 
[starPath fill];