Determine when NSSlider knob is 'let go' in continuous mode

后端 未结 7 1115
青春惊慌失措
青春惊慌失措 2020-12-17 10:19

I\'m using an NSSlider control, and I\'ve configured it to use continuous mode so that I can continually update an NSTextField with the current value of the slider while the

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-17 10:38

    This works for me (and is easier than subclassing NSSlider):

    - (IBAction)sizeSliderValueChanged:(id)sender {
        NSEvent *event = [[NSApplication sharedApplication] currentEvent];
        BOOL startingDrag = event.type == NSLeftMouseDown;
        BOOL endingDrag = event.type == NSLeftMouseUp;
        BOOL dragging = event.type == NSLeftMouseDragged;
    
        NSAssert(startingDrag || endingDrag || dragging, @"unexpected event type caused slider change: %@", event);
    
        if (startingDrag) {
            NSLog(@"slider value started changing");
            // do whatever needs to be done when the slider starts changing
        }
    
        // do whatever needs to be done for "uncommitted" changes
        NSLog(@"slider value: %f", [sender doubleValue]);
    
        if (endingDrag) {
            NSLog(@"slider value stopped changing");
            // do whatever needs to be done when the slider stops changing
        }
    }
    

提交回复
热议问题