如何在 Swift 中连续创建 4-5 个涟漪并在延迟一段时间后重复该过程

问题描述 投票:0回答:0

我正在开发一个 iOS 应用程序,我需要在 UIView 上创建涟漪效果。我可以创建涟漪效果,但无法自定义。这就是我想要实现的目标:

这是我当前的输出

我想要实现的是连续创建 4-5 个涟漪,然后在一些延迟之后再产生 4-5 个涟漪并无限重复该过程。但目前我只能产生 1 个波纹(它会无限传播)。

这是我的代码:

func addRipple(rView: UIView) {
    let path = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: rView.bounds.size.width, height: rView.bounds.size.height))

    let shapePosition = CGPoint(x: rView.bounds.size.width / 2.0, y: rView.bounds.size.height / 2.0)
    let rippleShape = CAShapeLayer()
    rippleShape.bounds = CGRect(x: 0, y: 0, width: rView.bounds.size.width, height: rView.bounds.size.height)
    rippleShape.path = path.cgPath
    rippleShape.fillColor = UIColor.clear.cgColor
    rippleShape.strokeColor = UIColor.systemBlue.cgColor
    rippleShape.lineWidth = 1
    rippleShape.position = shapePosition
    rippleShape.opacity = 0
    
    rView.layer.addSublayer(rippleShape)

    let scaleAnim = CABasicAnimation(keyPath: "transform.scale")
    scaleAnim.fromValue = NSValue(caTransform3D: CATransform3DIdentity)
    scaleAnim.toValue = NSValue(caTransform3D: CATransform3DMakeScale(2, 2, 1))

    let opacityAnim = CABasicAnimation(keyPath: "opacity")
    opacityAnim.fromValue = 1
    opacityAnim.toValue = nil

    let animation = CAAnimationGroup()
    animation.animations = [scaleAnim, opacityAnim]
    animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeOut)
    animation.duration = 1
    animation.repeatCount = Float.infinity
    animation.isRemovedOnCompletion = false
    rippleShape.add(animation, forKey: "rippleEffect")
}

我觉得我很接近,但我无法弄清楚。

提前致谢。

ios swift cashapelayer cabasicanimation
© www.soinside.com 2019 - 2024. All rights reserved.