func animation() {
// 第一个layer
bezierPath.removeAllPoints() // 清空上一次的所有点,也就是擦除上一次的波浪线
offset += 2
//var startY = 10.0*sinf(Float(M_PI*Double(offset)/375.0))
print(startY)
bezierPath.move(to: CGPoint(x: 0, y: 240)) // 设置起点
// 终点和control点不用设置,通过下面的正弦函数计算,依靠addLine手动设置
// 通过for生成一系列的point,当然,point.y的值是根据正弦sinf生成的
for i in 0...374 {
let a = 2.5*M_PI*Double(i)/375.0
let b = Double(offset)*M_PI/375.0
let y = 1.1*10.0*sinf(Float(a+b)) + 200
// 将点连线
bezierPath.addLine(to: CGPoint(x: CGFloat(i), y: CGFloat(y)))
}
// 闭环
bezierPath.addLine(to: CGPoint(x: 375, y: 240)) // 设置终点
backg.fillColor = UIColor.lightGray.cgColor
backg.opacity = 0.5
backg.path = bezierPath.cgPath
// 同理第二个layer
bezierPath.removeAllPoints()
offset += 2
bezierPath.move(to: CGPoint(x: 0, y: 240))
for i in 0...374 {
let a = 2.5*M_PI*Double(i)/375.0
let b = Double(3*offset)*M_PI/375.0
let y = 10.0*sinf(Float(a+b+M_PI/4)) + 200
bezierPath.addLine(to: CGPoint(x: CGFloat(i), y: CGFloat(y)))
}
bezierPath.addLine(to: CGPoint(x: 375, y: 240))
backg_.fillColor = UIColor.lightGray.cgColor
backg_.opacity = 0.5
backg_.path = bezierPath.cgPath
}