drawRect-Draw Star

问题描述 投票:2回答:3

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

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

ios drawing core-graphics
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个蓝星


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

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

0
投票

您可以查看我对这个问题的回答:

How to draw heart shape in UIView (iOS)?

我涵盖了绘制心脏形状和星形形状,因为它们都采用相同的基本方法。星星看起来就像美国国旗上的星星一样清晰。产生的星星具有透明的背景,可以轻松覆盖在其他物体上。

© www.soinside.com 2019 - 2024. All rights reserved.