1
  • Code is working want to convert into Pure SwiftUI code. without UIViewRepresentable, UIHostingController please let me know how can I achieve. Thanks *

struct PathAnimatingView: UIViewRepresentable where Content: View {

var path: Path
let content: () -> Content

func makeUIView(context: UIViewRepresentableContext<PathAnimatingView>) -> UIView {

    let view = UIView(frame: .zero)

    let animation = CAKeyframeAnimation(keyPath: #keyPath(CALayer.position))
    animation.duration = CFTimeInterval(10)
    animation.repeatCount = 10
    animation.path = path.cgPath
    animation.keyTimes = [0.0,0.5,1.0]
    animation.isRemovedOnCompletion = true
    animation.fillMode = .forwards
    animation.timingFunction = CAMediaTimingFunction(name: .linear)

    let sub = UIHostingController(rootView: content())
    sub.view.translatesAutoresizingMaskIntoConstraints = false

    view.addSubview(sub.view)
    sub.view.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    sub.view.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

    view.layer.add(animation, forKey: "snake")
    return view
}

func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PathAnimatingView>) {
    print(uiView)
}

}

0 Answers0