微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在尝试调整苹果提供的示例,以便以编程方式绘制星星,代码如下:
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);
}

原文地址:https://www.jb51.cc/c/112161.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐