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

折月煮酒 提交于 2019-12-06 14:35:15

问题


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 can easily implement it. Any other suggestion for implementing same are most most welcome.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/3156069/menu-over-slider-iphone-getting-the-x-co-ordinate-of-uislider-pointer-when-sli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!