如何使用步进滑块在滑块拇指上添加 UILabel? [英] how to add UILabel over slider thumb with step slider?

查看:24
本文介绍了如何使用步进滑块在滑块拇指上添加 UILabel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 5 个部分的滑块,我想跨过这些部分!我这样做了:

I have a slider with 5 part, which I wanted to step over for these part! and I did this :

@IBAction func changeCostSlider(_ sender: UISlider) {
    sender.value = roundf(costSlider.value)
}

我还想在滑块的拇指上添加一个标签,我尝试将此代码添加到上面的代码中:

I also want to add a label over the thumb of my slider, I tried to add this code to the code above :

let trackRect: CGRect  = costSlider.trackRect(forBounds: costSlider.bounds)
    let thumbRect: CGRect  = costSlider.thumbRect(forBounds: costSlider.bounds , trackRect: trackRect, value: costSlider.value)
    let x = thumbRect.origin.x + costSlider.frame.origin.x
    let y = costSlider.frame.origin.y - 20
    sliderLabel.center = CGPoint(x: x, y: y)

但是当我移动滑块时,我的标签从我在故事板中放置标签的位置跳转,并且有一些滞后!有人对如何将此标签添加到我的步骤滑块并正常工作有任何建议吗?!

but when I move my slider, my label jumps from where I put the label in storyboard, and it has some lags! anybody have any suggestion on how to add this label to my step slider and works correctly?!

我认为我的问题是我把我的 UILable 固定在 storyborad 中,当我在滑块中移动拇指时,它会回到固定位置!我该如何解决?!

I think my problem is that I put my UILable fixed in storyborad, and when I move thumb in slider, for a mili second, it goes back to the fixed place! how can I solve it?!

我尝试以编程方式添加我的 UILabel,但在我触摸拇指之前它无法工作!这是如何完成的:

I've tried to add my UILabel programmatically and it's not working until I touch the thumb! and there is how it's done:

var sliderLableP = UILabel()
@IBAction func changeCostSlider(_ sender: UISlider) {
    sender.value = roundf(costSlider.value)
    print(costSlider.value)
    let trackRect: CGRect  = costSlider.trackRect(forBounds: costSlider.bounds)
    let thumbRect: CGRect  = costSlider.thumbRect(forBounds: costSlider.bounds , trackRect: trackRect, value: costSlider.value)
    let x = thumbRect.origin.x + costSlider.frame.origin.x
    let y = costSlider.frame.origin.y - 20
    sliderLableP.center = CGPoint(x: x, y: y)
}

在我的 viewDidLoad 中,我添加了这个:

and in my viewDidLoad I've added this:

sliderLableP = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
    let trackRect: CGRect  = costSlider.trackRect(forBounds: costSlider.bounds)
    let thumbRect: CGRect  = costSlider.thumbRect(forBounds: costSlider.bounds , trackRect: trackRect, value: costSlider.value)
    let x = thumbRect.origin.x + costSlider.frame.origin.x
    let y = costSlider.frame.origin.y - 20
    sliderLableP.center = CGPoint(x: x, y: y)
    sliderLableP.textAlignment = NSTextAlignment.center
    sliderLableP.textColor = UIColor.blue
    self.containerView.addSubview(sliderLableP)
    containerView.bringSubview(toFront: sliderLableP)

推荐答案

这个解决方案对我有用:

This solution works for me:

@IBAction func changeCostSlider(_ sender: UISlider) {
    sender.value = roundf(sender.value)

    let trackRect = sender.trackRect(forBounds: sender.frame)
    let thumbRect = sender.thumbRect(forBounds: sender.bounds, trackRect: trackRect, value: sender.value)
    self.sliderLabel.center = CGPoint(x: thumbRect.midX, y: self.sliderLabel.center.y)
}

这篇关于如何使用步进滑块在滑块拇指上添加 UILabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆