sagarkothari
sagarkothari

Reputation: 24810

Menu over slider - iPhone getting the x co-ordinate of UISlider Pointer when sliding

It is ok to set the max & min value for a UISlider control in iPhone. Accordingly slider will work - OK - Good.

First, I will give an example of what exactly I want to implement.

I am trying hard, But if I am able to get x co-ordinate, I can easily implement it. Any other suggestion for implementing same are most most welcome.

Upvotes: 1

Views: 1021

Answers (2)

Kalle
Kalle

Reputation: 13346

Start out with a UILabel for testing (just write something in it) and add IBOulets to it, as well as the UISlider. Next, add a IBAction for "value changed" for the slider.

You then need to calculate two values to figure out where your label should be placed -- adjustment from left, and pixels per value for the slider. Your @interface might look like this:

@interface sliderViewController : UIViewController {
    IBOutlet UISlider *slider;
    IBOutlet UILabel *label;
    float pixelsPerValue;
    float leftAdjust;
}
-(IBAction) sliderValueChanged:(id)sender;

@end

In e.g. viewDidLoad: for your view controller, you do the calculation for the two floats.

- (void)viewDidLoad {
    float width = slider.frame.size.width;
    pixelsPerValue = width / (slider.maximumValue - slider.minimumValue);
    leftAdjust = slider.frame.origin.x;
    [super viewDidLoad];
}

And finally, your IBAction might look like this:

-(IBAction) sliderValueChanged:(id)sender
{
    NSLog(@"changed");
    CGRect frame = label.frame;
    frame.origin.x = leftAdjust + (pixelsPerValue * slider.value);
    label.frame = frame;
}

Hope that helps.

Upvotes: 3

kennytm
kennytm

Reputation: 523374

I don't think that thing is implemented with a UISlider, but you can use the x-coordinate of the touch easily.

In the UISlider's action, you could accept a 2nd argument.

-(void)action:(UISlider*)sender forEvent:(UIEvent*)event
//                              ^^^^^^^^^^^^^^^^^^^^^^^^

From the UIEvent you can get a UITouch which contains its x, y coordinates.

Upvotes: 0

Related Questions