How can NSSlider be customised to provide a non-linear scale in cocoa?

拟墨画扇 提交于 2019-12-09 12:41:14

问题


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.


回答1:


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:



来源:https://stackoverflow.com/questions/5810753/how-can-nsslider-be-customised-to-provide-a-non-linear-scale-in-cocoa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!