iPhone iOS以编程方式生成星形、太阳余辉或多边形UIBezierPath
我正在寻找一种使用UIBezierPath以编程方式创建星星、太阳爆发和其他“尖刺”效果的方法iPhone iOS以编程方式生成星形、太阳余辉或多边形UIBezierPath,iphone,objective-c,quartz-graphics,graphic,uibezierpath,Iphone,Objective C,Quartz Graphics,Graphic,Uibezierpath,我正在寻找一种使用UIBezierPath以编程方式创建星星、太阳爆发和其他“尖刺”效果的方法 UIBezierPath *sunbeamsPath = [UIBezierPath bezierPath]; [sunbeamsPath moveToPoint: CGPointMake(x, y)]; 是否有任何算法可以以编程方式为类似太阳爆发的形状生成点,而不会出现路径重叠 我还对不规则形状的太阳爆发感兴趣,如下图所示: 我可以想象这样的算法将获取一定数量的光线,然后将圆大致划分为若干段,
UIBezierPath *sunbeamsPath = [UIBezierPath bezierPath];
[sunbeamsPath moveToPoint: CGPointMake(x, y)];
是否有任何算法可以以编程方式为类似太阳爆发的形状生成点,而不会出现路径重叠
我还对不规则形状的太阳爆发感兴趣,如下图所示:
我可以想象这样的算法将获取一定数量的光线,然后将圆大致划分为若干段,并在顺时针方向为这些段生成点。像我描述的那样的算法已经存在,还是我必须自己写一个
谢谢大家! 我不知道创建这些的算法,但我有一些建议-创建贝塞尔路径,使(0,0)成为太阳暴流的中心,然后定义需要多少点才能绘制一束太阳暴流向上返回(0,0) 然后,对于任意数量的光束,执行循环:将旋转变换(2π/光束数)应用于日光点(CGPointApplyTransform),并将其添加到路径中 完成后,可以平移和缩放图形的路径
我最近用了一个类似的过程来画星形多边形,非常简单。这一想法值得称赞 我知道这一点,但我自己对这个问题的第一部分很好奇,在jrturton的帖子中,我创建了一个自定义UIView,从视图的中心生成UIBezierPath。甚至还设置了它旋转的动画以获得额外积分。结果如下: 我使用的代码如下:
- (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];
-(void)drawRect:(CGRect)rect{
CGFloat半径=rect.size.width/2.0f;
[self.fillColor setFill];
[self.strokeColor setStroke];
UIBezierPath*bezierPath=[UIBezierPath bezierPath];
CGPoint centerPoint=CGPointMake(矩形原点.x+半径,矩形原点.y+半径);
CGPoint thisPoint=CGPointMake(centerPoint.x+半径,centerPoint.y);
[bezierPath移动点:中心点];
cG角度=0.0f;
CGF角度=360.0f/自聚焦光束/2.0f;
对于(int i=0;i
}
您可以在此处下载示例项目:
此版本的Swift版本
import UIKit
extension Int {
var degreesToRadians: Double { return Double(self) * .pi / 180 }
var radiansToDegrees: Double { return Double(self) * 180 / .pi }
}
extension FloatingPoint {
var degreesToRadians: Self { return self * .pi / 180 }
var radiansToDegrees: Self { return self * 180 / .pi }
}
class SunBurstView: UIView {
override func draw(_ rect: CGRect) {
let radius: CGFloat = rect.size.width / 2.0
UIColor.red.setFill()
UIColor.blue.setStroke()
let bezierPath = UIBezierPath()
let centerPoint = CGPoint(x: rect.origin.x + radius, y: rect.origin.y + radius)
var thisPoint = CGPoint(x: centerPoint.x + radius, y: centerPoint.y)
bezierPath.move(to: centerPoint)
var thisAngle: CGFloat = 0.0
let sliceDegrees: CGFloat = 360.0 / self.beams / 2.0
for _ in 0..<self.beams {
let x = radius * CGFloat(cosf(Float((thisAngle + sliceDegrees).degreesToRadians))) + centerPoint.x
let y = radius * CGFloat(sinf(Float((thisAngle + sliceDegrees).degreesToRadians))) + centerPoint.y
thisPoint = CGPoint(x: x, y: y)
bezierPath.addLine(to: thisPoint)
thisAngle += sliceDegrees
let x2 = radius * CGFloat(cosf(Float((thisAngle + sliceDegrees).degreesToRadians))) + centerPoint.x
let y2 = radius * CGFloat(sinf(Float((thisAngle + sliceDegrees).degreesToRadians))) + centerPoint.y
thisPoint = CGPoint(x: x2, y: y2)
bezierPath.addLine(to: thisPoint)
bezierPath.addLine(to: centerPoint)
thisAngle += sliceDegrees
}
bezierPath.close()
bezierPath.lineWidth = 1
bezierPath.fill()
bezierPath.stroke()
}
}
导入UIKit
扩展整数{
变度弧度:双{返回双(自)*.pi/180}
变量:双{返回双(自)*180/.pi}
}
扩展浮点数{
var degreestoradis:Self{return Self*.pi/180}
变量:Self{return Self*180/.pi}
}
SunBurstView类:UIView{
重写函数绘图(rect:CGRect){
let半径:CGFloat=rect.size.width/2.0
UIColor.red.setFill()
UIColor.blue.setStroke()
设bezierPath=UIBezierPath()
设中心点=CGPoint(x:rect.origin.x+半径,y:rect.origin.y+半径)
var thisPoint=CGPoint(x:centerPoint.x+半径,y:centerPoint.y)
bezierPath.移动(到:中心点)
var thisAngle:CGFloat=0.0
让切片度为:CGFloat=360.0/self.beams/2.0
对于uu in 0..我注意到Swift版本没有为我编译,也没有占用足够的屏幕,因此这里是针对矩形视图调整的Swift 4中Reinier的答案
extension Int {
var degreesToRadians: Double { return Double(self) * .pi / 180 }
var radiansToDegrees: Double { return Double(self) * 180 / .pi }
}
extension FloatingPoint {
var degreesToRadians: Self { return self * .pi / 180 }
var radiansToDegrees: Self { return self * 180 / .pi }
}
class SunBurstView: UIView {
var beams: CGFloat = 20
override func draw(_ rect: CGRect) {
self.clipsToBounds = false
self.layer.masksToBounds = false
let radius: CGFloat = rect.size.width * 1.5
UIColor.orange.withAlphaComponent(0.3).setFill()
UIColor.clear.setStroke()
let bezierPath = UIBezierPath()
let centerPoint = CGPoint(x: rect.origin.x + (radius / 3), y: rect.origin.y + (radius / 1.5))
var thisPoint = CGPoint(x: centerPoint.x + radius, y: centerPoint.y)
bezierPath.move(to: centerPoint)
var thisAngle: CGFloat = 0.0
let sliceDegrees: CGFloat = 360.0 / self.beams / 2.0
for _ in 0...Int(beams) {
let x = radius * CGFloat(cosf(Float((thisAngle + sliceDegrees).degreesToRadians))) + centerPoint.x
let y = radius * CGFloat(sinf(Float((thisAngle + sliceDegrees).degreesToRadians))) + centerPoint.y
thisPoint = CGPoint(x: x, y: y)
bezierPath.addLine(to: thisPoint)
thisAngle += sliceDegrees
let x2 = radius * CGFloat(cosf(Float((thisAngle + sliceDegrees).degreesToRadians))) + centerPoint.x
let y2 = radius * CGFloat(sinf(Float((thisAngle + sliceDegrees).degreesToRadians))) + centerPoint.y
thisPoint = CGPoint(x: x2, y: y2)
bezierPath.addLine(to: thisPoint)
bezierPath.addLine(to: centerPoint)
thisAngle += sliceDegrees
}
bezierPath.close()
bezierPath.lineWidth = 1
bezierPath.fill()
bezierPath.stroke()
}
}