UIAnimation repeating issue in Swift

大城市里の小女人 提交于 2019-12-12 02:09:02

问题


I have a simple UIButton in my project with animation block running inside the button. Basically, I have a mainButton that connected to two other sub Buttons. When mainButton is pressed, my subButtons will appear to the view with animation. I am using the following code.

@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)
     })
     }

Above code works as I want it to work but only when I press the mainButton for the first time. But, the problem is that nothing happens when I press the mainButton again. I want the animation to occur every time when the mainButton is pressed....

Thanks in Advance.


回答1:


You are having an issue with the animation timing for appearing and disappearing. Both portions of animation are working fine. If you click the button ahead of time, you are having issues with the X location of the button. Notice that if you comment on of "pieces of animation" your code works fine. You can print the X values and see what is happening.

Few options:

  1. Toggle between the two.
  2. Create a starting point, and when the button is pressed:

    class ViewController: UIViewController {
    
    var intialPointXButton1: CGFloat = 0.0
    var intialPointXButton2: CGFloat = 0.0
    ...
    
  3. Disable the button while the animation is running:

    @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
        })
      })
    }
    


来源:https://stackoverflow.com/questions/37453328/uianimation-repeating-issue-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!