MattBlack
MattBlack

Reputation: 3828

Very Basic Swift Animation

I am trying to move a view from its position on the storyboard up by 100px.

If the random generated number is 1, the label changes to say thay have not won.

If the random generated number is 2, the label changes to say they have won a shot.

I am using the following code:

 override func motionEnded(motion: UIEventSubtype,
    withEvent event: UIEvent?) {

        if motion == .MotionShake{

           let randomNumber = arc4random_uniform(2) + 1

            print(randomNumber)

            if randomNumber == 1 {

                labelOne.text = "Sorry!"
                labelTwo.text = "You Have Not Won This Time"
                labelThree.text = "Better Luck Tomorrow"

            }

            if randomNumber == 2 {

                labelOne.text = "Winner!"
                labelTwo.text = "1 x Shot Patron Cafe"
                labelThree.text = "Show at the bar to claim."

            }

            let xPosition = stampView.frame.origin.x

            //View will slide 20px up
            let yPosition = stampView.frame.origin.y - 100

            let height = stampView.frame.size.height
            let width = stampView.frame.size.width

            UIView.animateWithDuration(3.0, animations: {

                self.stampView.frame = CGRectMake(xPosition, yPosition, height, width)


            })


            print("SHAKEN!")

        }

}

I am finding that sometimes when I shake the device the animation starts at 100px below the original position and moves back to it, and other times it will move an additional 100px up from its last position.

How can I ensure it always moves from its original position on the storyboard?

Upvotes: 1

Views: 176

Answers (1)

Noah Wilder
Noah Wilder

Reputation: 1574

The best way to ensure that something stays in the same location is to give it constraints in your Main.storyboard file (first thing to do). And also, as an extra precaution, you can read its frame value in viewDidLoad and set it again in motionEnded.

var stampFrame : CGRect = CGRect(x: 0, y: 0, width: 0, height: 0) // This is stampFrame's initial value before being set in viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()
    stampFrame = stampView.frame // Here we store the frame value for usage later
    // If you would like, you can also set this in a method like viewWillAppear or viewDidAppear, but I'd just keep it in viewDidLoad
}


override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {

    stampView.frame = stampFrame 

    if motion == .MotionShake{

       let randomNumber = arc4random_uniform(2) + 1

        print(randomNumber)

        if randomNumber == 1 {

            labelOne.text = "Sorry!"
            labelTwo.text = "You Have Not Won This Time"
            labelThree.text = "Better Luck Tomorrow"

        }

        if randomNumber == 2 {

            labelOne.text = "Winner!"
            labelTwo.text = "1 x Shot Patron Cafe"
            labelThree.text = "Show at the bar to claim."

        }

     //   let xPosition = stampView.frame.origin.x

     //   View will slide 20px up
     //   let yPosition = stampView.frame.origin.y - 100

     //   let height = stampView.frame.size.height
     //   let width = stampView.frame.size.width
     // The code above is redundant, we can just do it all in the UIView.animate 

        UIView.animate(withDuration: 3.0, animations: {

            self.stampView.frame.origin.y -= 100
     // This moves stampView's position up 100pts from its current location


        })


        print("SHAKEN!")

    }

Well anyways, I hope that helps, if you have any more questions, your welcomed to ask.

Upvotes: 2

Related Questions