问题描述
我正在尝试在两个 UIWindow
图像之间使用动画过渡(使用 backgroundColor
和 UIColor
patternImage
设置>).
I am trying to transition with an animation between two UIWindow
images (set using backgroundColor
with a UIColor
patternImage
).
我有两个图像 myImage1
和 myImage2
.我使用函数 changeImage1()
更改了 UIWindow
backgroundColor
一次,然后再次使用函数 changeImage2()
.我还设置了 view.backgroundColor = UIColor.clearColor()
以便我可以看到 UIWindow
上发生了什么.代码如下.
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.
测试 A:
当我将 window.backgroundColor = UIColor.redColor()
更改为 window.backgroundColor = UIColor.blueColor()
时,我在 上获得了很好的过渡UIWindow
介于红色和蓝色之间的动画.(参见代码 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.)
测试 B:
但是,当我设置图案图像 window.backgroundColor = UIColor(patternImage:...)
并调用 changeImage1()
, myImage1
动画效果很好,但是当我调用 changeImage2()
时没有动画,myImage2
突然替换了 myImage1
.(参见代码 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.)
问题:
1 - 如何在 myImage1
和 myImage2
之间使用 UIWindow 上的动画过渡?
1 - How can I transition with an animation on UIWindow between myImage1
and myImage2
?
2 - 我的代码中缺少什么只是简单地交换图像而不是在它们之间设置动画,为什么 myImage1
可以设置动画而 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 - 是否有其他方法可以在不使用 UIColor(patternImage:...) 的 UIWindow
上为两个图像设置动画/交叉溶解?
3 - Are there other ways to animate two images/cross dissolve on UIWindow
that don't use UIColor(patternImage:...)?
代码:
答:
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)
}
乙:
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)
}
推荐答案
动画意味着一系列中间状态的存在.两个图案图像之间没有可理解的中间状态概念;因此,该动画(从一个图案图像到另一个)失败了.
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.
这篇关于使用动画更改 UIWindow 图像(使用 UIColor patternImage 的背景颜色)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!