user2549858
user2549858

Reputation: 21

Let NSSlider move in other ways?

I am trying to figure out how to make a slide action without dragging. Usually when I want to change values using slider, I do either "leftClick anywhere on the slider field" or, continuously "leftClick the knob and drag it". What I would like to do is have the values change to wherever "the mouse cursor is put" on the slider without clicking or dragging, so that Only moving mouse cursor enables me to change values.

I have looked up and been digging down to NSControl, NSEvent(MouseDown) as well as NSSlider, and I guess "mousedown" method in NSControl is the one I want to fix somehow, but have no specific idea how to do that.

I would appreciate answers. Thank you very much in advance.

Upvotes: 2

Views: 564

Answers (1)

Parag Bafna
Parag Bafna

Reputation: 22930

Embed you NSSlider in NSView.

enter image description here

Subclass NSView and catch mouse move event

@interface PBView : NSView {
    id delegate;
}

@property (assign)id delegate;
@end

@implementation PBView
@synthesize delegate;

-(void) mouseMoved: (NSEvent *) thisEvent
{
    NSPoint cursorPoint = [ thisEvent locationInWindow ];
    [delegate sliderValueChanged];
}
- (void)createTrackingArea
{
    NSTrackingAreaOptions focusTrackingAreaOptions = NSTrackingMouseMoved;
    focusTrackingAreaOptions |= NSTrackingActiveInActiveApp;
    focusTrackingAreaOptions |= NSTrackingMouseEnteredAndExited;
    focusTrackingAreaOptions |= NSTrackingAssumeInside;
    focusTrackingAreaOptions |= NSTrackingInVisibleRect;

    NSTrackingArea *focusTrackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect
                                                                     options:focusTrackingAreaOptions owner:self userInfo:nil];
    [self addTrackingArea:focusTrackingArea];
}


- (void)awakeFromNib
{
    [self createTrackingArea];
}
@end

enter image description here

Now implement

-(void)sliderValueChanged
{
    NSPoint mouseLoc;
    mouseLoc = [NSEvent mouseLocation]; // mouse location

    NSRect r= [window frame];// window location
    NSLog(@"%@", NSStringFromPoint(r.origin));

    NSLog(@"%@",    NSStringFromPoint(mouseLoc));
    [silder setIntValue:(mouseLoc.x -r.origin.x)];//silder is object of NSSlider
}

Upvotes: 1

Related Questions