Swift 贝塞尔曲线动画案例

2023-03-22  本文已影响0人  CYC666

        // 创建CAShapeLayer对象,并设置其路径
        let shapeLayer = CAShapeLayer()
        shapeLayer.strokeColor = UIColor.blue.cgColor
        shapeLayer.lineWidth = 2.0
        shapeLayer.fillColor = UIColor.clear.cgColor
        kPManager.window.layer.addSublayer(shapeLayer)

        // 创建CABasicAnimation对象,并设置其路径属性、持续时间等属性
        let fromPath = UIBezierPath(arcCenter: CGPoint(x: 0, y: 300), radius: 10, startAngle: 0, endAngle: CGFloat(Double.pi * 2), clockwise: true)
        let toPath = UIBezierPath(arcCenter: CGPoint(x: 400, y: 300), radius: 30, startAngle: 0, endAngle: CGFloat(Double.pi * 2), clockwise: true)

        let animation = CABasicAnimation(keyPath: "path")
        animation.duration = 2.0
        animation.fromValue = fromPath.cgPath
        animation.toValue = toPath.cgPath
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

        // 将动画添加到CAShapeLayer对象中,并启动动画
        shapeLayer.add(animation, forKey: "pathAnimation")
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 4.0) {
            
            shapeLayer.removeFromSuperlayer()
        }

上一篇 下一篇

猜你喜欢

热点阅读