dbr*_*dbr 15 cocoa objective-c nsslider
我正在使用NSSlider控件,并且我已将其配置为使用连续模式,以便在用户滑动时可以使用滑块的当前值不断更新NSTextField.我遇到的问题是,在用户放开旋钮之前我不想"提交"该值,即我不希望我的应用程序考虑该值,除非用户放开滑块来表示它达到了理想的价值.目前,我无法知道何时是这样的; 动作方法只是连续调用,没有指示滑块何时被释放.
如果可能的话,我需要一个解决方案来覆盖边缘情况,例如用户使用键盘或辅助功能工具(如果有这样的东西)与滑块交互.我开始研究使用鼠标事件,但由于我刚刚概述的原因,它似乎不是最佳解决方案.
mar*_*rux 28
这对我有用(并且比子类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
    }
}
您还可以在action方法中检查当前事件的类型:
- (IBAction)sliderChanged:(id)sender
{
    NSEvent *currentEvent = [[sender window] currentEvent];
    if ([currentEvent type] == NSLeftMouseUp) {
        // the slider was let go
    }
}
| 归档时间: | 
 | 
| 查看次数: | 3980 次 | 
| 最近记录: |