gpt4 book ai didi

ios - UISlider 拇指在不接触拇指的情况下移动

转载 作者:行者123 更新时间:2023-11-28 21:21:58 27 4
gpt4 key购买 nike

我在我的项目中使用 UISlider。通常,只要我们触摸并拖动拇指,拇指就会移动。但是,就我而言,当我触摸到它的边缘但没有触摸它时,拇指移动了。像这样:

enter image description here

这是我的代码:

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];

为什么拇指移动了,如何处理?我的目标是,只有当我触摸并拖动它时,拇指才会移动,而当我触摸它的边缘时,它不会移动。

最佳答案

尝试添加以下代码:

  - (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];

}

关于ios - UISlider 拇指在不接触拇指的情况下移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39524378/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com