Search code examples
iphoneuislider

Trying to figure out where my touches are, when moving a slider control


I would like to create a variable speed slider control on the iPhone. Basically I am using a UISlider control but I would like to add a second dimension to this control by detecting where the finger is in the current view.

E.G. A user can slide the control left and right, but I want to see where their finger is vertically in the view.

My issue is that when you are manipulating a slider control. The following function is not called.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

so this means I cannot detect any touch events because I assume they are being sent to the slider? Anyone tried to do this? Or should I sub class UISlider. Hmmm.


Solution

  • header

    #import <Foundation/Foundation.h>
    
    
    @interface JBSlider : UISlider {
        double verticalTouchDelta;
    }
    @property(nonatomic, assign)    double verticalTouchDelta;
    
    @end
    

    main file

    import "JBSlider.h"

    @implementation JBSlider

    @synthesize verticalTouchDelta; - (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{ [super sendAction:action to:target forEvent:event]; //NSLog(@"HI HI");

    UITouch * touch = [[event touchesForView:self] anyObject];
    //NSLog(@"%f", [touch locationInView:self].y);
    verticalTouchDelta  = [touch locationInView:self].y;    
    

    }

    @end

    Simply change your slider from UISlider to JBSlider in Interface Builder. Works awesome.