iOS – 在 UIKit 视图中使用 AudioKit 的 SwiftUI

huangapple go评论105阅读模式
英文:

iOS - SwiftUI inside a UIKit View using AudioKit

问题

抱歉,以下是您提供的文本的翻译:

" I'm fairly new to Swift and MVVM, so my apologies if I misunderstand some of the architecture here and say something completely wrong.
我对Swift和MVVM相对陌生,如果我对这里的一些架构有所误解并说了一些完全错误的话,请原谅。

I have an app written in Swift using UIKit with a main view controller that generates some sounds using AudioKit. I'd like to add a visualizer to that view, and it seems appropriate to use the AudioKitUI library to generate a visualization from the audio that my app is generating while in that storyboard view.
我有一个使用Swift和UIKit编写的应用程序,其中有一个主视图控制器,使用AudioKit生成一些声音。我想在该视图中添加一个可视化效果,似乎可以使用AudioKitUI库从我的应用程序生成的音频中生成可视化效果。

Most of the AudioKitUI stuff seems to be in SwiftUI on the backend. I've looked at using a UIHostingController but I get an error that the initializer is inaccessible due to internal protection level. If I copy/paste the SpectrogramView.swift from the AudioKitUI GitHub into a swift file, I am able to generate a preview successfully.
大部分的AudioKitUI内容似乎都在后端使用SwiftUI。我尝试使用UIHostingController,但遇到了初始化器不可访问的错误,因为它受到内部保护级别的限制。如果我将AudioKitUI GitHub中的SpectrogramView.swift文件复制/粘贴到一个Swift文件中,我可以成功生成预览。

I tried to put it into a UIHostingController inside a Container View, but I didn't exactly see how I should have the spectrogram read the audio node in real time if it's not in that view. So it put the UIHostingController inside my main view controller, added it as a child view, and added it as a subview to a blank UIView.
我尝试将其放入一个Container View中的UIHostingController,但我没有确切地看到如何使光谱图实时读取音频节点,因为它不在该视图中。所以我将UIHostingController放在我的主视图控制器中,将其添加为子视图,并将其添加为一个空白的UIView的子视图。

I'm not sure how to get this SwiftUI Visualization generator to work with UIKit, let alone inside one of my ViewController's sub views. Any tips or advice would be greatly appreciated.
我不确定如何使这个SwiftUI可视化生成器与UIKit一起工作,更不用说放在我的一个ViewController的子视图中。任何提示或建议将不胜感激。

This was my most recent attempt:
这是我最近的尝试:
@IBOutlet var blankView: UIView!

override func viewDidLoad() {
super.viewDidLoad()
let spectroView = UIHostingController(rootView: SpectrogramView(node: sound.mixer))
addChild(spectroView)
blankView.addSubview(spectroView.view)
}"

英文:

I'm fairly new to Swift and MVVM, so my apologies if I misunderstand some of the architecture here and say something completely wrong.

I have an app written in Swift using UIKit with a main view controller that generates some sounds using AudioKit. I'd like to add a visualizer to that view, and it seems appropriate to use the AudioKitUI library to generate a visualization from the audio that my app is generating while in that storyboard view.

Most of the AudioKitUI stuff seems to be in SwiftUI on the backend. I've looked at using a UIHostingController but I get an error that the initializer is inaccessible due to internal protection level. If I copy/paste the SpectrogramView.swift from the AudioKitUI GitHub into a swift file, I am able to generate a preview successfully.

iOS – 在 UIKit 视图中使用 AudioKit 的 SwiftUI

I tried to put it into a UIHostingController inside a Container View, but I didn't exactly see how I should have the spectrogram read the audio node in real time if it's not in that view. So it put the UIHostingController inside my main view controller, added it as a child view, and added it as a subview to a blank UIView.

I'm not sure how to get this SwiftUI Visualization generator to work with UIKit, let alone inside one of my ViewController's sub views. Any tips or advice would be greatly appreciated.

This was my most recent attempt:

@IBOutlet var blankView: UIView!


override func viewDidLoad() {
    super.viewDidLoad()
    let spectroView = UIHostingController(rootView: SpectrogramView(node: sound.mixer))
    addChild(spectroView)
    blankView.addSubview(spectroView.view)
    }

答案1

得分: 0

似乎我的原始帖子中的代码片段并不远离正确。以下是使AudioKitUI Spectrogram View在UIView中工作的完整部分。

@IBOutlet weak var blankView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    // 在加载视图后执行任何额外的设置。
    // 将传输控件启用为按钮
    let spectroView = UIHostingController(rootView: SpectrogramView(node: sound.mixer))
    addChild(spectroView)
    spectroView.view.frame = blankView.bounds
    blankView.addSubview(spectroView.view)
    spectroView.didMove(toParent: self)
}

现在,频谱图视图成功地出现在我的主视图控制器的子视图中。

英文:

It seems my code snippet in the original post wasn't far off. Here is the completed section to get the AudioKitUI Spectrogram View to work in a UIView.

 @IBOutlet weak var blankView: UIView!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    //enable transport controls as buttons
    let spectroView = UIHostingController(rootView: SpectrogramView(node: sound.mixer))
    addChild(spectroView)
    spectroView.view.frame = blankView.bounds
    blankView.addSubview(spectroView.view)
    spectroView.didMove(toParent: self)
    }

The spectrogram view now successfully appears in a subview of my main view controller.
iOS – 在 UIKit 视图中使用 AudioKit 的 SwiftUI

huangapple
  • 本文由 发表于 2023年3月10日 01:31:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75688114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定