Clickable UISlider

2019-04-14 09:28发布

问题:

I'd like to make my UISlider clickable - to change the value on click to "blank space", e.g. It's set to zero, I click in the middle of the slider and it will "jump" to the middle. Is there any way how to do this?

回答1:

I want to propose something similar as jrtc27, but without subclassing: add a UI(Tap)GestureRecognizer to the slider.

UISlider *slider = [[[UISlider alloc] init] autorelease];
//configure slider
UITapGestureRecognizer *tapGestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderTapped:)] autorelease];
[slider addGestureRecognizer:tapGestureRecognizer];


- (void)sliderTapped:(UIGestureRecognizer *)gestureRecognizer {
    UISlider *slider = (UISlider *) gestureRecognizer.view;
    //setValue 
}


回答2:

Subclass it and then alter the -touchesBegan method. If the touch is sufficiently far away, call -setValue:, else call the super implementation (if you want it to always jump, you can just always call -setValue:).