Artem
Artem

Reputation: 228

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

I have a vertical view and vertical slider on screen next to each other. They have almost same size. I want this view to move according to position of slider. I was trying this way:

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 is value to move my view for – e.g. 5 points

If I move slider slowly, view moves pretty accurate, but if I do it quickly, view moves in the way, like I only changed one value instead of twenty.

I know I can try to do it with "for in", but I was wondering is there a more proficient way to do this.

Upvotes: 0

Views: 584

Answers (1)

Andreas Oetjen
Andreas Oetjen

Reputation: 10199

I think the problem is that you don't use the sliders value property, but instead only add some heightOfMovement (nobody except you knows where this value comes from).

I think the following would be more straight-forward:

  • rename viewToMoveTopYConstreint to viewToMoveTopYConstraint (just a spelling mistake :-)
  • set the minValue of the slider to 0
  • set the maxValue of the slider to the height of the view to be moved
  • in your @IBAction just do the following:
    viewToMoveTopYConstraint.constant = sender.value

Upvotes: 1

Related Questions