How to get an event on UISlider track? I am able to get the events on UISlider's button but not on track. How should I go about it?
Thanks
For doing this you need to subclass the UISlider
and implement the touches event like: touchesBegan, touchesEnd,touchesCancelled,touchesMoved etc.
@interface yourSlider:UISlider
@end
@implementation yourSlider
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
@end
Subclass the UISlider
and override its touchesBegan:withEvent:
method. Get the point value from the touch event and calculate it's percentage by the point's .x value relative to the width of the slider.
Answers are correct but there is a workaround here , you can simply add a clear button equal to slider coordinations on your slider and detect the touch point on it then convert the X position to a slider value.
- (IBAction)buttonPressed:(id)sender forEvent:(UIEvent*)event
{
UIView *button = (UIView *)sender;
UITouch *touch = [[event touchesForView:button] anyObject];
CGPoint location = [touch locationInView:button];
NSLog(@"Location in button: %f, %f", location.x, location.y); \\ use this x to determine slider's value
}
Another workaround :
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderTapped:)];
gr.delegate=self;
[Slider addGestureRecognizer:gr];
-(void)sliderTapped:(UIGestureRecognizer*)g{
UISlider* s = (UISlider*)g.view;
if (s.highlighted)
return;
CGPoint pt = [g locationInView: s];
CGFloat value = s.minimumValue + pt.x / s.bounds.size.width * (s.maximumValue - s.minimumValue);
[s setValue:value animated:YES];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.