Swift Variables in Switch or If statement

I'm trying to learn how to use Swift and I'm just at the beginning. So this code is probably very bad. I can't find some Information about var handling in switch statements.

In the iOS app you can drag a black view (imgView) along the screen. Now if the touch ends, the imgView should animate to a CGPoint which is calculated from the beginning point and end point of the pan gesture.

@objc func handlePan(recognizer: UIPanGestureRecognizer) {

    var locationOfBeganTap  = CGPoint()
    var locationOfEndTap = CGPoint()
    let finalDestination = CGPoint(x: (locationOfBeganTap.x  + locationOfEndTap.x), y: locationOfBeganTap.y  + locationOfEndTap.y)


    switch recognizer.state {

    case .changed, .began:

        locationOfBeganTap = recognizer.location(in: screenView)
        print(locationOfBeganTap)

        let translation = recognizer.translation(in: self.view)
        if let view = recognizer.view {
            view.center = CGPoint(x: view.center.x + translation.x, y: view.center.y + translation.y)
        }
        recognizer.setTranslation(CGPoint.zero, in: self.view)

    case .ended:

        locationOfEndTap = recognizer.location(in: screenView)

        UIView.animate(withDuration: 2.0, animations: {
            print(locationOfBeganTap)

            self.imgView.center = finalDestination
        })

    default:
        break
    }

I want to know why the Variable "locationOfBeginTap" is set correctly on .began but further in the Switch statement, the Variable is set to 0 again. When and why did it change? And How could I avoid that?

Upvotes: 0

Views: 583

Answers (2)

Raul Mantilla
Raul Mantilla

Reputation: 344

Move you variables outside the method.

var locationOfBeganTap  = CGPoint()
var locationOfEndTap = CGPoint()
let finalDestination = CGPoint(x: (locationOfBeganTap.x  + locationOfEndTap.x), y: locationOfBeganTap.y  + locationOfEndTap.y)

@objc func handlePan(recognizer: UIPanGestureRecognizer) {

    switch recognizer.state {

    case .changed, .began:

        locationOfBeganTap = recognizer.location(in: screenView)
        print(locationOfBeganTap)

        let translation = recognizer.translation(in: self.view)
        if let view = recognizer.view {
            view.center = CGPoint(x: view.center.x + translation.x, y: view.center.y + translation.y)
        }
        recognizer.setTranslation(CGPoint.zero, in: self.view)

    case .ended:

        locationOfEndTap = recognizer.location(in: screenView)

        UIView.animate(withDuration: 2.0, animations: {
            print(locationOfBeganTap)

            self.imgView.center = finalDestination
        })

    default:
        break
    }
}

Upvotes: 0

rmaddy
rmaddy

Reputation: 318774

It's all about variable scope. This has nothing do with switch or if. Each time handlePan is called, you create new local variables with their initial values.

Move the declaration of those variables to be outside the function so they are actually properties of your class. Then the values will remain between calls to handlePan.

Upvotes: 1

Related Questions