2

我正在视图上绘制注释。我用来画星星的方法如下。

-(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 方法)

4

3 回答 3

4

使用此代码:

-(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 个蓝星

于 2013-11-17T18:36:06.753 回答
1

使用 ,UIBezierPath您可以在Swift 4.

let polygonPath = UIBezierPath()

let xCenter: CGFloat = 0
let yCenter: CGFloat = 0

let w = CGFloat(300)
let r = w / 2.0
let flip: CGFloat = -1.0 // use this to flip the figure 1.0 or -1.0

let polySide = CGFloat(5)

let theta = 2.0 * Double.pi * Double(2.0 / polySide)

polygonPath.move(to: CGPoint(x: xCenter, y: r * flip + yCenter))

for i in 1..<Int(polySide) {
    let x: CGFloat = r * CGFloat( sin(Double(i) * theta) )
    let y: CGFloat = r * CGFloat( cos(Double(i) * theta) )
    polygonPath.addLine(to: CGPoint(x: x + xCenter, y: y * flip + yCenter))
}

polygonPath.close()
于 2018-04-07T17:29:00.737 回答
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];
于 2015-02-28T02:52:24.607 回答