nsslider.integerValue要求我在更新值之前专注于另一个应用程序

时间:2017-05-01 13:06:08

标签: swift macos cocoa nsslider

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

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

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

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

我的滑块是这样创建的:

    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)

这是从我得到值

的地方

1 个答案:

答案 0 :(得分:4)

最后这很简单,只要我看到Willeke关于线程的评论,我就用主线程来设置值:

DispatchQueue.main.async {
    mySelf.wave_mode_slider?.floatValue = ("\(params[2])" as NSString).floatValue
}