gpt4 book ai didi

ios - 如何找到快速绘制的曲线的 beizerpath 的端点?

转载 作者:行者123 更新时间:2023-11-30 12:00:40 25 4
gpt4 key购买 nike

我画了一 strip 有起始角度和结束角度的贝泽路径,创建了一个完整的圆

circle = UIView(frame: CGRect(x: 100, y: 100, width: 200, height:200))
circle.layoutIfNeeded()
self.view.addSubview(circle)
let centerPoint = CGPoint (x: circle.bounds.width / 2, y: circle.bounds.width / 2)
let circleRadius : CGFloat = circle.bounds.width / 2 * 0.83
circlePath = UIBezierPath(arcCenter: centerPoint, radius: circleRadius, startAngle: CGFloat(-0.5 * M_PI), endAngle: CGFloat(1.5 * M_PI), clockwise: true)

之后,我在该圆上绘制一条曲线,并且没有将其填充到圆上。

progressCircle = CAShapeLayer ()
progressCircle.path = circlePath?.cgPath
progressCircle.strokeColor = UIColor.red.cgColor
progressCircle.fillColor = UIColor.clear.cgColor
progressCircle.lineWidth = 4.0
progressCircle.strokeStart = 0
progressCircle.strokeEnd = 0.7
circle.layer.addSublayer(progressCircle)

之后我想在曲线的末端添加图像,所以我创建了另一个比泽路径。但这里的问题是它应该作为曲线末端结束。所以我无法找到结束角度基于 CASHAPELAYER 的 endStroke 的 beizer 路径。请告诉我如何根据曲线终点找到新贝泽路径的结束角度。

let centerPoint = CGPoint (x: circle.bounds.width / 2, y: circle.bounds.width / 2)
let circleRadius : CGFloat = circle.bounds.width / 2 * 0.83
let arcStartAngle: Double = 0.0


let rotationDiff = 360 - abs((0.0 - 270))
let startAngle: CGFloat = -1.57
let endAngle: CGFloat = CGFloat(Double.pi * 0.7)
let bpath = UIBezierPath(arcCenter: centerPoint, radius: circleRadius, startAngle: startAngle, endAngle: endAngle, clockwise: true)

enter image description here

最佳答案

class ViewController: UIViewController {

var array = [[CGFloat:CGFloat]]()
var midX : CGFloat = CGFloat()
var midY : CGFloat = CGFloat()
let radius : CGFloat = 100.0

override func viewDidLoad() {
super.viewDidLoad()

midX = self.view.frame.midX
midY = self.view.frame.midY

let path = UIBezierPath()
path.addArc(withCenter: CGPoint.init(x: midX, y: midY), radius: radius, startAngle: 0, endAngle:2 * .pi, clockwise: true)
let shape = CAShapeLayer()
shape.path = path.cgPath
shape.lineWidth = 5.0
shape.fillColor = UIColor.clear.cgColor
shape.strokeColor = UIColor.lightGray.cgColor
self.view.layer.addSublayer(shape)


var temp090x = [CGFloat:CGFloat]()
var temp90180x = [CGFloat:CGFloat]()
var temp180270x = [CGFloat:CGFloat]()
var temp270360x = [CGFloat:CGFloat]()



for i in 1...360 {
let path3 = UIBezierPath()
path3.move(to: CGPoint.init(x: midX, y: midY))
path3.addLine(to:CGPoint.init(x: midX + radius * sin(CGFloat(i)), y:midY + radius*cos(CGFloat(i))))
//design path in layer
if midX + radius * sin(CGFloat(i)) > midX && midY + radius*cos(CGFloat(i)) < midY {
temp090x[midX + radius * sin(CGFloat(i))] = midY + radius*cos(CGFloat(i))
}else if midX + radius * sin(CGFloat(i)) > midX && midY + radius*cos(CGFloat(i)) > midY {
temp90180x[midX + radius * sin(CGFloat(i))] = midY + radius*cos(CGFloat(i))
}else if midX + radius * sin(CGFloat(i)) < midX && midY + radius*cos(CGFloat(i)) > midY {
temp180270x[midX + radius * sin(CGFloat(i))] = midY + radius*cos(CGFloat(i))
}else if midX + radius * sin(CGFloat(i)) < midX && midY + radius*cos(CGFloat(i)) < midY {
temp270360x[midX + radius * sin(CGFloat(i))] = midY + radius*cos(CGFloat(i))
}
let shapeLayer3 = CAShapeLayer()
shapeLayer3.path = path3.cgPath
shapeLayer3.strokeColor = UIColor.clear.cgColor
shapeLayer3.lineWidth = 1.0
self.view.layer.addSublayer(shapeLayer3)
}

let tempSorted090x = BubbleAsceSort(array: temp090x)
let tempSorted90180x = BubbleDescSort(array: temp90180x)
let tempSorted180270x = BubbleDescSort(array: temp180270x)
let tempSorted270360x = BubbleAsceSort(array: temp270360x)

for item in tempSorted090x {
array.append(item)
}
for item in tempSorted90180x {
array.append(item)
}
for item in tempSorted180270x {
array.append(item)
}
for item in tempSorted270360x {
array.append(item)
}

let lbl = UILabel()
lbl.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30)
lbl.backgroundColor = UIColor.black
lbl.layer.cornerRadius = 15


for i in 1...180 {
let xpos = Array(array[i-1].keys)[0]
let ypos = Array(array[i-1].values)[0]

let xpos1 = Array(array[i].keys)[0]
let ypos1 = Array(array[i].values)[0]

let path3 = UIBezierPath()
path3.move(to: CGPoint.init(x: xpos, y: ypos))
path3.addLine(to:CGPoint.init(x: xpos1, y:ypos1))
let shapeLayer3 = CAShapeLayer()
shapeLayer3.path = path3.cgPath
shapeLayer3.strokeColor = UIColor.red.cgColor
shapeLayer3.lineWidth = 5.0
self.view.layer.addSublayer(shapeLayer3)


}
lbl.center = CGPoint.init(x: Array(array[180].keys)[0], y: Array(array[180].values)[0])
self.view.addSubview(lbl)
}

func BubbleDescSort(array : [CGFloat:CGFloat]) -> [[CGFloat:CGFloat]] {
var sortedArray = Array(array.keys)
var sortedvalueArray = Array(array.values)
var sortedAboveIndex = sortedArray.count-1 // Assume all values are not in order
repeat {
var lastSwapIndex = 0

for i in 1...sortedAboveIndex{
if (sortedArray[i] as AnyObject) as! CGFloat > (sortedArray[i - 1] as AnyObject) as! CGFloat {
sortedArray.swapAt(i, i-1)
sortedvalueArray.swapAt(i, i-1)
lastSwapIndex = i
}
}
sortedAboveIndex = lastSwapIndex

} while (sortedAboveIndex != 0)
var index = 0
var arr = [[CGFloat:CGFloat]]()
for item in sortedArray {
var dic = [CGFloat:CGFloat]()
dic[item] = sortedvalueArray[index]
arr.append(dic)
index += 1
}
return arr
}

func BubbleAsceSort(array : [CGFloat:CGFloat]) -> [[CGFloat:CGFloat]] {
var sortedArray = Array(array.keys)
var sortedvalueArray = Array(array.values)
var sortedAboveIndex = sortedArray.count-1 // Assume all values are not in order
repeat {
var lastSwapIndex = 0

for i in 1...sortedAboveIndex{
if (sortedArray[i - 1] as AnyObject) as! CGFloat > (sortedArray[i] as AnyObject) as! CGFloat {
sortedArray.swapAt(i, i-1)
sortedvalueArray.swapAt(i, i-1)
lastSwapIndex = i
}
}
sortedAboveIndex = lastSwapIndex

} while (sortedAboveIndex != 0)
var index = 0
var arr = [[CGFloat:CGFloat]]()
for item in sortedArray {
var dic = [CGFloat:CGFloat]()
dic[item] = sortedvalueArray[index]
arr.append(dic)
index += 1
}
return arr
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

在此代码中,您需要设置基于 360 度的进度百分比。在for循环和Round lbl的上限中心点x和y pos。这里只需要将 180 替换为你的进度百分比

关于ios - 如何找到快速绘制的曲线的 beizerpath 的端点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47278875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com