久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 Quartz Core 繪制星星?

How to draw stars using Quartz Core?(如何使用 Quartz Core 繪制星星?)
本文介紹了如何使用 Quartz Core 繪制星星?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試改編 Apple 提供的示例,以便以編程方式在直線上繪制星星,代碼如下:

I'm trying to adapt an example provided by Apple in order to programmatically draw stars in line, the code is the following:

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, 0, 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)...你能幫幫我嗎?

The code above draws a perfect star, but is 1. displayed upside down 2. is black and without border. What I want to achive is to draw many stars on the same line and with the given style. I understand that I'm actually drawing the same path 5 times in the same position and that I have somehow to flip the context vertically, but after several tests I gave up! (I lack the necessary math and geometry skills :P)... could you please help me?

更新:

好的,感謝 CocoaFu,這是我的重構和工作繪圖實用程序:

Ok, thanks to CocoaFu, this is my refactored and working draw utility:

- (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 顆星的代碼,雖然不漂亮,但可能會有所幫助:

Here is code that will draw 3 stars in a horizontal line, it's is not pretty but it may help:

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

這篇關于如何使用 Quartz Core 繪制星星?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to have a circular, center-cropped imageView, without creating a new bitmap?(如何在不創建新位圖的情況下擁有圓形、中心裁剪的 imageView?)
Why does giving addArcWithCenter a startAngle of 0 degrees make it start at 90 degrees?(為什么給 addArcWithCenter 一個 0 度的 startAngle 使它從 90 度開始?)
Android Catch Notes App Like Circle Menu(Android Catch Notes 應用程序,如圓形菜單)
No such acos function exists(不存在這樣的 acos 函數)
Find the CGPoint on a UIView rectangle intersected by a straight line at a given angle from the center point(在與中心點成給定角度的直線相交的 UIView 矩形上找到 CGPoint)
Draw circle of certain radius on map view in Android(在Android的地圖視圖上繪制一定半徑的圓)
主站蜘蛛池模板: 99久久婷婷国产综合精品电影 | 久久y| 在线成人一区 | 中文字幕在线一区 | 日本精品一区 | 欧美成人一区二区三区 | 久久久在线视频 | 国产精品成人一区二区三区 | 亚洲综合99 | 亚洲黄色一级毛片 | 精品国产久 | 日韩精品成人 | 激情黄色在线观看 | 日日爱夜夜操 | 美女三区| 精品1区2区 | 成人性生交大片免费看r链接 | 成人免费福利视频 | 亚洲区一区二 | 神马久久久久久久久久 | 亚洲色图网址 | 91视频进入 | 97久久久久久久久 | 亚洲a毛片 | 久久国产精品视频 | 日本久草| 国产精品国产精品国产专区不蜜 | 手机看片在线播放 | 精品国产乱码久久久久久88av | 亚洲精品专区 | 国产精品久久久一区二区三区 | 精品国模一区二区三区欧美 | 国产91亚洲精品一区二区三区 | 亚洲永久精品国产 | 日本一区二区三区精品视频 | 国产一二三区电影 | avmans最新导航地址 | 粉嫩国产精品一区二区在线观看 | 日本一区二区三区在线观看 | 成人午夜影院 | 999免费观看视频 |