iPhone iOS以编程方式生成星形,旭日形或多边形UIBezierPath [英] iPhone iOS Generate star, sunburst or polygon UIBezierPath programmatically

查看:264
本文介绍了iPhone iOS以编程方式生成星形,旭日形或多边形UIBezierPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for a way to programmatically create stars, sunburst, and other "spiky" effects using UIBezierPath.

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

是否有任何算法能够以编程方式为形状为sunburst生成点,而不会重叠路径?

Are there any algorithms that can generate points for sunburst like shapes programmatically, without paths overlapping?

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

I'm also interested in an irregular shape sunburst like the one below:

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

I would imagine that such algorithm would take a certain number of rays, then roughly divide the circle in a number of segments and generate points for such segment in a clockwise direction. Does an algorithm like the one I'm describing already exists or will I have to write one by myself?

谢谢!

推荐答案

我知道这个老了,但是我我自己对这个问题的第一部分感到好奇,并且从jrturton的帖子开始,我创建了一个自定义的UIView,它从视图的中心生成一个UIBezierPath。甚至动画它旋转奖励积分。结果如下:

I know this old, but I was curious about the first part of this question myself, and going off jrturton's post, I created a custom UIView that generates a UIBezierPath from center of the view. Even animated it spinning for bonus points. Here is the result:

我使用的代码在这里:

- (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];

}

你可以下载一个示例项目:

And you can download a sample project here:

https://github.com / meekapps / Sunburst

这篇关于iPhone iOS以编程方式生成星形,旭日形或多边形UIBezierPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆