UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

return the value of UISlider into func mapView

Forums > Swift

When I click on a UIButton, the @IBAction func pressPlay() starts which, using a timer, moves the thumb of the slider (UISlider) every second. The initial value of the slider is 0.

class MapViewController: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate,SCSEarthquakesHandler,SCSPublicSeismometersHandler
{
    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet var sliderTime: UISlider!

    @IBAction func pressPlay(_ sender: Any)
    {
        timer = Timer.scheduledTimer(withTimeInterval: 1,repeats: true)
                { [self]t in //ogni secondo questo timer cambia il valore dell'alpha del pin che sta vibrando
                    if cnt < 0
                    {
                        cnt = Int(self.sliderTime.value)
                        self.sliderTime.value += 1
                        let newDate2 = calendar2.date(byAdding: .day, value: Int(self.sliderTime.value), to:today)! //sottraggo alla data attuale il vlaore dello slider per tornare indietro nel tempo
                        format.dateStyle = .medium // "MM/GG/AAAA"
                        self.labelTime.text = "\(format.string(from: newDate2))"
                    }
                 }
    }
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
    {
        print(Int(sliderTime.value))
     ...
    }

}

I tried to recall the value of the slider inside the func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { but despite the slider has no value 0, when I go to print it in here it always returns value 0. Why? I tried something ... I defined a global variable and initialized it to 0. I called it inside the mapView (...) function and increased it by 1. I noticed that it increments it every second (minus of a second). So I don't understand why sliderTime.value doesn't return the current value to me, but instead always 0. How can I have the value of the slider printed during this animation?

2      

Hi, did you try using setValue method on the UISlider instead of setting it manually?

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.