UISlider 不更新值
Posted
技术标签:
【中文标题】UISlider 不更新值【英文标题】:UISlider not updating values 【发布时间】:2018-08-11 02:35:04 【问题描述】:抱歉,如果这是一个愚蠢的问题。我似乎无法让我的滑块在与之交互时更新其值。 (我将向大家指出这段长代码中的最后一个方法)
class CustomSlider: UISlider
override func trackRect(forBounds bounds: CGRect) -> CGRect
var rect = super.trackRect(forBounds: bounds)
rect.size.height = 7
return rect
class FactionButton: CustomSlider
var factionSlider = CustomSlider(frame: CGRect(x: 15, y: 542, width: 386, height: 57))
func factionBalanceSlider()
factionSlider.minimumValueImage = #imageLiteral(resourceName: "Alliance Slider")
factionSlider.maximumValueImage = #imageLiteral(resourceName: "Horde Slider")
factionSlider.setThumbImage(#imageLiteral(resourceName: "Thumb Image"), for: .normal)
factionSlider.minimumTrackTintColor = UIColor(red:0.08, green:0.33, blue:0.69, alpha:0.8)
factionSlider.maximumTrackTintColor = UIColor(red:1.00, green:0.00, blue:0.00, alpha:0.59)
factionSlider.setValue(0.5, animated: true)
factionSlider.isContinuous = false
factionSlider.addTarget(self, action: #selector(recordFactionBalance(sender:)), for: .valueChanged)
func getSlider() -> CustomSlider
return factionSlider
override func trackRect(forBounds bounds: CGRect) -> CGRect
var customBounds = super.trackRect(forBounds: bounds)
customBounds.size.height = 10
return customBounds
@objc func recordFactionBalance(sender: CustomSlider)
//also calculates balance and adds it into the quiz data
print("hi")
print(sender.value) //It's this part that doesn't work
这是最接近底部的位置有问题。 (其他一切都很好)动作功能似乎根本没有被触发,即使我正在与之交互。两个打印语句都没有被执行。任何想法为什么?
干杯
【问题讨论】:
由于isContinuous = false
,滑动完成后不打印吗?你在哪里打电话factionBalanceSlider
?可能您忘记添加有问题的那部分?
@adev 我考虑了他们两个,不幸的是 re: isContinuous - 我确实尝试过它声明为真和假,但都没有修复它。 re: 滑块函数 - 我在 viewDidLoad 中调用了它:(
将初始化代码移至override func didMoveToSuperview() minimumValueImage = #imageLiteral(resourceName: "Alliance Slider") maximumValueImage = #imageLiteral(resourceName: "Horde Slider") setThumbImage(#imageLiteral(resourceName: "Thumb Image"), for: .normal) minimumTrackTintColor = UIColor(red:0.08, green:0.33, blue:0.69, alpha:0.8) maximumTrackTintColor = UIColor(red:1.00, green:0.00, blue:0.00, alpha:0.59) setValue(0.5, animated: true) isContinuous = false addTarget(self, action: #selector(recordFactionBalance), for: .valueChanged)
并在那里设置你的框架frame = CGRect(x: 15, y: 542, width: 386, height: 57)
【参考方案1】:
从getSlider()
,我猜你正在使用这个class
作为一个实用程序来获取CustomSlider
。所以,我怀疑您正在将滑块添加到view
,如下所示,
let container = FactionButton()
container.factionBalanceSlider()
let slider = container.getSlider()
self.view.addSubview(slider)
如果您不将container
添加到设置为.valueChange
事件接收者的view
,则它不会收到任何事件。要接收事件,您还需要在view
中添加container
,如下所示,
self.view.addSubview(container)
【讨论】:
以上是关于UISlider 不更新值的主要内容,如果未能解决你的问题,请参考以下文章
为 UISlider 的 valueChanged 更新 UITableView titleForFooterInSection?