Matthew Kaplan
Matthew Kaplan

Reputation: 119

trying to change the background color in a fading manner

I'm trying to change the background color from an image to white in a fading manner. Here's the current code I have:

UIView.animate(withDuration: 1, delay: 0, animations: { () -> Void in self.answer.alpha = 0})
UIView.animate(withDuration: 1, delay: 0, animations: { () -> Void in self.continueLabel.alpha = 0})
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [unowned self] in self.view.backgroundColor = UIColor(displayP3Red: 255/255, green: 255/255, blue: 255/255, alpha: 1)}

As you can see I'm also having two other items fade out, which are working fine. Right now though I just have the background change to white with a delay of 0.5 seconds, which doesn't look as smooth as I'd like. Ideally I'd like to have all three (the two labels and the background) fade together, with delay 0 and duration 1. I tried setting the background to white with alpha = 0, and then changing the alpha to 1 in a fading manner but that did not work either. If anybody could please provide some insight I'd greatly appreciate it. Thanks!

Also, below I've included how I called the image background, just in case it's helpful:

UIGraphicsBeginImageContext(self.view.frame.size)
    UIImage(named: "nyc")?.draw(in: self.view.bounds)
    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    self.view.backgroundColor = UIColor(patternImage: image)

Upvotes: 0

Views: 555

Answers (1)

matt
matt

Reputation: 535880

Ideally I'd like to have all three (the two labels and the background) fade together, with delay 0 and duration 1

Then why didn't you do that? You animated the alphas; why didn't you animate the background color? I mean to say, it isn't going to animate unless you tell it to animate:

UIView.animate(withDuration: 1, delay: 0, animations: { self.view.backgroundColor = UIColor(displayP3Red: 255/255, green: 255/255, blue: 255/255, alpha: 1)})

By the way, did you know you're allowed to animate more than one view in one animations block? Plus you don't need an in line for a known function type. So the whole thing can be rewritten a lot more neatly.

UIView.animate(withDuration: 1, delay: 0, animations: { 
    self.answer.alpha = 0
    self.continueLabel.alpha = 0
    self.view.backgroundColor = // whatever
})

EDIT: I’m going to guess that the animation of the background color doesn't work for you because your background color is a pattern. In that case you will need to use trickery! You’ll need another view in front of the background that animates from clear color to white color. — On the other hand, maybe you should never have used a pattern image to start with. Personally I never use them. If I want a backdrop, I put an image view behind everything else. If you had done that, you could now fade the image view's alpha along with the others, revealing the empty white window and view behind everything.

Upvotes: 1

Related Questions