如何让 UISlider 响应拇指滑块上方和下方轨道上的点击事件

发布于 2024-09-07 08:15:47 字数 215 浏览 8 评论 0原文

我有一个 284 像素宽的 UISlider,范围为 0-25。用户需要选择一个精确到小数点后 1 位的值(例如 12.3),这可以通过滚动手指来小心地实现,但是很繁琐,并且当您抬起手指时往往会改变值。

我正在尝试对其进行安排,以便您可以通过点击拇指上方或下方的滑块将滑块向上或向下移动 0.1 个间隔。我确信这是可能的,但由于这是我的第一个 xcode 项目,我正在努力。有人能指出我正确的方向吗?

I have a 284 pixel wide UISlider with a range of 0-25. The user needs to select a value to 1 decimal place (say 12.3), which is possible with care by rolling your finger, but is fiddly and tends to change value as you lift your finger.

I am trying to arrange it so that you can inch the slider up or down by 0.1 intervals by tapping on the slider rail above or below the thumb. I'm sure it's possible, but as this is my first xcode project I'm struggling. Can anyone point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦里的微风 2024-09-14 08:15:47

重新实现。

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event

我认为更好的解决方案就是子类化 UISlider,并在触摸事件位于滑块边界内时返回 YES 来

I think a better solution is just to subclass UISlider, and to re-implement

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event

by returning YES when the touch event is inside the slider bounds.

抚你发端 2024-09-14 08:15:47

iPhone OS 的 UISlider 控件的一个长期存在的问题是你无法将其变得更高。更具体地说,您可以通过为其拇指和轨道指定更高的图像来使其看起来更高,但触摸区域的高度仍为 23 像素。

您需要做的是子类 UISlider,并覆盖类中的 pointInside:

// How many extra touchable pixels you want above and below the 23px slider
#define SIZE_EXTENSION_Y 10

- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent*)event {
    CGRect bounds = self.bounds;
    bounds = CGRectInset(bounds, 0, SIZE_EXTENSION_Y);
    return CGRectContainsPoint(bounds, point);
}

在 Interface Builder 中,将滑块设置为使用新的 UISlider 子类,现在您拥有一个可触摸高度为 43px 的滑块。

A long-standing problem with the iPhone OS' UISlider control is that you can't make it taller. More specifically, you can make it look taller by specifying taller images for its thumb and track, but the touch area stays as a tiny 23px tall.

What you need to do is subclass UISlider, and override pointInside in your class:

// How many extra touchable pixels you want above and below the 23px slider
#define SIZE_EXTENSION_Y 10

- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent*)event {
    CGRect bounds = self.bounds;
    bounds = CGRectInset(bounds, 0, SIZE_EXTENSION_Y);
    return CGRectContainsPoint(bounds, point);
}

In Interface Builder, set the slider to use your new UISlider subclass, and you now have a slider with a 43px touchable height.

久隐师 2024-09-14 08:15:47

我想到了几个选项:

  1. 创建不可见按钮并将它们放置以接收这些点击
  2. 子类 UISlider 并拦截 TouchBegan 等方法。确定是否要根据命中的位置响应命中,或调用超级函数。您可能需要扩展框架以包含您触摸的位置。

我不确定创建一个非标准(尤其是不可见的!)实现是一个好主意,但是 - 我建议您添加可见按钮进行微调。侧面的简单向上箭头/向下箭头可能会有所帮助。还要考虑 UISlider 是否是您想要的,因为它对您来说工作得不够好。

A couple options come to mind:

  1. Create invisible buttons and place them to receive these clicks
  2. Subclass UISlider and intercept the touchesBegan, etc methods. Determine if you want to respond to the hit based on its location, or call the super's function. You may need to expand the frame to include where you're touching.

I'm not sure that creating a non standard (especially invisible!) implementation is a great idea, however - I'd recommend you add visible buttons for fine-tuning. A simple up-arrow / down arrow to the side might help. Also consider if a UISlider is what you want at all, given that it doesn't work well enough for you.

入画浅相思 2024-09-14 08:15:47

在 Interface Builder 中,您无法设置 UISlider 的高度,因为它是固定的。但是在您设置拇指图像和轨道图像值的代码中,您可以设置帧高度,如下所示。这将打开对触摸敏感的区域。

CGRect sliderFrame = self.mySlider.frame;
sliderFrame.size.height = 42.0;
[self.mySlider setFrame:sliderFrame];

In the Interface Builder you cannot set the height of UISlider because it is fixed. But in your code where you set the values for the thumb image and the track images you can set the frame height as you see below. This will open up the area which is sensitive to touches.

CGRect sliderFrame = self.mySlider.frame;
sliderFrame.size.height = 42.0;
[self.mySlider setFrame:sliderFrame];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文