user4806509
user4806509

Reputation: 3011

Changing UIWindow image (backgroundColor using a UIColor patternImage) with an animation

I am trying to transition with an animation between two UIWindow images (set using backgroundColor with a UIColor patternImage).

I have two images myImage1 and myImage2. I change the UIWindow backgroundColor once using the function changeImage1() and then again using the function changeImage2(). I also set the view.backgroundColor = UIColor.clearColor() so that I can see what is happening on the UIWindow. Code below.

Test A :

When I change window.backgroundColor = UIColor.redColor() to window.backgroundColor = UIColor.blueColor(), I get a nice transition on the UIWindow between the colors animating red to blue. (See Code A.)

Test B:

However when I set a pattern image window.backgroundColor = UIColor(patternImage:...) and call changeImage1(), myImage1 animates nicely, however when I then call changeImage2() there is no animation, myImage2 abruptly replaces myImage1. (See Code B.)

Questions:

1 - How can I transition with an animation on UIWindow between myImage1 and myImage2?

2 - What am I missing in my code that simply swaps the images instead of animate between them, why is myImage1 animating but myImage2 is not?

3 - Are there other ways to animate two images/cross dissolve on UIWindow that don't use UIColor(patternImage:...)?

Code:

A:

func changeImage1() {
    self.view.backgroundColor = UIColor.clearColor()  //show UIWindow

    UIView.animateWithDuration(2, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
        if let window = UIApplication.sharedApplication().windows.first as UIWindow! {
            window.backgroundColor = UIColor.redColor() //Animation OK
        }
        }, completion: nil)
    }

func changeImage2() {
    UIView.animateWithDuration(2, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
        if let window = UIApplication.sharedApplication().windows.first as UIWindow! {
            window.backgroundColor = UIColor.blueColor() //Animation OK
        }
        }, completion: nil)
    }

B:

func changeImage1() {
    self.view.backgroundColor = UIColor.clearColor()  //show UIWindow

    UIView.animateWithDuration(2, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
        if let window = UIApplication.sharedApplication().windows.first as UIWindow! {
            window.backgroundColor = UIColor(patternImage: UIImage(named: "myImage1")!) //Animation OK
        }
        }, completion: nil)
    }

func changeImage2() {
    UIView.animateWithDuration(2, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
        if let window = UIApplication.sharedApplication().windows.first as UIWindow! {
            window.backgroundColor = UIColor(patternImage: UIImage(named: "myImage2")!) //NO animation
        }
        }, completion: nil)
    }

Upvotes: 0

Views: 504

Answers (1)

matt
matt

Reputation: 535945

Animation implies the existence of a series of intermediate states. There is no intelligible notion of an intermediate state between two pattern images; hence, that animation (from one pattern image to another) fails.

A workaround might be to animate to a color and then, in its completion handler, from the color to the next pattern image. But even that might fail; actually, I'm surprised to hear that your animation from a color to a pattern image worked in the first place.

Upvotes: 1

Related Questions