1
  • 代码正在工作想要转换成纯 SwiftUI 代码。如果没有 UIViewRepresentable,UIHostingController 请告诉我如何实现。谢谢 *

结构 PathAnimatingView:UIViewRepresentable 其中内容:查看 {

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)
}

}

4

0 回答 0