3

I'm pretty new to Swift, so please have mercy on me.

I have a function that I want to call when the user has finished sliding an NSSlider object. My natural thought is to use the mouseUp event. However, lots of Googling and reading documentation has left me stranded. What am I missing here?


    @IBOutlet weak var tSlider: NSSlider!
    @IBOutlet weak var tLabel: NSTextField!

    @IBAction func sliderValueChanged(_ sender: NSSlider) {

        if let label = self.value(forKey: "tLabel") as? NSTextField {

            label.intValue = sender.intValue
            t_value = Int8(sender.intValue)

        }

        appDelegate.sendValue()

        Swift.print("Value Changed")

        // Detect Mouse Up

        override func mouseUp(with theEvent: NSEvent) {

             appDelegate.sendHIDValue()

             Swift.print("Mouse Up")

         }

    }

Nothing happens when I release my mouse button from the slider. However, if I mouse up in the window itself, I do get a mouse up event. Specifically, how might I get the mouse up event from the NSSlider?

MCP_User
  • 79
  • 6
  • Maybe [this article](https://www.raywenderlich.com/760-macos-controls-tutorial-part-2-2) can help you – Joakim Danielson Apr 13 '19 at 18:52
  • Thank you @JoakimDanielson. That is indeed a good overview of the NSSlider. The intended functionality is to execute the function once the user lets go of the slider. In the article, it specifies how to execute something every time the slider value changes. In my case, I'm sending a save command to nvm cache on an external device, and saving hundreds of times as the user moves the slider overloads the memory heap. Thus, I need a one shot to happen each time someone adjusts the slider and lets go of it. That's why I'm thinking that mouseUp is the way to go. – MCP_User Apr 13 '19 at 19:32
  • Possible duplicate of [Subclassing NSSlider: Need a workaround for missing mouse up events (Cocoa OSX)](https://stackoverflow.com/questions/3919905/subclassing-nsslider-need-a-workaround-for-missing-mouse-up-events-cocoa-osx) – Willeke Apr 13 '19 at 21:38
  • Possible duplicate of [Determine when NSSlider knob is 'let go' in continuous mode](https://stackoverflow.com/questions/9416903/determine-when-nsslider-knob-is-let-go-in-continuous-mode/12066378) – Willeke Apr 13 '19 at 21:39

1 Answers1

4

Thank you @Willeke for posting that possible duplicate. With the information contained therein, I was able to solve this for myself. For anyone else who is interested in this topic, here is my solution that's been updated for Swift 4:



    @IBAction func sliderValueChanged(_ sender: NSSlider) {

        let event = NSApplication.shared.currentEvent

        if event?.type == NSEvent.EventType.leftMouseUp {

        // Do something

        }

    }

MCP_User
  • 79
  • 6