带停止(非连续)的圆形NSSlider [英] Circular NSSlider with stop (non-continuous)
问题描述
我不知道如何将这更好地作为一个标题,但我需要使NSSlider作为一个正常的音量旋钮。在这一刻,它会旋转多次,我按住鼠标,并将其移动控件。我需要它停在0位置和100位置,我不能让它跳从0到100当我拖动它的方式。我希望我明白这一点。
一个老问题,但是在这里我们还是走了:
(1)将滑块设置为有100个刻度
(2)选择 >
(3)在Interface Builder中,将自定义单元格类设置为 SBCustomSliderCell
使用下面的代码:
@interface SBCustomSliderCell()
@property CGFloat lastSliderValue ;
@end
@implementation SBCustomSliderCell
- (void)awakeFromNib
{
self.lastSliderValue = -1;
}
- (double)closestTickMarkValueToValue:(double)value
{
double proposedValue = [super closestTickMarkValueToValue:value];
if(self.lastSliderValue == -1)
{
self.lastSliderValue = proposedValue;
return proposedValue;
}
double MAX_JUMP = 50;
double tickDifference = ABS(self.lastSliderValue - proposedValue);
BOOL isTurningUp = proposedValue> self.lastSliderValue;
if(tickDifference> MAX_JUMP)
{
proposedValue = isTurningUp? 0.0:self.maxValue;
}
NSLog(@value:%.2f gap:%.2f,proposedValue,tickDifference);
self.lastSliderValue = proposedValue;
return proposedValue;
}
- (NSRect)rectOfTickMarkAtIndex:(NSInteger)index
{
return NSZeroRect;
}
@end
I am not sure how to phrase this better as a title but I need to make an NSSlider that functions as a normal volume knob. At the moment it will spin around as many times as I hold the mouse down and move it around the control. I need it to stop at the "0" position and the "100" position, I cannot have it jumping from 0 to 100 when I drag it the other way. I hope I am making this clear. Does anyone know how to do this or have any suggestions?
An old question, but here we go anyway:
(1) Set the slider to have 100 tickmarks
(2) Select "Only stop at tickmarks"
(3) In Interface Builder, set the custom cell class to SBCustomSliderCell
Use the code below:
@interface SBCustomSliderCell()
@property CGFloat lastSliderValue;
@end
@implementation SBCustomSliderCell
- (void)awakeFromNib
{
self.lastSliderValue = -1;
}
- (double)closestTickMarkValueToValue:(double)value
{
double proposedValue = [super closestTickMarkValueToValue:value];
if (self.lastSliderValue == -1)
{
self.lastSliderValue = proposedValue;
return proposedValue;
}
double MAX_JUMP = 50;
double tickDifference = ABS(self.lastSliderValue - proposedValue);
BOOL isTurningUp = proposedValue > self.lastSliderValue;
if (tickDifference > MAX_JUMP)
{
proposedValue = isTurningUp ? 0.0 : self.maxValue;
}
NSLog(@"value: %.2f gap: %.2f", proposedValue, tickDifference);
self.lastSliderValue = proposedValue;
return proposedValue;
}
- (NSRect)rectOfTickMarkAtIndex:(NSInteger)index
{
return NSZeroRect;
}
@end
这篇关于带停止(非连续)的圆形NSSlider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!