Как NSSlider может быть настроен для обеспечения нелинейного масштаба в какао?

StackOverflow https://stackoverflow.com/questions/5810753

  •  25-10-2019
  •  | 
  •  

Вопрос

Как 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 (подключить iboutlet / connect ibaction / enable continuos обновление)
- Добавить nstextfield (подключить iboutlet)

Результат:

enter image description here

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top