ios - 从 out-of-viewController 更新振荡器频率

标签 ios swift

在学习 iOS 的过程中,我正在制作一个使用 AudioKit.io 播放音调的应用

当用户点击屏幕时,会绘制一个新的“键”,并根据其 X 位置设置振荡器的频率。

所以现在我希望用户能够在“键”周围拖动并获得振荡器更新的频率。

我想这更像是一个项目组织/逻辑组织问题。

有:

  • ViewController.swift(制作振荡器并绘制按键:didTap、toggleSound)
  • CircularKeyView.swift(圆形键的属性:drawRect、didPan等)

来自 CircularKeyView.swift:

//PAN IT LIKE u FRYIN.
func didPan(panGR: UIPanGestureRecognizer) {

self.superview!.bringSubviewToFront(self)

var translation = panGR.translationInView(self)

self.center.x += translation.x
self.center.y += translation.y


panGR.setTranslation(CGPointZero, inView: self)

//so the coordinates can be updated when there's a pan
self.setNeedsDisplay() //updates the coordinates on a pan event
//updateOscillatorFrequency here?
}

来自 circularKeyView:

    ...
    override func viewDidLoad() {
        super.viewDidLoad()

        let tapGR = UITapGestureRecognizer(target: self, action: "didTap:")
        self.view.addGestureRecognizer(tapGR)


        let mixer = AKMixer(oscillator, osc2)
        AudioKit.output = mixer
        AudioKit.start()
    }

    func didTap(tapGR: UITapGestureRecognizer) {

        let tapPoint = tapGR.locationInView(self.view)

        let newCircularKey = circularKeyView(origin: tapPoint)

        self.view.addSubview(newCircularKey)
        oscillator.frequency = Double(newCircularKey.center.x) * 2
    }

基本上,我的问题是:如何将平移行为与更改 viewController 中的变量(或变量数组/集合)联系起来?

最佳答案

我觉得创建 UIPanGestureRecognizer 应该是您的 View Controller 的责任,设置它并将其添加到您的 View (这里是您的CircularKeyView)。

它也应该是那个手势识别器的目标,所以它可以在自己的坐标系中设置 View 的中心( View 更新自己的框架似乎很奇怪)。正如 Apple 的 View Controller 编程指南所述:“ View Controller 负责管理其 View 层次结构”。

关于ios - 从 out-of-viewController 更新振荡器频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36680013/

相关文章:

ios - 如何在 iOS 应用程序中获取手机 IP 地址

ios - MVC : Custom type declared in model or view?

ios - iPhone 应用程序在某些设备上启动时崩溃,但在其他设备上运行

ios - 永远不会调用 CAAnimationDelegate 委托(delegate)

ios - CCCrypt 处理文件片段 AES CBC : garbage to end of files on encryption-decryption cycle

ios - 有没有办法在编辑 UITableView 时隐藏 "-"(删除)按钮

ios - 如果所有文本字段都在ios中填写,则启用按钮

ios - 指定在 XCTest XCUIApplication - Swift 中使用的模拟器

ios - 使用 'containing' 而不是 'equal to' 搜索数组

json - HTTP Post 请求 API - 解析用单引号括起来的 JSON