iPhone iOS 以编程方式生成星形、旭日形或多边形 UIBezierPath

标签 iphone objective-c quartz-graphics graphic uibezierpath

我正在寻找一种使用 UIBezierPath 以编程方式创建星星、旭日和其他“尖刺”效果的方法。

Starburst image

UIBezierPath *sunbeamsPath = [UIBezierPath bezierPath];
[sunbeamsPath moveToPoint: CGPointMake(x, y)];

是否有任何算法可以以编程方式为类似朝阳的形状生成点,而路径不会重叠?

我也对像下面这样的不规则形状的旭日形感兴趣:

irregular sunburst

我想这样的算法会采用一定数量的光线,然后将圆粗略地分成若干段,并按顺时针方向为这些段生成点。像我所描述的那样的算法是否已经存在,或者我必须自己编写一个吗?

谢谢!

最佳答案

我知道这个很老,但我自己对这个问题的第一部分很好奇,并且根据 jrturton 的帖子,我创建了一个自定义 UIView,它从 View 中心生成一个 UIBezierPath。甚至动画它旋转以获得奖励积分。这是结果:

enter image description here

我使用的代码在这里:

- (void)drawRect:(CGRect)rect {
CGFloat radius = rect.size.width/2.0f;

[self.fillColor setFill];
[self.strokeColor setStroke];

UIBezierPath *bezierPath = [UIBezierPath bezierPath];
CGPoint centerPoint = CGPointMake(rect.origin.x + radius, rect.origin.y + radius);

CGPoint thisPoint = CGPointMake(centerPoint.x + radius, centerPoint.y);
[bezierPath moveToPoint:centerPoint];

CGFloat thisAngle = 0.0f;
CGFloat sliceDegrees = 360.0f / self.beams / 2.0f;

for (int i = 0; i < self.beams; i++) {

    CGFloat x = radius * cosf(DEGREES_TO_RADIANS(thisAngle + sliceDegrees)) + centerPoint.x;
    CGFloat y = radius * sinf(DEGREES_TO_RADIANS(thisAngle + sliceDegrees)) + centerPoint.y;
    thisPoint = CGPointMake(x, y);
    [bezierPath addLineToPoint:thisPoint];
    thisAngle += sliceDegrees;

    CGFloat x2 = radius * cosf(DEGREES_TO_RADIANS(thisAngle + sliceDegrees)) + centerPoint.x;
    CGFloat y2 = radius * sinf(DEGREES_TO_RADIANS(thisAngle + sliceDegrees)) + centerPoint.y;
    thisPoint = CGPointMake(x2, y2);
    [bezierPath addLineToPoint:thisPoint];
    [bezierPath addLineToPoint:centerPoint];
    thisAngle += sliceDegrees;

}

[bezierPath closePath];

bezierPath.lineWidth = 1;
[bezierPath fill];
[bezierPath stroke];

您可以在此处下载示例项目:

https://github.com/meekapps/Sunburst

关于iPhone iOS 以编程方式生成星形、旭日形或多边形 UIBezierPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10399958/

相关文章:

ios - 对 NSDictionary 进行降序排序。如何使用 `compare:options:` 选择器发送选项?

ios uiviewcontroller继承抛出重复符号错误

iphone - 如何在项目中添加自定义框架?

ios - 如何设置具有延迟的显式 CA 动画的最终值

ios - CGGradient 在 iPad 1 和 2 上显示不正确

iphone - 如何调整 Quartz 2D 上下文以适应 Retina 显示屏?

iPhone如何验证字符串只是Objective-c中的图像名称或图像url?

iphone - Facebook Connect SSO 不适用于某些 iPhone

iphone - 如果日期的年份已经过去,则将日期的年份增加一

iphone - UIWebView 的内容颜色意外改变