0

I am creating an application with some buttons in a view controller. I want to add one animation like all the buttons are move from right to left. But all my buttons are moving from left to right. Here is my code.

 override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        b1_center_alignment.constant -= self.view.bounds.width;
        b2_center_alignment.constant -= self.view.bounds.width;
        b3_center_alignment.constant -= self.view.bounds.width;
        b4_center_alignment.constant -= self.view.bounds.width;
        b5_center_alignment.constant -= self.view.bounds.width;
}

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated);
        UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
            self.b5_center_alignment.constant += self.view.bounds.width
            self.view.layoutIfNeeded()
            }, completion: nil)

        UIView.animateWithDuration(0.5, delay: 0.3, options: UIViewAnimationOptions.CurveEaseOut, animations: {
            self.b4_center_alignment.constant += self.view.bounds.width
            self.view.layoutIfNeeded()
            }, completion: nil)


        UIView.animateWithDuration(0.5, delay: 0.6, options: UIViewAnimationOptions.CurveEaseOut, animations: {
            self.b3_center_alignment.constant += self.view.bounds.width
            self.view.layoutIfNeeded()
            }, completion: nil)
        UIView.animateWithDuration(0.5, delay: 0.9, options: UIViewAnimationOptions.CurveEaseOut, animations: {
            self.b2_center_alignment.constant += self.view.bounds.width
            self.view.layoutIfNeeded()
            }, completion: nil)

        UIView.animateWithDuration(0.5, delay: 1.2, options: UIViewAnimationOptions.CurveEaseOut, animations: {
            self.b1_center_alignment.constant += self.view.bounds.width
            self.view.layoutIfNeeded()
            }, completion: nil);

    }

I am very new to ios development. Please someone help me to solve this issue. Here b1_center_alignment, b2_center_alignment are center horizontal constraints from storyboard.

Amsheer
  • 7,046
  • 8
  • 47
  • 81
  • Have you tried `-=` instead of `+=`? – keithbhunter Nov 05 '15 at 13:19
  • You mean b1_center_alignment.constant += self.view.bounds.width; in viewwill appear and self.b1_center_alignment.constant -= self.view.bounds.width like this in viewdidappear right? – Amsheer Nov 05 '15 at 13:22
  • Yes. Does that work? – keithbhunter Nov 05 '15 at 13:23
  • I also think the same ant try to do like that and the response is buttons moving from right to left but it is not completed. the issue is initially buttons are in its origional position and animation move top of the button. – Amsheer Nov 05 '15 at 13:34

1 Answers1

2

When you do

b1_center_alignment.constant -= self.view.bounds.width;

You're hiding the button on the left side of the screen, and then with

self.b5_center_alignment.constant += self.view.bounds.width

you move it back to it's original position

You need to invert the signals

b1_center_alignment.constant += self.view.bounds.width;

and self.b5_center_alignment.constant -= self.view.bounds.width

Leonardo
  • 1,740
  • 18
  • 27
  • I also think the same ant try to do like your answer and the response is buttons moving from right to left but it is not completed. the issue is initially buttons are in its origional position and animation move top of the button. – Amsheer Nov 05 '15 at 13:33
  • Did you understand what i mean? If not please tell me i'll add the screenshot – Amsheer Nov 05 '15 at 13:33
  • Sorry, I didn't understand – Leonardo Nov 05 '15 at 13:39
  • I found the issue but don't know how to fix it. My problem is I set the leading space for buttons that is the issue. Once I remove the leading space width is changing. Any idea to fix this? – Amsheer Nov 05 '15 at 14:05
  • If I understood correctly, you don't need the leading space constraint. You need the center alignment and width constraints. – Leonardo Nov 05 '15 at 14:10
  • Yes, you are right. But how to set the width. Because it is changing. – Amsheer Nov 05 '15 at 14:24
  • I suggest you study iOS auto layout a little more through this [tutorial](http://www.raywenderlich.com/115440/auto-layout-tutorial-in-ios-9-part-1-getting-started-2), I think it will help you understand your problem a lot better – Leonardo Nov 05 '15 at 14:29
  • 1
    Thanks, I fixed my issue. One more change I did I just remove leading space and add trailing space. Everything is perfect. – Amsheer Nov 06 '15 at 06:27