如何定制NSSlider以提供可可的非线性比例?即 - 0,2,4,6,10。
由于滑块仅限于在刻度线上停止,我希望滑块停在0,2,4,6,10(例如,不是8)。感谢。
答案 0 :(得分:12)
基于具有所需值的数组快速编写的示例:
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:
- 添加NSSlider(连接IBOutlet /连接IBAction /启用连续更新)
- 添加NSTextField(连接IBOutlet)
结果: