首页 > 解决方案 > 如何使 UIImageView 闪烁/闪烁动画无限?

问题描述

我正在尝试在我的 iOS 应用程序中为 UIImageView 实现一段时间的闪烁/闪烁效果。我希望 UIImageView 使用 alpha 闪烁/闪烁,而不是使用不同的图像。

我有另一个来自 android 应用程序的代码片段,它描述了我试图在 XCode Swift 中实现的目标。我把它贴在下面:

这是我迄今为止在 XCode Swift 中所取得的成就:

        ellipses.alpha=0.8
        ellipses.animationDuration=1
        ellipses.animationRepeatCount=0
        ellipses.startAnimating()

当然,它不起作用!我不太确定如何实现这一目标,并且很乐意获得一些指导。

下面的代码适用于 android,而不适用于 Swift/Xcode(但它描述了我的目标非常明确)

        Animation animation = new AlphaAnimation(1, 0); //to change visibility from visible to invisible
        animation.setDuration(1000); //1 second duration for each animation cycle
        animation.setInterpolator(new LinearInterpolator());
        animation.setRepeatCount(Animation.INFINITE); //repeating indefinitely
        animation.setRepeatMode(Animation.REVERSE); //animation will start from end point once ended.
        imageButton.startAnimation(animation); //to start animation

标签: iosswiftimageviewblink

解决方案


您可以反复为图像视图的不透明度设置动画。就像是:

UIView.animate(withDuration: 2.0,
                      delay: 0,
                    options: [.repeat, .autoreverse],
                 animations: { imageView.alpha = 0 }
              )

推荐阅读