简体   繁体   中英

Disable UISlider from sliding, but still allow it to receive UIControlEventTouchDown

Is there a way to disable a UISlider from sliding, but to get its UIControlEventTouchDown to fire? One way it could be done I believe is to set the slider start value the same as the end value, but I'd like to avoid doing that. Does anyone know a better way?

I would suggest setting userInteractionEnabled = NO for your UISlider, and adding an invisible button to handle the TouchDown

UIButton *btnSlider = [UIButton buttonWithType:UIButtonTypeCustom];
btnSlider.frame = CGRectMake(slider.frame.origin.x, slider.frame.origin.y,         slider.frame.size.width, slider.frame.size.height);
[btnSlider addTarget:self action:@selector(sliderTouched) forControlEvents:UIControlEventTouchDown];
btnSlider.backgroundColor = [UIColor clearColor];

[myView addSubview:btnSlider];

Then based on whatever condition you want to make the slider enabled, you can enable it and disable the button.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM