是否可以向 UISlider 的缩略图添加标签?
我想向滑块的拇指添加一个标签,该标签应该显示滑块的值,并且当拇指被拖动到右侧时也会发生变化。 是否可以??
任何意见或建议将不胜感激。
I want to add a label to the slider's thumb which should show the value of the slider and changes too when thumbs is being dragged towards right side.
Is it possible??
Any comments or suggestion would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我从滑块 (UIImageView) 中抓取拇指图像并将标签添加到其中。漂亮又干净。
然后您可以在需要时更改 label.text。
注意:UISlider 的子视图顺序将来可能会发生变化,但是拇指不太可能不再是最上面的视图,因为它始终是滑块中交互的主要点。
Swift 3 - 更详细的示例(在 IB 中链接您的滑块)
I grab the thumb image from the slider (UIImageView) and add my label to it. Nice and clean.
Then you change the label.text whenever you need to.
Note: the subview order of UISlider could change in the future, however it's unlikely that the thumb would no longer be the topmost view, as it will always be the main point of interaction in a slider.
Swift 3 -- More detailed example (link your slider in IB)
您可以执行与此示例类似的操作,将文本直接绘制到拇指图像上。这是一个粗略的示例,因此您需要更改它才能对您的项目有意义。
You could do something similar to this example which draws text directly to your thumb image. It's a rough example so you will need to change it to make sense for your project.
Swift 3.2 中自定义类的简单实现。这对我来说效果很好。
我希望它有帮助:)
A simple implementation of a custom class in Swift 3.2. This is working nicely for me.
I hope it helps :)
在 Interface Builder 中添加
UISlider
和UILabel
。创建 IBOutlet 以在代码中访问它们,并添加 IBAction 以响应滑块值的更改。然后在您的代码中写入:
编辑:
要使用代码创建滑块和标签,请添加以下内容:
Add a
UISlider
and aUILabel
in Interface Builder. Create IBOutlets to access them in code and add a IBAction to respond to changes of the slider value.Then in your code write:
EDIT:
To create the slider and label with code add this:
您可以使用
-thumbRectForBounds:trackRect:value:]
访问滑块的拇指图像视图的矩形,因此如果添加UILabel
子视图,您可以将其相对于拇指对齐-layoutSubviews
中的图像视图使用此返回的矩形。但是,如果您想使用自动布局将标签与拇指图像对齐,那么您需要直接访问拇指的
UIImageView
。我一般不热衷于探究 UIKit 套件组件的子视图,但我认为您可以使用公共 API 的详细信息以面向未来的方式搜索图像视图:该 API 提供了能够为拇指设置 UIImage ,因此您可以使用它作为测试以确保您不会获得其他图像视图。如果您自己明确设置拇指图像,这会更安全(因为将来可能会更改滑块以绘制拇指图像并非不可能...)。另外,拇指图像视图当前是最后一个子视图(不保证继续前进),因此我在这里进行反向枚举,希望尽可能快地获得它,同时不对其索引做出假设。
注意:我必须懒洋洋地创建拇指标签,因为 UISlider 的子视图似乎就是这样。
You can access the rect for the slider's thumb image view with
-thumbRectForBounds:trackRect:value:]
, so if you add aUILabel
subview you can align it relative to the thumb image view in-layoutSubviews
using this returned rect.However, if you'd like to use Autolayout to align your label with the thumb image then you need access to the thumb's
UIImageView
directly. I'm not keen on spelunking the subviews ofUIKit
kit components generally, but I think you can use details of the public API to search for the image view in a future-proofed way:The API provides the ability to set a
UIImage
for the thumb, so you can use this as a test to ensure that you aren't getting some other image view. This is even safer if you are explicitly setting a thumb image yourself (since it's not impossible that the slider might be changed to draw the thumb image in future...). Also, the thumb image view is currently the last subview (not guaranteed going forward), so I'm doing a reverse enumeration here to hopefully get it as faster as possible whilst not making assumptions about its index.NB: I had to create my thumb label lazily, since
UISlider
's subviews seem to be so.让这变得美好而简单..首先这是最终结果
现在,代码:
上述方法来自我使用的
NSLayoutConstraint
类别(即将推出)make this nice and easy.. first off this is the final result
and now, code:
the above methods are used from an
NSLayoutConstraint
category i use (coming soon)好吧,我对 UISlider 进行了子类化并重写了 NSControl 的方法。您只需要将您的课程更改为故事板,
我的标签就可以根据要求添加到拇指上方。
Well, I subclassed the UISlider and override the methods of NSControl. You just need to change your class into storyboard
I label can be add above thumb as per requirement.