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.
- In iPad comes with Calendar application as a default.
- Open calendar, at the bottom - you can see day/week/month listing
- When you slide your finger on it, you will see a pop up menu when sliding.
- I want to implement the same functionality on slider.
I am trying hard, But if I am able to get x co-ordinate, I c开发者_StackOverflow社区an easily implement it. Any other suggestion for implementing same are most most welcome.
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.
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.
精彩评论