如何制作UISlider的“轨迹”无形的?

发布于 2024-10-01 21:03:22 字数 281 浏览 9 评论 0原文

我试图在我的应用程序中模拟苹果的“滑动解锁”功能。我已经到了这一点(下图),但是正如您所看到的,UISlider 的“轨道”是可见的并且覆盖了我的文本。有没有办法以编程方式更改属性,使“轨道”不可见?

alt text

如果您需要我的任何代码,请告诉我。

提前致谢!

编辑:如果我将滑块的 alpha 更改为 0,它将摆脱我的滑动按钮,因此除非我做错了,否则这样做将不起作用。 :)

I'm trying to emulate Apple's "Slide to Unlock" feature in my application. I get to this point (image below), but as you can see the UISlider's "track" is visible and is covering up my text. Is there a way to change an attribute programmatically that will make the "track" invisible?

alt text

Please let me know if you need any of my code.

Thanks in advance!

EDIT: If I change the slider's alpha to 0, it gets rid of my sliding button, so doing that won't work unless I'm doing it wrong. :)

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

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

发布评论

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

评论(8

情深缘浅 2024-10-08 21:03:22

这是一个更简单的方法。无需创建图像,只需实例化一个空的 UIImage 类即可:P

UIImage *clearImage = [[UIImage alloc] init];
[self.slider setMinimumTrackImage:clearImage forState:UIControlStateNormal];
[self.slider setMaximumTrackImage:clearImage forState:UIControlStateNormal];

here's an even easier way. No need to create images, just instantiate an empty UIImage class :P

UIImage *clearImage = [[UIImage alloc] init];
[self.slider setMinimumTrackImage:clearImage forState:UIControlStateNormal];
[self.slider setMaximumTrackImage:clearImage forState:UIControlStateNormal];
江南月 2024-10-08 21:03:22

其实我只是想明白了。这就是我所做的:

UIImage *sliderMinimum = [[UIImage imageNamed:@"clearTrack.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:0];
[slider setMinimumTrackImage:sliderMinimum forState:UIControlStateNormal];
UIImage *sliderMaximum = [[UIImage imageNamed:@"clearTrack.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:0];
[slider setMaximumTrackImage:sliderMaximum forState:UIControlStateNormal];

clearTrack.png 只是我制作的清晰滑块图像。

现在我有这个:耶!
替代文本

Actually I just figured it out. Here's what I did:

UIImage *sliderMinimum = [[UIImage imageNamed:@"clearTrack.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:0];
[slider setMinimumTrackImage:sliderMinimum forState:UIControlStateNormal];
UIImage *sliderMaximum = [[UIImage imageNamed:@"clearTrack.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:0];
[slider setMaximumTrackImage:sliderMaximum forState:UIControlStateNormal];

clearTrack.png is just a clear slider image I made.

Now I have this: yay!
alt text

半枫 2024-10-08 21:03:22

可能没有办法隐藏轨迹; “滑动解锁”的行为不像 UISlider,可能是自定义控件。您也许能够破解滑块控件,也许可以通过将不透明度设置为低(0将使其隐藏并且不会接收触摸),但如果您走这条路,您可能会在之后遇到一些问题操作系统更新。苹果不会像微软那样在兼容性方面不遗余力。

正确的方法是使用自定义控件。这似乎比使用 UISlider 需要更多工作,但如果您将其与您花费和/或将花费在破解 UISlider 上的所有时间进行比较,则事实并非如此。

为此:子类化 UIControl。编写您自己的绘图代码以使其看起来正确(您可能可以重用您现在正在做的一些事情)。然后注册触摸事件来移动滑块手柄:

  • UIControlEventTouchDown:如果在手柄上,则设置一个“移动”标志
  • UIControlEventTouchDragInside:如果设置了移动标志,则将手柄移动到触摸位置;您可以只更新实例变量,调用 setNeedsDisplay 将其绘制在新位置。
  • UIControlEventTouchUpInside:如果设置了移动标志,并且句柄位于末尾,则解锁

如果您想模仿真正的解锁句柄,请尝试使用它并查看它的行为方式。您可能需要以不同的方式响应事件(如果将鼠标拖动到滑块路径之外会发生什么)。但你应该能够理解它的要点。

There probably isn't a way to hide the track; the "slide to unlock" doesn't behave like a UISlider and is probably a custom control. You might be able to hack the slider control, maybe by setting opacity low (0 will make it hidden and it won't receive touches), but if you go that route you will probably have something break after an OS update. Apple doesn't bend over backwards for compatibility like Microsoft does.

The right way to do this is with a custom control. It may seem like more work than using a UISlider, but it's not if you compare it against all the time you have spent and/or will spend hacking a UISlider.

To do it: subclass UIControl. Write your own drawing code to make it look right (you can probably reuse some of whatever you are doing now). Then register for touch events to move the slider handle:

  • UIControlEventTouchDown: if it's on the handle, set a "moving" flag
  • UIControlEventTouchDragInside: if the moving flag is set, move the handle to the touch position; you can just update an instance variable, call setNeedsDisplay to draw it in the new position.
  • UIControlEventTouchUpInside: if moving flag is set, and handle is at end, unlock

If you want to mimic the real unlock handle, play around with it and see how it behaves. You might need to respond to the events differently (what happens if you drag outside the slider path). But you should be able to get the gist of it.

泼猴你往哪里跑 2024-10-08 21:03:22

在斯威夫特中:

slider.setMinimumTrackImage(UIImage(), forState: .Normal)
slider.setMaximumTrackImage(UIImage(), forState: .Normal)

In Swift:

slider.setMinimumTrackImage(UIImage(), forState: .Normal)
slider.setMaximumTrackImage(UIImage(), forState: .Normal)
财迷小姐 2024-10-08 21:03:22

为了回答上述问题,您可以为最小和最大轨道图像设置透明的 1px png。

In answer to the stated question, you can set a transparent 1px png for the minimum and maximum track images.

生死何惧 2024-10-08 21:03:22

看起来只是将色调颜色设置为清除就可以了......

    [slider setMinimumTrackTintColor:[UIColor clearColor]];
    [slider setMaximumTrackTintColor:[UIColor clearColor]];

Looks like just setting the tint color to clear does it...

    [slider setMinimumTrackTintColor:[UIColor clearColor]];
    [slider setMaximumTrackTintColor:[UIColor clearColor]];
墨落画卷 2024-10-08 21:03:22

我认为@Arjay Waran 的回答是最好的。

这是它的 Swift 3.0 版本。

slider.minimumTrackTintColor = UIColor.clear
slider.maximumTrackTintColor = UIColor.clear

干杯

Answer by @Arjay Waran is the best in my opinion.

Heres the Swift 3.0 Version of it.

slider.minimumTrackTintColor = UIColor.clear
slider.maximumTrackTintColor = UIColor.clear

Cheers

南街九尾狐 2024-10-08 21:03:22

从故事板:-

Min TrackMax Track 选择透明颜色

在此处输入图像描述

From Story Board:-

select clear color for Min Track and Max Track

enter image description here

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