简体   繁体   中英

Swift animateWithDuration is completing immediately

Here is my code i am getting the printed output immediately. i want to change the backgroundcolor of my uiview in this duration.

func animate() {

UIView.animateWithDuration(3.0, delay: 0.0, options:

UIViewAnimationOptions.CurveLinear, animations: {

  //self.grid.backgroundColor = UIColor.redColor()            
  println("1")

  UIView.animateWithDuration(1.0, delay: 1.0, options: UIViewAnimationOptions.CurveLinear, animations: {

      //self.grid.backgroundColor = UIColor.clearColor()
      println("2")

    },
    completion: {
      (finished: Bool) - > Void in

        println("3")

    })
}, completion: {
  (finished: Bool) - > Void in

    println("4")

  })
}

The thing you are trying to animate is not animatable. By inspecting your code i understood that you are trying to make a view transparent.

You can achieve this by making the alpha of the view zero. (Alpha is animatable).

The same with the red colour. Try using layers background colour instead

Also keep in mind that animationWithDuration is not delaying the assignment of the value, the value is being assigned immediately

UIView.animateWithDuration(3.0, delay: 0.0, options:UIViewAnimationOptions.CurveLinear, animations: {

  self.grid.layer.backgroundColor = UIColor.redColor().CGColor           


  UIView.animateWithDuration(1.0, delay: 1.0, options: UIViewAnimationOptions.CurveLinear, animations: {

      self.grid.alpha = 0


    },
    completion: {(finished: Bool) - > Void in})
}, completion: {(finished: Bool) - > Void in})}

Several things. Only some things are animatable. println() is not animatable.

Second, it's not clear what nesting a call to UIView.animateWithDuration inside the animation block of another call to UIView.animateWithDuration would do.

It is reasonable to nest a 2nd call to UIView.animateWithDuration inside the completion block of an outer UIView.animateWithDuration call, but not in the animation block.

The code that MIGHT be animating is commented out, so what do you hope to accomplish?

Edit:

Here is an IBAction method that animates a view's background color from white to red, then back to white. It works perfectly:

@IBAction func handleAnimateButton(sender: UIButton)
{
  sender.enabled = false
  UIView.animateWithDuration(NSTimeInterval(0.2),
    delay: NSTimeInterval(0.5),
    options: UIViewAnimationOptions.CurveEaseInOut,
    animations:
    {
      self.aView.backgroundColor = UIColor.redColor()
  },
    completion:
    {
      This code gets called when the first animation finished.
      (value: Bool ) in
      //Create a new animation to switch the BG color back to white
      UIView.animateWithDuration(NSTimeInterval(0.5),
        animations:
        {
          self.aView.backgroundColor = UIColor.whiteColor()
        },
        completion:
        {
          (value: Bool ) in
          sender.enabled = true
        }
      )
    }
  )
}

Note that the second animation call is inside the first animation call's completion block.

I think the problem you're encountering is that you're calling the option animateWithDuration inside another one and a delay . This might break the program. You should fix that.

Hope that helps :)

I'm no expert but Duncan's answer of nesting the second animation within the completion block of the first animation should work for you. I had to delete that final "sender.enabled = true" piece to make it work. That said, if any readers' timing issues aren't solved by that, here are other options I've read about...

Use NSTimeInterval: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/

Or using grand central dispatch (powerful, but complicated): http://www.raywenderlich.com/79149/grand-central-dispatch-tutorial-swift-part-1

Apparently iOS has a new method: http://commandshift.co.uk/blog/2014/04/01/stop-nesting-animation-blocks/

Again, I think sequenced animation can be obtained through the completion block method but these are some alternatives.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM