【问题标题】:How to make UIImageView blink/flash animation infinite?如何使 UIImageView 闪烁/闪烁动画无限?
【发布时间】:2019-04-10 07:00:16
【问题描述】:

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

我有另一个来自 android 应用程序的 code-sn-p,它描述了我试图在 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

【问题讨论】:

标签: ios swift imageview blink


【解决方案1】:

使用下面的代码来获得像眨眼一样的图像动画。

图像闪烁(使用图像视图隐藏/显示):-

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.alarmAlertActivate), userInfo: nil, repeats: true)
    }

    @objc func alarmAlertActivate(){
        myImage.isHidden = !myImage.isHidden
    }

图像闪烁(使用图像视图 alpha):-

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        Timer.scheduledTimer(timeInterval: 0.7, target: self, selector: #selector(self.alarmAlertActivate), userInfo: nil, repeats: true)
    }

@objc func alarmAlertActivate(){
        UIView.animate(withDuration: 0.7) {
            self.alarmImage.alpha = self.alarmImage.alpha == 1.0 ? 0.0 : 1.0
        }
    }

【讨论】:

  • 谢谢,就像一个魅力!我想要一些不透明度/alpha,你对如何实现它有什么建议吗?
  • 是的,你可以改变 UIImageView 的 alpha,我会更新我的代码。
【解决方案2】:

您可以反复为图像视图的不透明度设置动画。类似的东西:

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

【讨论】:

  • "'UIImageView 类型的值?'没有成员“不透明””
  • @Logi95 使用“阿尔法”
【解决方案3】:

我想让我的按钮突然闪烁/闪烁,而不是像大多数 IOS 动画默认那样平滑地淡入和淡出。上面接受的答案是使用计时器来完成的,但您也可以设置常规动画师类来执行此操作 - 无需平滑。下面是一个闪烁按钮边框颜色的示例,但这可以用于任何需要相同效果的东西。

这里的诀窍是使用“关键帧”动画,并将计算模式设置为“离散”:

    let color = CAKeyframeAnimation(keyPath: "borderColor")
    color.values = [CGColor.white, CGColor.black, CGColor.white]
    color.keyTimes = [0, 0.5, 1]
    color.duration = 1
    color.repeatCount = .infinity
    color.calculationMode = .discrete
    gotItButton.layer.borderWidth = 3
    gotItButton.layer.add(color, forKey: "")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-03
    • 2011-12-02
    • 2012-03-12
    • 2011-03-30
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多