2013-08-16 3 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); 

} 

다른 내부 선을 제거하는 별의 경계를 갖고 싶습니다.

+0

이후를 당신은 별의 경계가 어디 있는지 알고 있습니다. 내가 생각할 수있는 전부는 그것의 내부 경로를 삭제하는 것입니다. – geekchic

+1

예를 들어 나머지 5 개의 안쪽 점을 계산하면 10 점을 사용하여 별 모양이됩니다. – holex

+0

별의 가운데에 역 대각선이 있습니다. 그 점을 알아야 겠어, 그 뜻 이니? –

답변

3

사용 (떨어져 moveToPoint 및 addLine 방법을 사용하여)에서이 코드 :

-(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];