确定连续模式下NSSlider旋钮何时处于“放开"状态 [英] Determine when NSSlider knob is 'let go' in continuous mode
问题描述
我正在使用NSSlider控件,并且已将其配置为使用连续模式,以便在用户向周围滑动时可以使用滑块的当前值连续更新NSTextField.我遇到的问题是在用户放开旋钮之前,我不想提交"值,即我不希望我的应用程序考虑到该值,除非用户放开滑块以表示它处于所需的值.目前,我无法得知情况为何;动作方法只是被连续调用而没有指示释放滑块的时间.
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 user is sliding it around. The issue I have is that I don't want to 'commit' the value until the user lets go of the knob, i.e I don't want my application to take account of the value unless the user lets go of the slider to signify it's at the desired value. At the moment, I have no way of knowing when that's the case; the action method is just getting called continuously with no indication of when the slider has been released.
如果可能的话,我需要一个解决方案来涵盖一些极端的情况,例如用户与键盘或辅助工具(如果有的话)与with滑块交互.我开始研究使用鼠标事件,但是由于我刚才概述的原因,它似乎不是最佳解决方案.
If possible, I need a solution which will cover edge cases such as the user interacting the with slider with the keyboard or accessibility tools (if there is such a thing). I'd started to look into using mouse events, but it didn't seem like an optimum solution for the reasons I've just outlined.
推荐答案
这对我有效(并且比对NSSlider进行子类化更容易):
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
}
}
这篇关于确定连续模式下NSSlider旋钮何时处于“放开"状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!