UISlider 拇指在不接触拇指的情况下移动
UISlider thumb moving without touching thumb
我在我的项目中使用 UISlider
。通常,只要我们触摸并拖动拇指,拇指就会移动。但是,就我而言,当我触摸到它的边缘但没有触摸它时,拇指移动了。像这样:
这是我的代码:
self.slider = [[UISlider alloc] initWithFrame:CGRectMake(30, [UIScreen mainScreen].bounds.size.height - 200, [UIScreen mainScreen].bounds.size.width-30, 30)];
[self.view addSubview:self.slider];
为什么拇指动了,如何处理?我的目标是,拇指只有在我触摸并拖动它时才会移动,而当我触摸到它的边缘时它不会移动。
为此,您需要将 taoGesture 与 UISlider
一起使用,因此首先在您的滑块对象中添加 UITapGestureRecognizer
,然后在 gestureRecognizer 选择器的操作方法中设置其值。
在视图中添加滑块后添加以下代码。
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSliderTap:)];
[self.slider addGestureRecognizer:tapGestureRecognizer];
现在onSliderTap
你可以获取定位点然后更新滑块的值
- (void)onSliderTap:(UITapGestureRecognizer *)recognizer {
float width = self.slider.frame.size.width;
CGPoint point = [recognizer locationInView:recognizer.view];
CGFloat sliderX = self.slider.frame.origin.x;
float newValue = ((point.x - sliderX) * self.slider.maximumValue) / width;
[self.slider setValue:newValue];
}
尝试添加以下代码:
- (void)viewDidLoad
{
UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderTapped:)];
[slider addGestureRecognizer:tapGest];
}
- (void)sliderTapped:(UIGestureRecognizer *)tapGest
{
UISlider* slider = (UISlider*)tapGest.view;
if (slider.highlighted)
return; // tap on thumb, let slider deal with it
CGPoint pt = [tapGest locationInView: slider];
CGFloat percentage = pt.x / slider.bounds.size.width;
CGFloat delta = percentage * (slider.maximumValue - slider.minimumValue);
CGFloat value = slider.minimumValue + delta;
[slider setValue:value animated:YES];
}
我在我的项目中使用 UISlider
。通常,只要我们触摸并拖动拇指,拇指就会移动。但是,就我而言,当我触摸到它的边缘但没有触摸它时,拇指移动了。像这样:
这是我的代码:
self.slider = [[UISlider alloc] initWithFrame:CGRectMake(30, [UIScreen mainScreen].bounds.size.height - 200, [UIScreen mainScreen].bounds.size.width-30, 30)];
[self.view addSubview:self.slider];
为什么拇指动了,如何处理?我的目标是,拇指只有在我触摸并拖动它时才会移动,而当我触摸到它的边缘时它不会移动。
为此,您需要将 taoGesture 与 UISlider
一起使用,因此首先在您的滑块对象中添加 UITapGestureRecognizer
,然后在 gestureRecognizer 选择器的操作方法中设置其值。
在视图中添加滑块后添加以下代码。
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSliderTap:)];
[self.slider addGestureRecognizer:tapGestureRecognizer];
现在onSliderTap
你可以获取定位点然后更新滑块的值
- (void)onSliderTap:(UITapGestureRecognizer *)recognizer {
float width = self.slider.frame.size.width;
CGPoint point = [recognizer locationInView:recognizer.view];
CGFloat sliderX = self.slider.frame.origin.x;
float newValue = ((point.x - sliderX) * self.slider.maximumValue) / width;
[self.slider setValue:newValue];
}
尝试添加以下代码:
- (void)viewDidLoad
{
UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderTapped:)];
[slider addGestureRecognizer:tapGest];
}
- (void)sliderTapped:(UIGestureRecognizer *)tapGest
{
UISlider* slider = (UISlider*)tapGest.view;
if (slider.highlighted)
return; // tap on thumb, let slider deal with it
CGPoint pt = [tapGest locationInView: slider];
CGFloat percentage = pt.x / slider.bounds.size.width;
CGFloat delta = percentage * (slider.maximumValue - slider.minimumValue);
CGFloat value = slider.minimumValue + delta;
[slider setValue:value animated:YES];
}