Water animation in swift iOS with xcode 16

Поділитися
Вставка
  • Опубліковано 7 лют 2025
  • Water animation like wave in iOS swift. in code we design the custom class for water animation.
    using following animation code we animate the water wave.
    let w = bounds.size.width
    let h = bounds.size.height
    let bezier = UIBezierPath()
    let path = CGMutablePath()
    let startoffsetY = waveHeight * CGFloat(sinf(Float(offset*twon/w)))
    var origiOffsetY: CGFloat = 0.0
    path.move(to: CGPoint(x: 0.0, y: startoffsetY),transform: .identity)
    bezier.move(to: CGPoint(x: 0.0, y: startoffsetY))
    for i in stride(from: 0.0, to: w*1000, by: 1){
    origiOffsetY = waveHeight * CGFloat(sinf(Float(twon / w*i + offset * twon/w)))
    bezier.addLine(to: CGPoint(x: i, y: origiOffsetY))
    }
    bezier.addLine(to: CGPoint(x: w*1000, y: origiOffsetY))
    bezier.addLine(to: CGPoint(x: w*1000, y: h))
    bezier.addLine(to: CGPoint(x: 0.0, y: h))
    bezier.addLine(to: CGPoint(x: 0.0, y: startoffsetY))
    bezier.close()
    let anim = CABasicAnimation(keyPath: "transform.translation.x")
    anim.duration = 2.0
    anim.fromValue = -w * 0.5
    anim.toValue = -w - w*0.5
    anim.repeatCount = .infinity
    anim.isRemovedOnCompletion = false

    firstlayer.fillColor = firstColor.cgColor
    firstlayer.path = bezier.cgPath
    firstlayer.add(anim, forKey: nil)

    if !showSingleWave{
    let bezier = UIBezierPath()
    let startoffsetY = waveHeight * CGFloat(sinf(Float(offset*twon/w)))
    var origiOffsetY: CGFloat = 0.0
    bezier.move(to: CGPoint(x: 0.0, y: startoffsetY))
    for i in stride(from: 0.0, to: w*1000, by: 1){
    origiOffsetY = waveHeight*CGFloat(cosf(Float(twon/w*i + offset*twon/w)))
    bezier.addLine(to: CGPoint(x: i, y: origiOffsetY))
    }
    bezier.addLine(to: CGPoint(x: w*1000, y: origiOffsetY))
    bezier.addLine(to: CGPoint(x: w*1000, y: h))
    bezier.addLine(to: CGPoint(x: 0.0, y: h))
    bezier.addLine(to: CGPoint(x: 0.0, y: startoffsetY))
    bezier.close()

    secondLayer.fillColor = secondColor.cgColor
    secondLayer.path = bezier.cgPath
    secondLayer.add(anim, forKey: nil)

КОМЕНТАРІ • 1