gpt4 book ai didi

swift - 在 UIBezierPath 中的 View 之间设置相等间距的问题

转载 作者:行者123 更新时间:2023-11-28 15:18:50 24 4
gpt4 key购买 nike

这是我的代码:

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let amountOfViews = 3
let difference: CGFloat = 360 / CGFloat(amountOfViews)
for i in 0...amountOfViews - 1{
print((CGFloat(i) * difference))
let view2 = UIView()
view2.translatesAutoresizingMaskIntoConstraints = false
view2.backgroundColor = .red
self.view.addSubview(view2)
NSLayoutConstraint(item: view2, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: view2, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: view2, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 0.09, constant: 0).isActive = true
NSLayoutConstraint(item: view2, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 0.35, constant: 0).isActive = true
let orbit = CAKeyframeAnimation(keyPath: "position")
let circlePath = UIBezierPath(arcCenter: self.view.frame.origin, radius: CGFloat(self.view.frame.height * 0.3), startAngle: CGFloat(i) * difference, endAngle: CGFloat.pi * 2 + CGFloat(i) * difference, clockwise: true)
orbit.path = circlePath.cgPath
orbit.duration = 10
orbit.isAdditive = true
orbit.repeatCount = Float.greatestFiniteMagnitude
orbit.calculationMode = kCAAnimationPaced
orbit.rotationMode = kCAAnimationRotateAuto
view2.layer.add(orbit, forKey: "orbit")
}
}

这是结果:

enter image description here

这是打印品:

0.0
120.0
240.0

对我来说,观点之间的差异应该是正确的,但事实并非如此。 View 相互接触,而它们之间的间距应该相等。将 amountOfViews 设置为 4 时,结果如下:

enter image description here

这看起来也很奇怪。我怎样才能让它发挥作用?

最佳答案

这是度数与弧度的问题 UIBezierPath 需要以弧度为单位的角度值。您的 i 变量以度为单位。

let radians = (CGFloat(i) * difference) / 180 * CGFloat.pi
let circlePath = UIBezierPath(arcCenter: self.view.frame.origin, radius: CGFloat(self.view.frame.height * 0.3), startAngle: radians, endAngle: CGFloat.pi * 2 + radians, clockwise: true)

关于swift - 在 UIBezierPath 中的 View 之间设置相等间距的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46381509/

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