禁用 UISlider 滑动,但仍允许它接收 UIControlEventTouchDown

Ser*_*nce 1 iphone cocoa-touch uislider ios

有没有办法禁用 aUISlider滑动,但让它UIControlEventTouchDown触发?我认为可以完成的一种方法是将滑块起始值设置为与结束值相同,但我想避免这样做。有人知道更好的方法吗?

Sea*_*anT 5

我建议为您的 UISlider 设置 userInteractionEnabled = NO,并添加一个不可见的按钮来处理 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];
Run Code Online (Sandbox Code Playgroud)

然后根据您想要启用滑块的任何条件,您可以启用它并禁用按钮。