-2

I'd like to animate an existing UIButton in swift, the goal is to let get the attention of the user to this button when he taps the view controller. The animation is supposed to start from the top in the view controller and go to the initial position of the button.

I did the following scenario but it's not working:

import UIKit

class ViewController: UIViewController {
@IBOutlet var dismissButton: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()

    dismissButton = UIButton()

    var tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTapGesture:"))
    view.addGestureRecognizer(tapGesture)

}



func handleTapGesture(tapGesture: UITapGestureRecognizer) {
    println("tap")

    UIView.animateWithDuration(2.0, delay: 0.5, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: nil, animations: {

        self.dismissButton.center = CGPoint(x: 200, y: 90 + 200)
        }, completion: nil)
}


}

Any suggestion?

Thank you!

Rami Ammoun
  • 855
  • 2
  • 10
  • 25

1 Answers1

1

Step 1: Take IBOutlet of your Top Constraint of button
Step 2: Update your Constraint's constant by using constraintName.constant property
Step 3: In animation block type self.view.layoutIfNeeded()

Here is an simple example: https://stackoverflow.com/a/26040569/3411787

Community
  • 1
  • 1
Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130