问题描述
NSSlider如何定制以提供可可的非线性尺度?即 - 0,2,4,6,10。
滑块被限制在停止在刻度线上时,我希望滑块停止在0,2,4 ,6,10(例如不是8)。感谢。
基于具有所需值的数组的快速写入示例:
SampleAppDelegate.h
#import< Cocoa / Cocoa.h>
@interface SampleAppDelegate:NSObject< NSApplicationDelegate> {
NSWindow * window;
NSArray * values;
IBOutlet NSSlider * theSlider;
IBOutlet NSTextField * theLabel;
}
- (IBAction)sliderChanged:(id)sender;
@property(assign)IBOutlet NSWindow * window;
@end
SampleAppDelegate.h
#importSampleAppDelegate.h
@implementation SampleAppDelegate
@合成窗口;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
values = [[NSArray alloc] initWithObjects:@0,@1,@2 ,@10,@50,nil];
[theSlider setNumberOfTickMarks:[values count]];
[theSlider setMinValue:0];
[theSlider setMaxValue:[values count] -1];
[theSlider setAllowsTickMarkValuesOnly:YES];
[theLabel setStringValue:[values objectAtIndex:0]];
[theSlider setIntValue:0];
}
- (IBAction)sliderChanged:(id)sender {
int current = lroundf([TheSlider floatValue]) ;
[theLabel setStringValue:[values objectAtIndex:current]];
}
@end
:
- 添加NSSlider(连接IBOutlet /连接IBAction /启用连续更新)
- 添加NSTextField(连接IBOutlet)
结果:
p>
How can NSSlider be customised to provide a non-linear scale in cocoa? i.e - 0, 2, 4, 6, 10.
With the slider constrained to stopping on tick marks only, I want the slider to stop on 0, 2, 4, 6, 10 (and not 8 for instance). Thanks.
Quickly written example based on an array with the desired values:
SampleAppDelegate.h
#import <Cocoa/Cocoa.h>
@interface SampleAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow * window;
NSArray * values;
IBOutlet NSSlider * theSlider;
IBOutlet NSTextField * theLabel;
}
- (IBAction)sliderChanged:(id)sender;
@property (assign) IBOutlet NSWindow *window;
@end
SampleAppDelegate.h
#import "SampleAppDelegate.h"
@implementation SampleAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
values = [[NSArray alloc] initWithObjects:@"0",@"1",@"2",@"10",@"50",nil];
[theSlider setNumberOfTickMarks:[values count]];
[theSlider setMinValue:0];
[theSlider setMaxValue:[values count]-1];
[theSlider setAllowsTickMarkValuesOnly:YES];
[theLabel setStringValue:[values objectAtIndex:0]];
[theSlider setIntValue:0];
}
- (IBAction)sliderChanged:(id)sender {
int current = lroundf([theSlider floatValue]);
[theLabel setStringValue:[values objectAtIndex:current]];
}
@end
Interface Builder:
- Add NSSlider (connect IBOutlet / connect IBAction / enable continuos updating)
- Add NSTextField (connect IBOutlet)
Result:
这篇关于NSSlider如何定制以提供可可的非线性尺度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!