【问题标题】:How to add fade to an animating image set in Swift 3?如何在 Swift 3 中为动画图像集添加淡入淡出?
【发布时间】:2017-03-16 16:11:31
【问题描述】:

我已经设置了几个图像,我需要自动过渡到另一个图像,直到那时一切正常。这是我的代码:

image1 = UIImage(named: "loginBg1.png")
image2 = UIImage(named: "loginBg2.png")

images = [image1, image2]
animatedImage = UIImage.animatedImage(with: images, duration: 3)
backgroundImageView.image = animatedImage

现在的问题是,我需要它们在过渡时褪色。基本上我需要在动画之间添加一个淡入淡出过渡。

另外,如果您有经验,我想在图像中添加刻痕效果,因为它应该在转换之前和之后平移/缩放每个图像。

【问题讨论】:

    标签: ios swift3 uiimage xcode8 uiviewanimation


    【解决方案1】:

    动画图像与过渡不同。您真正想要做的是在显示第二张图像的过渡中添加动画。你可以使用 CATransition() 来实现。

    检查此答案以获取代码: https://stackoverflow.com/a/9773674/2584110

    另外,如果你想要一个固定的解决方案,试试这个 github 库: https://github.com/calm/KenBurns

    这是一个带有肯伯恩斯效应的过渡。

    【讨论】:

    • 感谢您的帮助!
    【解决方案2】:

    这是一个淡出当前图像并淡入新图像的函数。

    extension UIImageView{
        func transition(toImage: UIImage, withDuration duration: TimeInterval){
            UIView.animate(withDuration: duration, animations: {
                self.alpha = 0
            }) { (bool) in
    
                UIView.animate(withDuration: duration, animations: {
                    self.image = toImage
                    self.alpha = 1
                }, completion: nil)
            }
        }
    }
    

    肯伯恩斯效应更难。我建议使用像这样的podhttps://github.com/jberlana/JBKenBurns

    更新:

    这样调用函数:

    let transitionToImage = UIImage.init(named: "newImage")! //newImage is the name of the Image Set in xcassets
    
    let duration: TimeInterval = 1 //seconds, can also be fractions, i.e. 0.3, 0.5
    imageView.transition(toImage: transitionToImage, withDuration: duration)
    

    【讨论】:

    • 嘿,谢谢你,但你能更进一步告诉我如何使用它。我的意思是,我实现了扩展,但是如何调用这个函数呢?非常感谢它,这里是新手。
    • 感谢您的帮助,谢谢! PS:对于任何发现这一点的人,一旦通过此函数调用的图像超出范围,我认为没有应用约束。
    猜你喜欢
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    相关资源
    最近更新 更多