根据 swift 中的 UISlider 位置移动视图的正确方法是什么?

What is the proper way to move view according to UISlider position in swift?

我在屏幕上有一个垂直视图和垂直滑块并排显示。它们的尺寸几乎相同。我希望此视图根据滑块的位置移动。我正在尝试这种方式:

if oldValue < Int(sender.value) {
        viewToMoveTopYConstreint.constant = viewToMoveTopYConstreint.constant - heightOfMovement
        oldValue = Int(sender.value)
    } else if oldValue > Int(sender.value) {
        viewToMoveTopYConstreint.constant = viewToMoveTopYConstreint.constant + heightOfMovement
        oldValue = Int(sender.value)
    }

heightOfMovement 是移动我的视图的值 - 例如5分

如果我缓慢移动滑块,视图移动会非常准确,但如果我快速移动滑块,视图就会移动,就像我只更改了一个值而不是二十个值一样。

我知道我可以尝试用 "for in" 来做到这一点,但我想知道是否有更熟练的方法来做到这一点。

我认为问题是你没有使用滑块 value 属性,而是只添加了一些 heightOfMovement(除了你没有人知道这个值来自哪里) .

我认为以下内容会更直接:

  • viewToMoveTopYConstreint 重命名为 viewToMoveTopYConstraint(只是一个拼写错误:-)
  • 将滑块的 minValue 设置为 0
  • 将滑块的maxValue设置为要移动的视图的高度
  • 在您的 @IBAction 中,只需执行以下操作:
    viewToMoveTopYConstraint.constant = sender.value