移动按钮从右到左动画iOS - swift

时间:2015-11-05 13:10:00

标签: ios swift ios-animations

我正在视图控制器中创建一个带有一些按钮的应用程序。我想添加一个动画,就像所有按钮从右向左移动一样。但我的所有按钮都是从左向右移动。这是我的代码。

 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);

    }

我对ios开发很新。请有人帮我解决这个问题。此处b1_center_alignment, b2_center_alignment是来自storyboard的中心水平约束。

1 个答案:

答案 0 :(得分:2)

当你这样做时

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

您隐藏了屏幕左侧的按钮,然后使用

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

你把它移回原来的位置

您需要反转信号

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

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