gpt4 book ai didi

swift - 如何在swift中将圆圈变成一条线?

转载 作者:行者123 更新时间:2023-11-28 06:40:26 25 4
gpt4 key购买 nike

所以我有一个带有特定图案的圆圈。我想把这个圆圈变成一条线。我有什么想法可以做到这一点吗?

我确实看过在圆上制作 UIBezierPath,然后在没有任何帮助的情况下将其转换为一条线。我还在 opencv ( fast Cartesian to Polar to Cartesian in Python ) 中看到了执行此操作的链接,但更喜欢在 native swift 中执行此操作。

enter image description here

最佳答案

如果您不关心转换的性质,最简单的方法是执行 CABasicAnimation,为 CAShapeLayerpath 设置动画> 从一条路径到另一条路径。要使用 CAShapeLayer 实现图案化路径,实际上是有两个重叠的 CAShapeLayer 对象,一个在一个没有破折号图案的上面。

let fromPath = UIBezierPath(arcCenter: view.center, radius: 100, startAngle: 0, endAngle: CGFloat(M_PI) * 2.0, clockwise: true)
let toPath = UIBezierPath()
toPath.moveToPoint(CGPoint(x: view.frame.size.width / 2.0 - CGFloat(M_PI) * 100.0, y:view.center.y))
toPath.addLineToPoint(CGPoint(x: view.frame.size.width / 2.0 + CGFloat(M_PI) * 100.0, y: view.center.y))

let shapeLayer = CAShapeLayer()
shapeLayer.path = fromPath.CGPath
shapeLayer.lineWidth = 5
shapeLayer.strokeColor = UIColor.redColor().CGColor
shapeLayer.fillColor = UIColor.clearColor().CGColor

let shapeLayer2 = CAShapeLayer()
shapeLayer2.path = fromPath.CGPath
shapeLayer2.lineWidth = 5
shapeLayer2.strokeColor = UIColor.blackColor().CGColor
shapeLayer2.fillColor = UIColor.clearColor().CGColor
shapeLayer2.lineDashPattern = [100,50]

view.layer.addSublayer(shapeLayer)
view.layer.addSublayer(shapeLayer2)

shapeLayer.path = toPath.CGPath
shapeLayer2.path = toPath.CGPath

CATransaction.begin()
CATransaction.setAnimationDuration(5)

let animation = CABasicAnimation(keyPath: "path")
animation.fromValue = fromPath.CGPath
animation.toValue = toPath.CGPath
shapeLayer.addAnimation(animation, forKey: nil)

let animation2 = CABasicAnimation(keyPath: "path")
animation2.fromValue = fromPath.CGPath
animation2.toValue = toPath.CGPath
shapeLayer2.addAnimation(animation, forKey: nil)

CATransaction.commit()

产量:

enter image description here

如果你想更好地控制过渡的性质,那么你必须求助于更多的手动技术,例如手动调整路径的 CADisplayLink。但这更复杂。

关于swift - 如何在swift中将圆圈变成一条线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38116247/

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