11

NSSlider我的macOS 应用程序中有一个名为wave_mode_slider. 我要做的是在输入时更新所述滑块的值。所以,我所做的是:

self.wave_mode_slider?.integerValue = ("\(params[2])" as NSString).integerValue

这会将滑块(白点)的值设置为预期的输入值。但是,在看到任何实际结果之前,我必须在应用程序外部单击,这会导致滑块的白点跳到它的新值。如此处所示:在此处输入图像描述

有没有办法让滑块立即更新?

我的滑块是这样创建的:

    self.wave_mode_slider = NSSlider(frame:CGRect(x: 10, y: 100, width: 20, height: 300))
    self.wave_mode_slider?.cell = OWOWSliderVertical()
    self.wave_mode_slider?.maxValue = 127
    self.wave_mode_slider?.target = self
    self.view?.addSubview(self.wave_mode_slider!)

我试图将isContinuous滑块的属性设置为 true,但这不会改变结果。


编辑:

    var midiClient : MIDIClientRef = 0
    var inPort : MIDIPortRef = 0
    let observer = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())

        MIDIClientCreate("WobClient" as CFString, nil, nil, &midiClient)
        MIDIInputPortCreate(midiClient, "WobClient_InPort" as CFString, {
            (pktList: UnsafePointer<MIDIPacketList>, readProcRefCon: UnsafeMutableRawPointer?, srcConnRefCon: UnsafeMutableRawPointer?) -> Void in
            let packetList : MIDIPacketList = pktList.pointee
            var packet : MIDIPacket = packetList.packet
            let mySelf = Unmanaged<Wob>.fromOpaque(srcConnRefCon!).takeUnretainedValue()
            for _ in 1...packetList.numPackets {
                let bytes = Mirror(reflecting: packet.data).children
                var params : [UInt64] = []

                var i = packet.length
                for (_, attr) in bytes.enumerated() {
                    let string = String(format: "%02X ", attr.value as! UInt8)
                    params.append(UInt64(strtoul(string, nil, 16)))
                    i -= 1

                    if (i <= 0) {
                        break
                    }
                }

//                print(("\(params[2])" as NSString).integerValue)
                mySelf.setWaveSliderValue(value: ("\(params[2])" as NSString).integerValue)
                packet = MIDIPacketNext(&packet).pointee
            }

        }, nil, &inPort)

        MIDIPortConnectSource(inPort, self.source, observer)

这是我从中获得价值的地方

4

1 回答 1

4

最后,这很容易,只要我看到 Willeke 关于线程的评论,我就使用主线程将值设置为:

DispatchQueue.main.async {
    mySelf.wave_mode_slider?.floatValue = ("\(params[2])" as NSString).floatValue
}
于 2017-05-03T14:24:19.887 回答