UIAnimation在Swift中重复出现问题

时间:2016-05-26 06:40:04

标签: swift uiviewanimation

我的项目中有一个简单的UIButton,在按钮内部运行动画块。基本上,我有一个mainButton连接到另外两个子按钮。按下mainButton时,我的子按钮将显示在包含动画的视图中。我使用以下代码。

@IBOutlet weak var mainButton: UIButton!
@IBOutlet weak var subButton1: UIButton!
@IBOutlet weak var subButton2: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

     subButton1.hidden = true
     subButton2.hidden = true
}

 //    override func viewDidAppear(animated: Bool) {
 //        subButton1.hidden = true
 //        subButton2.hidden = true
 //    }

@IBAction func mainButtonAction(sender: AnyObject) {

     self.subButton1.hidden = false
     self.subButton2.hidden = false

     self.subButton1.center.x += self.view.frame.width
     self.subButton2.center.x -= self.view.frame.width

     //First Piece of Animation:
     UIView.animateWithDuration(1.5, delay: 0.05,usingSpringWithDamping: 0.9,initialSpringVelocity: 0.5, options:UIViewAnimationOptions.CurveEaseIn, animations: {

     self.subButton1.center.x -= self.view.frame.width
     self.subButton2.center.x += self.view.frame.width

        }, completion: {finished in 

      //Second Piece of Animation:       
     UIView.animateWithDuration(1.5, delay: 5, options:UIViewAnimationOptions.CurveEaseOut, animations: {

     self.subButton1.center.x += self.view.frame.width 
     self.subButton2.center.x -= self.view.frame.width

     }, completion:  nil)
     })
     }

上面的代码可以正常工作,但只有当我第一次按下mainButton时才能工作。但是,问题是当我再次按下mainButton时没有任何反应。我希望每次按下mainButton时都会出现动画....

先谢谢。

1 个答案:

答案 0 :(得分:2)

您在出现和消失的动画时间方面遇到问题。动画的两个部分都运行正常。如果提前单击按钮,则表示按钮的X位置存在问题。 请注意,如果您评论“动画片段”,您的代码就可以正常工作。您可以打印X值并查看发生的情况。

几个选项:

  1. 在两者之间切换。
  2. 创建一个起点,按下按钮时:

    class ViewController: UIViewController {
    
    var intialPointXButton1: CGFloat = 0.0
    var intialPointXButton2: CGFloat = 0.0
    ...
    
  3. 动画运行时禁用按钮:

    @IBAction func mainButtonAction(sender: AnyObject) {
    
      self.subButton1.hidden = false
      self.subButton2.hidden = false
    
      self.subButton1.center.x += self.view.frame.width
      self.subButton2.center.x -= self.view.frame.width
      self.mainButton.enabled = false // Disable the main button
    
      //First Piece of Animation:
      UIView.animateWithDuration(1.5, delay: 0.05,usingSpringWithDamping: 0.9,initialSpringVelocity: 0.5, options:UIViewAnimationOptions.CurveEaseIn, animations: {
    
      self.subButton1.center.x -= self.view.frame.width
      self.subButton2.center.x += self.view.frame.width
    
      }, completion: {finished in
    
        //Second Piece of Animation:
        UIView.animateWithDuration(1.5, delay: 5, options:UIViewAnimationOptions.CurveEaseOut, animations: {
    
          self.subButton1.center.x += self.view.frame.width
          self.subButton2.center.x -= self.view.frame.width
    
          }, completion:  { finished in
            self.mainButton.enabled = true // Enable the main button
        })
      })
    }