2

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.

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144

1 Answers1

1

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
}
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
  • Fantastic example, Thank you very much. I did exactly the same thing, however, I cannot move slider without clicking. Let me understand, your saying "Embed your NSSlider in NSView" is: 1. Choose any slider from the library bottom right and put it on the window 2. Select Editor -> Embed in -> Custom View correct? Is there anything special that I need to do other than embedding and coding? Well the problem I think is that my PBView (embedded in the slider) is not connected with anything (judging from the connection inspector), though yours is. Could you tell me where/how to connect it with? – user2549858 Jul 08 '13 at 05:33
  • change Custom View class to PBView (NSView-->PBView) – Parag Bafna Jul 08 '13 at 05:45
  • Thank you very much for the quick reply. I had done it already but it did not work. But Now I got how to; in the app delegate objects in IB, I drag "File's Owner" to the PBView and it worked. I need to study why it worked though. However, Another problem is that the slider knob won't move unless I move the mouse horizontally. I want the knob move when I move the mouse upn down. Would the solution lie in the func "createTrackingArea"? Sorry for questions, but could you give me advice how to correct the function(s) to do the same thing by using vertical slider, if you would not mind? – user2549858 Jul 08 '13 at 08:35
  • I got the solution as following your advice. Since the slider value depends on the "int value" variable in the sliderValueChanged func, which is determined by mouse locations, adjusting "value" enables me to set the volume appropriately. Thank you very much Mr.Bafna. You helped me a lot!! – user2549858 Jul 09 '13 at 04:50