,您可以拨打UIBezierPath
与2 Bézier curves相当准确地近似正弦波:
/// Convert degrees to radians
func degreeToRadian(_ angle: CGFloat) -> CGFloat {
return angle * CGFloat.pi/180.0
}
let path = sinePath(size: CGSize(width: 200.0, height: 100.0))
path.apply(CGAffineTransform(rotationAngle: degreeToRadian(20)))
我得到了baseX
常数:
/// Make a path that approximates a peak-to-peak sine wave
func sinePath(size: CGSize) -> UIBezierPath {
let path = UIBezierPath()
// path starts at 0,0
path.move(to: CGPoint.zero)
let halfWidth = size.width/2.0
// base x-values for control points
// baseX approximates the best fit Bézier curve for a sine wave
let baseX: = CGFloat(0.3642124232)
let firstX = halfWidth * baseX
let secondX = halfWidth * (1.0 - baseX)
// curve from start peak to trough
path.addCurve(to: CGPoint(x: halfWidth, y: size.height),
controlPoint1: CGPoint(x: firstX , y: 0),
controlPoint2: CGPoint(x: secondX, y: size.height))
// curve from trough to end peak
path.addCurve(to: CGPoint(x: size.width, y:0),
controlPoint1: CGPoint(x: size.width - secondX, y: size.height),
controlPoint2: CGPoint(x: size.width - firstX , y: 0))
return path
}
然后你可以用一个CGAffineTransform
像这样旋转它从这个问题:
How to approximate a half-cosine curve with bezier paths in SVG?
您需要包含更多信息,您如何使用路径?作为SKShapeNode?只需旋转节点 – Knight0fDragon