gpt4 book ai didi

ios - drawRect - 绘制星星

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:29:58 25 4
gpt4 key购买 nike

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

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

最佳答案

使用这段代码:

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

关于ios - drawRect - 绘制星星,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18268079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com