加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 如何使用Quartz Core绘制星星?

发布时间:2020-12-16 03:13:51 所属栏目:百科 来源:网络整理
导读:我正在尝试调整苹果提供的示例,以便以编程方式绘制星星,代码如下: CGContextRef context = UIGraphicsGetCurrentContext();CGContextSetLineWidth(context,aSize); for (NSUInteger i=0; istars; i++) { CGContextSetFillColorWithColor(context,aColor); C
我正在尝试调整苹果提供的示例,以便以编程方式绘制星星,代码如下:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context,aSize);

    for (NSUInteger i=0; i<stars; i++) 
    {

       CGContextSetFillColorWithColor(context,aColor);
       CGContextSetStrokeColorWithColor(context,aColor);

       float w = item.size.width;
       double r = w / 2;
       double theta = 2 * M_PI * (2.0 / 5.0); // 144 degrees

       CGContextMoveToPoint(context,r);

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

       CGContextClosePath(context);
       CGContextFillPath(context);
    }

上面的代码画出了一个完美的明星,但是是1.反复显示2.是黑色的,没有边框.我想要的是在同一条线上画出许多星星,并给予风格.我明白我实际上在同一个位置上绘制了5次相同的路径,而且我以某种方式垂直翻转上下文,但经过几次测试,我放弃了! (我缺乏必要的数学和几何技能:P)…你能帮助我吗?

更新:

好的,感谢CocoaFu,这是我的重构和工作绘图实用程序:

- (void)drawStars:(NSUInteger)count inContext:(CGContextRef)context;
{
    // constants
    const float w = self.itemSize.width;
    const float r = w/2;
    const double theta = 2 * M_PI * (2.0 / 5.0);
    const float flip = -1.0f; // flip vertically (default star representation)

    // drawing center for the star
    float xCenter = r;

    for (NSUInteger i=0; i<count; i++) 
    {
        // get star style based on the index
        CGContextSetFillColorWithColor(context,[self fillColorForItemAtIndex:i]);
        CGContextSetStrokeColorWithColor(context,[self strokeColorForItemAtIndex:i]);

        // update position
        CGContextMoveToPoint(context,xCenter,r * flip + r);

        // draw the necessary star lines
        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 + r);
        }

        // update horizontal center for the next star
        xCenter += w + self.itemMargin; 

        // draw current star
        CGContextClosePath(context);
        CGContextFillPath(context);
        CGContextStrokePath(context);
    }
}

解决方法

这里是一个水平线上绘制3颗星的代码,它不漂亮,但可能有帮助:
-(void)drawRect:(CGRect)rect
{
    int aSize = 100.0;
    const CGFloat 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;

    for (NSUInteger i=0; i<3; i++) 
    {
        CGContextSetFillColorWithColor(context,aColor);
        CGContextSetStrokeColorWithColor(context,aColor);

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

        CGContextMoveToPoint(context,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);
        }
        xCenter += 150.0;
    }
    CGContextClosePath(context);
    CGContextFillPath(context);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读