David Seek David Seek - 5 months ago 18
iOS Question

Swift / SnappingSlider / two instances of the slider

I'm using the SnappingSlider.
I want to use two instances of the Slider and be able to change different values depending on the used Slider.

This is my current code:

import UIKit
import SnappingSlider

class ViewController: UIViewController, SnappingSliderDelegate {

private var remainingTime:Double = 0
private let remainingTimeLabel:UILabel = UILabel(frame: CGRectZero)
private let remainingTimeSlider:SnappingSlider = SnappingSlider(frame: CGRectMake(0.0, 0.0, 10.0, 10.0), title: "Slide Me")
private let percentageSlider:SnappingSlider = SnappingSlider(frame: CGRectMake(0.0, 0.0, 10.0, 10.0), title: "Slide Me")

override func viewDidLoad() {
super.viewDidLoad()
remainingTimeLabel.frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width * 0.5, 80.0)
remainingTimeLabel.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.21 - remainingTimeLabel.bounds.size.height * 0.75)
remainingTimeLabel.font = UIFont(name: "TrebuchetMS-Bold", size: 25.0)
remainingTimeLabel.textColor = UIColor.lightGrayColor()
remainingTimeLabel.textAlignment = NSTextAlignment.Center
remainingTimeLabel.text = "\(remainingTime)"
self.view.addSubview(remainingTimeLabel)

remainingTimeSlider.delegate = self
remainingTimeSlider.frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width * 0.9, 50.0)
remainingTimeSlider.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.2)
self.view.addSubview(remainingTimeSlider)

percentageSlider.delegate = self
percentageSlider.frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width * 0.9, 50.0)
percentageSlider.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.4)
self.view.addSubview(percentageSlider)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func snappingSliderDidIncrementValue(slider: SnappingSlider) {

remainingTime += 0.5
remainingTimeLabel.text = "\(remainingTime)"
}

func snappingSliderDidDecrementValue(slider: SnappingSlider) {

remainingTime = max(0, remainingTime - 0.5)
remainingTimeLabel.text = "\(remainingTime)"
}


}


I want to do something like this:

func snappingSliderDidIncrementValue(slider: SnappingSlider) {

if slider == sliderA {
remainingTime += 0.5
remainingTimeLabel.text = "\(remainingTime)"
} else if slider == sliderB {
integerXY = += 0.5
labelXY.text = "\(remainingTime)"
}


I have send the author of the Slider an email and got the follwing text back: You can set a single object to act as a delegate for multiple instances of the slider. So, you’d set the .delegate property of the sliders to be whatever object you have conforming to the delegate and then you’d do the check inside of the delegate callbacks exactly how you’ve shown underneath.

But I don't get it... Help is very appreciated.

Edit:

enter image description here

Answer

You need to do something like this:

func snappingSliderDidIncrementValue(slider: SnappingSlider) {
    if slider == remainingTimeSlider {
        remainingTime += 0.5
        remainingTimeLabel.text = "\(remainingTime)"
    }
    else if slider == percentageSlider {
        // do something for this slider
    }
}