如何定制NSSlider以在可可中提供非线性量表?
题
如何定制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
#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
接口构建器:
- 添加nsslider(connect iboutlet / connect ibaction / enable continuos更新)
- 添加nstextfield(连接iboutlet)
结果:
不隶属于 StackOverflow